Upload
others
View
16
Download
0
Embed Size (px)
Citation preview
Summit 2007: Get Connected
W402 - Writing UNIX Scripts: The Basics
Al FreemanTechnical Services
Writing shell scripts can be an important part of UNIX system administration, relieving administrators and users from manually performing repetitive tasks.This session will cover the basics of writing shell scripts as well as some basic UNIX commands with parallels between AIX, Linux and SCO UNIX.
Pre-requisites: None
Wednesday, 1:15 p.m.
© 2007 Activant Solutions Inc. All Rights Reserved
22
Session Objectives
At the end of this course, you will be able toUse basic UNIX commands Combine commands using pipesWrite simple shell scripts to automate proceduresKnow 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 information, such as 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
Basic UNIX Commands
Most UNIX commands can be broken down into groups based on their functionThis class covers commands for
File / Directory administrationPrinter administrationProcess administration
© 2007 Activant Solutions Inc. All Rights Reserved
66
File / Directory Administration
UNIX has a hierarchical, single-tree file structureFiles / directories can be
Created (touch, mkdir)Copied (cp)Moved (mv)Deleted (rm, rmdir)
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
77
File / Directory Creation Commands
Files can be created by programs or by redirection
Example: To create a file called Summit under the /usr/p21 directory
touch /usr/p21/Summit >/usr/p21/Summit
Directories can be created with the “mkdir” command
Example: Create a directory under /usr called p21mkdir /usr/p21
© 2007 Activant Solutions Inc. All Rights Reserved
88
Copying Files / Directories
Use the “cp” command to copy filesExample: To copy /usr/bin/ls to /tmp/lsuse the command:
cp /usr/bin/ls /tmp/ls
To copy directories use “cp –r”Example: To copy the directory /etc/rc2.dand everything underneath it to /tmp/rc2.duse the command:
cp -r /etc/rc2.d /tmp
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
99
Moving Files
Moving a file is the same as renaming itUse the “mv” command to move files to another directory or to rename them
Example: rename /usr/adm/syslog as /tmp/syslog.oldmv /usr/adm/syslog /tmp/syslog.old
© 2007 Activant Solutions Inc. All Rights Reserved
1010
Moving Directories
Can change the directory’s name, its location or bothUse the “mv” or “mv -r” command to move directories
Example: Renaming a directorymv /usr/p21 /usr/activant
Example: Changing a directory’s locationmv /yardley/headoffice /livermore/headoffice
Example: Changing a directory’s name and locationmv /yardley/p21 /livermore/activant
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
1111
Deleting Files
Use the “rm” (remove) command to delete filesThe “rm -i” command will ask before deleting filesA successful remove will have no outputExample:
rm myfile
© 2007 Activant Solutions Inc. All Rights Reserved
1212
Deleting Directories
Use the “rmdir” or “rm -r” to delete directoriesThe rmdir command will only delete empty directoriesThe “rm -r” command can be very dangerous and should be avoided if possibleExample:
rmdir /users/myemptydirectory
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
1313
Printer Administration
Printer commands belong to one of the following categories
Printer status commandsPrint job creation commandsPrint job deletion commandsPrinter control commands
© 2007 Activant Solutions Inc. All Rights Reserved
1414
Printer Status Commands
Use the “lpstat” command to check the status of printers and print jobsOptions to the “lpstat” command
“lpstat -t” gives a complete status of the printer subsystem“lpstat -o” gives a listing of outstanding print jobs(non-AIX)“lpstat -v” gives a listing of printer devices (non-AIX)
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
1515
Print Job Creation Commands
Use the “lp” command to create print jobs“lp -dqueuename” creates a print job for a specific print queueUse the “qprt” command to create a print job with printer control options (AIX only)
© 2007 Activant Solutions Inc. All Rights Reserved
1616
Print Job Deletion Commands
Use the “cancel” command to delete print jobsExamples
cancel job# (AIX)cancel queuename-job# (UNIX/Linux)
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
1717
Printer Control Commands
Printer can be either UP (enabled) or DOWN (disabled)Enable / disable commands
To enable a printer, use the “enable printqueuename” commandTo disable a printer, use the “disable printqueuename” command
© 2007 Activant Solutions Inc. All Rights Reserved
1818
Printer Control Commands
Non-AIX systems also allow a print queue to accept or reject print job requestsAccept / Reject commands
Use the “accept printqueuename” command to set a print queue to receive print jobsUse the “reject printqueuename” command to set a print queue so it will not accept print jobs
Print jobs sent to a rejecting print queue will failThese errors can cause application programs to end abnormally
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
1919
Process Administration Commands
Process Administration commands can be divided into:
Process Status commandsProcess Priority Control commandsProcess Termination commands
© 2007 Activant Solutions Inc. All Rights Reserved
2020
Process Administration Commands
Process Status commandsUse the “ps” command to view status of running processes“ps -e” to view status of all running processes on the system“ps -uusername” to view status of processes for the user username“fuser -u filename” to show the process IDs of all processes using filename
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
2121
Process Administration Commands
Priority Control commands“nice” and “renice”
Priority ranges are from -20 (low) to +20 (high)Only “root” user can increase the priority of a processExamples:
nice -10 myprogram (lower priority)nice --10 myprogram (higher priority)
© 2007 Activant Solutions Inc. All Rights Reserved
2222
Priority Control Commands
Use the “renice” command to change the priority of a process while the command is running
Examplerenice --20 processID# (runs at highest priority)renice -20 processID#(runs at lowest priority)
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
2323
Process Termination Commands
To terminate a process, you need to know its “process id #”Use the “kill -signal# processid#” to terminate a processCommon signals are
15 SIGTERM (default) terminate1 SIGHUP (the terminal was turned off)9 SIGKILL (terminate immediately without cleanup)
Examplekill -1 123456
© 2007 Activant Solutions Inc. All Rights Reserved
2424
Combining Commands with Pipes
The vertical bar or “pipe” character can be used to combine commands to produce different outputCommand lines containing pipes are often referred to as “pipelines”Data in a pipeline flows from left to right
In the line “command1 | command2”the output of command1 becomes the input of command2
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
2525
Combining Commands with Pipes
Frequently used pipe receivers“grep object” displays only lines containing “object”
ps -e|grep p21 displays lines from “ps -e” containing “p21”
“grep -v object” displays all lines except those containing “object”
ps -e|grep -v p21displays lines from “ps -e” not containing “p21”
© 2007 Activant Solutions Inc. All Rights Reserved
2626
Combining Commands with Pipes
Frequently used pipe receivers“pg” displays output on terminals one page at a time
ps -e|pgdisplays output of “ps -e” on a terminal, one page at a time
“lp” prints output on a printerps -e|lpprints the output of the “ps -e” command on the default printer
“cut” narrows output by column or delimited fieldps -e|cut -c1-6 # displays only the first 6 charactersps -e|cut -f2 -d" " # displays the second space delimited field
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
2727
Combining Commands with Pipes
Pipelines can be very long and are limited only by the length of a UNIX command line (normally 255 characters)Pipelines can combine numerous commands to narrow data to exactly what's needed, for example:
ps -e|grep p21|grep -v ralph|grep -v dave|pgdisplays the output of “ps -e” that contains “p21” and doesn’t contain “ralph” or “dave”, one page at a time
© 2007 Activant Solutions Inc. All Rights Reserved
2828
Shell Structures
VariablesPositional parametersCommand substitutionConditional execution
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
2929
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}
© 2007 Activant Solutions Inc. All Rights Reserved
3030
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, etcUse $# 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
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
3131
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”
© 2007 Activant Solutions Inc. All Rights Reserved
3232
Shell Structures
Conditional executiontestifcase
Looping structuresforwhile
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
3333
Conditional Execution
The “test” command can be used to determine if a condition is true or not“test” 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
© 2007 Activant Solutions Inc. All Rights Reserved
3434
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
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
3535
The “if” Statement Structure
if [ "$color" = "white" ]thenwater=hot
elsewater=cold
fi
© 2007 Activant Solutions Inc. All Rights Reserved
3636
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
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
3737
A “case” Statement Example
case $dayofweek in"Mon") echo "Today is Monday";;"Tue") echo "Today is Tuesday";;"Wed") echo "Today is Wednesday";;"Thu" | "Fri" ) echo "Almost the Weekend";;* ) echo "The weekend is here!";;
esac
© 2007 Activant Solutions Inc. All Rights Reserved
3838
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
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
3939
A “for” Statement Example
i=1for file in `ls /p21`
doecho “File number ${i} is ${file}"i=`expr $i + 1` # increments i
done
© 2007 Activant Solutions Inc. All Rights Reserved
4040
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
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
4141
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
© 2007 Activant Solutions Inc. All Rights Reserved
4242
Shell Script Examples
Stopping and restarting the print spoolerstopsrc –sqdaemonsleep 120startsrc –sqdaemonsleep 300if [ ! "`ps –e|grep qdaemon`" ]; then
echo "qdaemon did not start"|mail rootfi
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
4343
Shell Script Examples
Notify administrator of permanent hardware errors
errpt –a –dH –TPERM | mail root
© 2007 Activant Solutions Inc. All Rights Reserved
4444
Shell Script Examples
E-mail administrator about more than three failed login attempts
lsuser -a unsuccessful_login_count ALL|awk -F= '{if ($2 > 3) print}' -|mail root
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
4545
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
4646
Get to a $ prompt and experiment with non-destructive commands (no rm or mv commands)When writing shells, test each command individually before adding it to the larger shell scriptTry writing at least one shell script to do something that you frequently are required to do
Suggested Action Plan
Summit 2007: Get Connected
© 2007 Activant Solutions Inc. All Rights Reserved
4747
Summary
UNIX commands can be used to administer files, processes and printersCommand output can be “customized” with pipesShell scripts can be used to automate procedures
© 2007 Activant Solutions Inc. All Rights Reserved
4848
Thank You for Attending
W402 – Writing Unix Scripts: The BasicsAl 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:___________________________________