Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

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

Using grep and egrep


Grep and egrep are the utilities for filtering output. Egrep can handle more complex criteria (like and / or, regular expressions)
# display detailed info on session processes ps -elf | grep $LOGNAME

# 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"

Using awk/nawk 101


Awk and nawk are powerful tools for processing output. Pattern scanning and matching are one of the many features which make these utilities indispensible. This first section deals with simple one-liners.
# print a simpler file list ls -lap | awk '{ print $9 "\t\t\t" $5 }'

# 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}

Using awk/nawk 201


This section illustrates several multi-line awk programs.
# Example 1a: to use an awk program file: ls -lap | nawk -f myfile.awk # Example 1b: passing a value to an awk program. # The variable "myvar" will be accessible within the test6.awk program ls | nawk -f test6.awk myvar=10 # Example 2: list users idle for mre than 15 minutes w | nawk -f idle15.awk # idle15.awk { if ($2~/pts/ && $4 > 15 && $1!~/root/) { print $1 system("echo " $1) } } # Example 3: file list, with total kbytes / meg #!/bin/ksh ls -lap | awk -f /usr/lib/scripts/dir3.awk # dir3.awk { if (length($9) > 7) {

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 }

Using awk/nawk 301


More complex examples. Note how the BEGIN and END blocks execute once. Also check out the getline function, which allows a file to be read within an awk program.
#------------------------------------------------------# Run kill script for users idle for more than 55 minutes # ... exclude users found in developers.txt file # .. note that the system function works in nawk only. #------------------------------------------------------BEGIN { devlist = "." while (getline devname < "developers.txt") { devlist = devlist "." devname } } { if ((substr($6,1,1) > 0 || substr($6,3,2) > 55) && index(devlist, $1)==0) { system("ps_kill " $1) } } #------------------------------------# Compare passwd file with # phone listing, change desc # in passwd file, send output to /tmp #------------------------------------BEGIN { FS=":" } { pline=$0 pwdline=$5 chflag=0 len2=0 while ((getline pname < "/tmp/list1.txt") && (chflag==0)) { addrline=pname; len2 = length(addrline); addrline2 = substr(addrline,1,len2 - 4 ); if ((index(pwdline,addrline2) > 0) && (len2 > 5)) {

} close("/tmp/list1.txt") ; print pline ;

pline=$1 ":" $2 ":" $3 ":" $4 ":" pname ":" $6 ":" $7 ; chflag=1 ; }

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

echo echo echo echo exit

" " "useage: " "chxtn source-xtn dest-xtn" " "

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>"

if test echo echo echo echo exit fi cd $1 i=1

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

You might also like