Professional Documents
Culture Documents
Unix 101
Unix 101
Information here is useful for all Unix systems ; some examples are geared toward Solaris, AIX, and Linux systems, with shell scripts leaning toward Bourne and Korn flavors. Setting variables
# set a variable (ksh) logfile=/usr/dumps/errors.log echo "Log file = " $logfile # set a variable to a command result date1=`date` echo "The date is " $date1 servname = `uname -n` echo "The server name is " $servname # numeric variable (ksh) i=1 i=`expr $i + 1` echo "i = " $i # set a variable (csh) TEST1="2522" export TEST1 echo "Test 1 = $TEST1"
Getting input
# get a value from the user, put it in myvar (ksh) read myvar'?Selection:> ' echo "myvar=" $myvar
Conditionals
Examples are for ksh and sh unless otherwise noted
if test $myvar = "20" ; then echo "myvar is twenty" fi if test ! $myvar = "20" ; then echo "myvar is not twenty" fi if test $# -lt 3 ; then echo "The script received less than 3 parms" fi # check for file existance
if test -f /usr/scripts/contact.txt ; then echo "contact.txt file was found !!!" fi # check for file existance, exit script if not found if test ! -f /usr/scripts/contact.txt ; then echo " contact.txt file not found " exit fi #!/usr/bin/ksh # --------------------------------# complete script, ksh if then stmt # note use of directive, at line 1 #---------------------------------i=12 i=`expr $i + 1` if [ $i -gt 5 ] ; then echo "i is greater than five" else echo "i is less or equal to than five" fi if [ $i -eq 5 ] ; then echo "i equals 5" fi
Looping
# loop until 'x' is entered while read sel1'?Selection:> '; do if test $sel1 = 'x' ; then clear exit fi echo 'Loop continues ...' done # loop for each qualifying file for filename in "*.txt" ; do echo 'Deleting ...' $filename rm $filename done
A simple menu
A menu is presented which automates simple system tasks. Two files are necessary: sysmgr (script file) and sysmgr.mnu (menu text)
#!/usr/bin/ksh #---------------------------------# System manager menu #----------------------------------
clear cat /usr/lib/scripts/sysmgr.mnu while read sel1'?Selection:> '; do if test $sel1 = "x" ; then clear exit fi if test $sel1 = "1" ; then finger fi if test $sel1 = "2" ; then ps -elf fi cd print "--------------------------------------------" read sel2'?Press any key to continue ...' clear cat /usr/lib/scripts/sysmgr.mnu done //------- listed below is contents of sysmgr.mnu System Menu 1 -> List active sessions 2 -> List all processes x -> Exit
# display only lines which contain the word "error" cat errors.log | grep "error" # display only lines which contain the word "error" or "warning" cat errors.log | egrep "error|warning" # display only lines which do not contain the word "message" cat errors.log | grep -v "message"
Using sed
The sed stream editor makes changes to submitted input and routes it to standard output. The examples here are fairly straightforward, as complex sed use is not needed, with the advent of Perl.
# replace ":" with "-"
cat /etc/passwd | sed "s/:/-/g" # replace spaces with "-" ls -lap | sed "s/ /-/g"
# list users idle for more than 15 minutes, not including root w | awk '$1!~/root/' | awk '$4 > 15 {print $1}' # same as above, but force line to contain "pts" w | awk '$2~/pts/ && $4 > 15 && $1!~/root/ {print $1}' # move the oldest file to the tmp directory ls -t *.txt | nawk '{ fname=$1 } END{ system("mv " fname "/tmp") }' # reverse two terms, separated by a comma echo "aaa, bbb" | sed s/,//g | awk '{print $2 " " $1}
print $9 "\t\t" $5 } else { print $9 "\t\t\t" $5 } x1 = x1 + $5 } END{ print "----------------------------" print "Total bytes:\t\t" x1 x2 = x1 / 1000000 print "Total meg: \t\t" x2 }
if (chflag==0) { system("echo " "\"" pline "\"" " >> /tmp/list2.txt") ; } else { modcmd = "zzz" addrline "zzz " ; system("echo " "\"" "usermod -c " modcmd $1 "\"" ">> /tmp/list3.txt") ; } } # to call the above awk program, use cat /etc/passwd | nawk -f passwdmod.awk # this final step puts the quotes in the usermods file cat /tmp/list3.txt | sed "s/zzz/\"/g" > /tmp/usermods
Finding files
A handy script for finding files quickly.
#!/usr/bin/ksh if test then echo echo echo echo echo echo fi exit $# -lt 1 " " "usage:" "------" "find2 " " " " "
echo " " echo "Searching file systems ..." echo " " find $PWD -name "$1" 2>/dev/null
Moving/copying files
A handy script for changing file extensions. The source could be modified to copy and move files.
#!/usr/bin/ksh #----------------------------# File Extension Changer # Parms: extension, new-extension #----------------------------if test $# -lt 2 ; then
fi
for fname in *.$1 ; do echo "Moving: " $fname newname=${fname%%.*} if test -f $fname ; then mv $fname $newname.$2 fi done
Deleting files
This script deletes the first n# files in a directory, useful for numbered log files.
#-----------------------------------------# Parms: Directory, wildcard, # of files to delete #-----------------------------------------if test then echo echo echo echo echo echo fi exit ! -d $1 ; then " " " Invalid path: " $1 " " $# -lt 3 " " "usage:" "------" "rmfiles " " " "
<# of files>"
for filename in $2 ; do echo $filename rm $filename i=`expr $i + 1` if test $i -gt $3 ; then cd exit fi done
Sending mail
#!/usr/bin/ksh # send an e-mail, via shell script rmail jsmith@mydomain.com << endmsg2 From sjones 10:00am remote from servername Hi Jim Send an e-mail if we're still on for lunch at 12:30 ... Steve endmsg2 #!/usr/bin/ksh #------------------------------------# Send big mail #------------------------------------if test $# -lt 4 then echo " " echo "usage:" echo "------" echo "bigmail " echo "(type quotes around message if more than one word)" echo " " echo " " exit fi print From $2 3:00pm remote from $3 > /tmp/r1.txt print " " >> /tmp/r1.txt banner $4 >> /tmp/r1.txt print '.' >> /tmp/r1.txt rmail $1 < /tmp/r1.txt rm /tmp/r1.txt