#!/bin/ksh # # Some script syntax examples: # # reading output from one program to use in a variable ps -eaf | grep httpd | while read USER PID PPID THEREST; do grep "error: $PID" /var/adm/somelogfile done # # The problem with the previous example is that you can't do # something with standard out in the middle of the loop, since # you are already using standard out. Another way to do it is this: ps -eaf | grep httpd | while read USER PID PPID THEREST; do PIDLIST="$PIDLIST $PID" done for CURPID in $PIDLIST; do grep "error: $PID" /var/adm/somelogfile >> /tmp/someotherlog done # # You can also format the output of "ps" the way you want using the # "-o" option. ps -eaf -o pid -o comm | grep "httpd" | while read PID CMD; do kill $PID done # # You can also use program output this way: # Suppose you want to see what the permissions and ownerships were on some # command that was in your path: ls -al `which su` # # Or suppose you wanted to save a date/time stamp so that you could use # it multiple times: DTIME=`date +"%m%d%y.%H%M"` mv log1 log1.$DTIME mv log2 log2.$DTIME # # Some examples of the "test" operation: # -r file exists and is readable # -w file exists and is writable # -x file exists and is executable # -f file exists and is a regular file. (not a dir) # -d file is a directory # -h file is a symbolic link # -c file is a character special file # -b block special file # -p named pipe # -u setuid file # -g setgid file # -k sticky bit set # -s file exists and has a size greater than 0 # -z test for zero len string # -n non-zero length string # # Numeric test operators: # -lt less than # -le less than or equal to # -gt greater than # -ge greater than or equal to # -eq equal to # -ne not equal to # # String test operators # = equal # != not equal # > greater than # < less than # # boolean operators # -a and # -o or # ! not # if [ -f /tmp/myfile ]; then cat /tmp/myfile fi # # elif structure if [ ! -f $FILE ]; then echo "No such file $FILE" elif [ ! -r $FILE ]; then echo "$FILE not readable" else echo "File is readable" fi # # Processing command line options: while [ $# -ge 2 ]; do case $1 in -f) FILE=$2 shift 2 ;; -d) DEVICE=$2 shift 2 ;; *) echo "usage: $0 [-f ] [-d ]" exit 1 ;; esac done # # This is an example of of subroutine definition and call hup_daemon () { DAEMON="$1" ps -e -o pid,comm | grep "$DAEMON" | while read PID COMM; do echo "send HUP to $PID $COMM" kill -HUP $PID done }; hup_daemon inetd # Writing data to syslog /usr/local/bin/someCommand > /var/adm/someCommand.log if [ $? -eq 0 ]; then logger -p user.notice "NOTE: someCommand worked just fine" else logger -p user.err "ERROR: someCommand had a problem" fi #sending a file as an attachment in email uuencode /var/adm/someCommand.log someCommand.log | mailx -s "Log file from someCommand" someUser@somedomain.com