Upload
others
View
11
Download
0
Embed Size (px)
Citation preview
Summit 2007: Get Connected
W621 – Increasing Productivity with Shell Scripting
Al FreemanTechnical Services
Cliché statements are generally true. Isn’t it better to work smarter not harder? While many companies opt to pay for custom software or continue to manually handle repetitive tasks, those a bit savvier utilize UNIX scripts to automate procedures. This session will cover more advanced shell scripting techniques specifically for SDI users, such as running any SDI program from a shell script, including interactive forms programs; customizing your END-OF-DAY for different days of the week or month with conditional execution; and using "expect" and "cron" to automate nearly anything on the system.
Pre-requisites: Unix knowledge
Wednesday, 3:45 p.m.
© 2007 Activant Solutions Inc. All Rights Reserved
22
Session Objectives
At the end of this course you will be able toWrite shell scripts to automate proceduresKnow when to and when not to automateKnow where to find more information on commands and their options and arguments
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
33
Return on Investment Opportunity
Avoid unplanned downtimeWork more efficiently, giving more time to
Assist sales and management by implementing high-level tools which turn data into useable informationCreate graphs and reports highlighting opportunities and potential problems Provide longer hours of system availability and increase remote accessibility
© 2007 Activant Solutions Inc. All Rights Reserved
44
Case Study
Activant SDI customer, APCO, Inc., wrote a shell script to import price updates into their SDI system saving them hours of manual labor each month They saved additionally by not having a custom program written to do these updates
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
55
Shell Structures
VariablesPositional parametersCommand substitutionConditional execution
© 2007 Activant Solutions Inc. All Rights Reserved
66
Shell Structures
VariablesVariables are used to store information during the execution of a shell scriptVariable names should be unique within the first eight charactersBraces { } can be used to delineate the start and end of variable names for clarity
Example:today=${month}${day}${year}
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
77
Shell Structures
Positional parametersAre special variables created by the shellEvery word on the command line is assigned a number, the first word is 0, the second is 1, etc.Use $# to get the value of the parameter
$0 is the first word (the command), $1 is the first argument to the command, etc
Use $* to get the values of all parametersThe "shift" command will move all positional parameters one number lower, discarding the first
© 2007 Activant Solutions Inc. All Rights Reserved
88
Shell Structures
Command substitutionCommands put in backwards quotes (`) are executed and their output is substituted in place of the commandThis is often used to get information into variables in shell scripts
Example:today=`date +%m%d%y`puts the current date in MMDDYY format into the variable called “today”
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
99
Shell Structures
Conditional executiontestifcase
Looping structuresforwhile
© 2007 Activant Solutions Inc. All Rights Reserved
1010
Conditional Execution
The “test” command can be used to determine if a condition is true or notThe “test” command has two formsExamples of two equivalent “test” statements
test "$color" = "white"[ "$color" = "white" ]
“test” is often used with “if” and “while” statements to control execution
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
1111
Conditional Execution
The “if” statement is used to control execution of statements when some condition is metAt the end of an “if” statement is a “fi”“else” and “elif” are allowed to control what happens when the condition for the if is not true
© 2007 Activant Solutions Inc. All Rights Reserved
1212
The “if” Statement Structure
if [ "$color" = "white" ]thenwater=hot
elsewater=cold
fi
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
1313
Conditional Execution
The “case” statement is used when execution depends upon multiple values of a single variableAt the end of a “case” statement is an “esac”Value possibilities should be listed from most specific to least specific
© 2007 Activant Solutions Inc. All Rights Reserved
1414
A “case” Statement Example
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
1515
Looping Structures
The “for” statement can be used to loop with automatic parameter substitutionThe structure of a for loop isfor variable in list of valuesdo
Statements to be executeddone
© 2007 Activant Solutions Inc. All Rights Reserved
1616
A “for” Statement Example
for file in *.lpdo
lp –dlineptr ${file}done
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
1717
Looping Structures
The “while” statement is used to loop while a condition is trueThe structure for a while loop iswhile conditional statement
doStatements to be executed
done
© 2007 Activant Solutions Inc. All Rights Reserved
1818
A “while” Statement Example
# This example will “sleep” until 11:00 PM
now=`date +%H%M` #Gets current timewhile [ "$now" –lt "2300" ]
dosleep 60now=`date +%H%M` #updates now
done
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
1919
Automating SDI Programs
There are three types of SDI programsPrograms that have initial inputPrograms that ask questionsFull screen programs
© 2007 Activant Solutions Inc. All Rights Reserved
2020
Programs That Have Initial Input
These are the easiest programs to automateThe format of these programs in a script is
SD-ZIP "PROGRAMNAME~INITIAL_INPUT~~PRINTER"
Most "batch" type programs use this formatA script consisting of these programs can be just SD-ZIP lines one following another or can have more elaborate controls within itMost END-OF-DAY programs are of this type
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
2121
Programs That Have Initial Input
© 2007 Activant Solutions Inc. All Rights Reserved
2222
Programs That Ask Questions
A little harder to automate than initial input programs, but still pretty easyThese programs can be automated like this:
SD-ZIP "PROGRAMNAME" <<eofanswer1answer2answer3eofThe eof strings must match or it won't work.
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
2323
Programs That Ask Questions
© 2007 Activant Solutions Inc. All Rights Reserved
2424
Full Screen Programs
These are the hardest programs to automateA backup should be run before running any shell script that runs a screen programThere are generally three task categories to running screen programs
Initialization task where you set all the parameters needed for running the programAction task where you actually are doing the work the program is supposed to doFinishing task where you finish up and tell the program to end
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
2525
Initialization TaskARCUSTMTN
© 2007 Activant Solutions Inc. All Rights Reserved
2626
Action Task / Exit TaskThis is where the repetitive work will be done
When the repetitive work is done, this is where the program will be ended
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
2727
Steps to Writing a Screen Program Script
Figure out what you want to changeManually go through the process of changing two records and record every key that you hit
If the program has a way to reduce the number of fields which can be changed like ARCUSTMTN or INMAINT, make sure the fewest possible fields can be changed
It will make writing the script easier because there will be fewer keystrokes to figure outIt will be much safer because only the specified fields can then be changed
© 2007 Activant Solutions Inc. All Rights Reserved
2828
Steps to Writing a Screen Program Script
Create a list of the records and values to be changed
Use SDIWriter or Excel with ODBCGet the smallest number of records to change possibleMake sure you get the main key field (Customer #, Part #, etc.) and any fields whose values need to be changedSave as a CSV file for easy processing with a shell
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
2929
Write the Main Loop First
This is going to be the most time consuming part of the shellCreate a “for loop” to process every record in the CSV file you created earlierIt’s a good idea to “Home” the cursor on every screen and then tab to the correct field for data entry
© 2007 Activant Solutions Inc. All Rights Reserved
3030
Write the Main Loop First
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
3131
Add the Codes to Get to the Main Screen
© 2007 Activant Solutions Inc. All Rights Reserved
3232
Add the Codes to End the Program
Now the logic of the shell script is complete
Running the shell will produce a file called “keystrokes” which can then be used to run ARCUSTMTN
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
3333
Running the Screen Program
Once the keystroke file is created it can be used as input to the screen program
SD-START ARCUSTMTN <keystrokes
Before running the script: make sure you back up every file affected by the program you’re going to run from the script
© 2007 Activant Solutions Inc. All Rights Reserved
3434
Using “expect” to Run Programs
You will need to have “expect” installed on your system“Expect” can be used to run programs in the background or through cron that normally require to be run from a terminalA typical “expect” script for running SDI in the background#!/usr/local/bin/expect --set timeout -1send "cd /PRISM\r"spawn MY_SPECIAL_SHELL_SCRIPTexpect
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
3535
Automating Tasks with “cron”
Use “crontab –e” to edit the crontab fileLogin as root if you want the commands to run as root before using crontab –eLogin as yourself and run “crontab –e” if the programs don’t need to run as rootEach user gets their own “crontab” file which controls the programs that cron will run on that users’ behalf
© 2007 Activant Solutions Inc. All Rights Reserved
3636
Automating Tasks with “cron”
The format of the crontab entries isMinute – (0-59) Minute of the hour when the job will runHour – (0-23) Hour of the day when the job will runDay – (1-31) Day of month when the job will runMonth – (1-12) Month of the year when the job will runDay of Week – (0-6) Day of the week on which the job will be run
0=Sunday6=Saturday
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
3737
Automating Tasks with “cron”
A typical crontab fileEnd of day is being started Monday-Friday at 11:30 PM
© 2007 Activant Solutions Inc. All Rights Reserved
3838
Automating Tasks with “at”
The “at” command can be used to schedule jobs on the fly
Exampleat 8:30 PM todayshutdown –Fr<Ctrl-D>Exampleat now +30 minutesenable printer1<Ctrl-D>
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
3939
Additional Resources
Exploring the UNIX System by Stephen G. Kochan and Patrick H. WoodEssential System Administration (Third Edition)by Æleen FrischUNIX “man” command, to get more information about any UNIX command, type “man command” at the $ or # prompt
© 2007 Activant Solutions Inc. All Rights Reserved
4040
Print out the SD-START shell script and try to figure out what each statement doesAdd at least one program to your END-OF-DAY process on your own. Write at least one shell script to do something that you frequently are required to doAutomate one process using cron
Suggested Action Plan
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
4141
Summary
Shell scripts can automate many tasks on the systemEvery SDI program can be run via a shell scriptBackups should be run before running new shell scripts in case the script does something wrongExpect can be used to run programs that require terminals in the background (or through cron)Cron can automatically execute programs at specific times and days
© 2007 Activant Solutions Inc. All Rights Reserved
4242
Thank You for Attending
W621 – Increasing Productivity with Shell ScriptingAl FreemanPlease submit the Session Feedback Form
To receive NASBA credits, please be sure to complete the Session Feedback Form and sign the class roster in the back of the room
Session Feedback Form Summit 2007 – Las Vegas, NV
Please take a moment to evaluate this session and offer feedback. Activant uses your input to understand your needs and to determine future Summit sessions.
Session Name: Session Number:
Presenter’s Name:
How important is this topic to your job/company? 1 2 3 4 5
Not Important Important
Please rate the educational value you received from this session. 1 2 3 4 5
Low Value High Value
1. What software are you currently using?
2. How long have you personally used this software?
3. Describe the effectiveness of your instructor.
4. What is your overall evaluation of this session?
5. What could have been done to improve this session?
6. What sessions would you like to see presented at future conferences?
7. What issues will be critical to your business in the next 12-24 months?
Check here if you would like CPE credits. To receive credits, be sure to sign your name at the bottom of this form and sign the roster in the session room.
Answering the following questions is OPTIONAL (but required for CPE Credits).
Yes No
Did this session meet your expectations, based on the description/objectives in the registration materials?
Were the pre-requisite requirements stated in the course description appropriate?
Did the session materials contribute to achieving the learning objectives?
Did the equipment (screen, microphone, projector, etc.) in the room enhance the instruction?
Was the time allotted for the session appropriate for the topic?
Name:__________________________________________________________________________________________________
Company:_____________________________________________________ E-mail:___________________________________