#!/bin/bash #the shebang, or some call it the hashpling! For it to work, the shebang (#!) must be the first two bytes in the file. #then anything following it will be considered the path to the interpreter (bash, perl, tcl or whatever.) #Speaking of whatever, bash stands for Bourne Again Shell, and is a bourne shell(sh) variant. Another family of shells #in unix are based on the C shell (csh). Bash is the default shell in linux. #also, remember that a user must have at least read and execute permissions on the script in order #to run it. RPMDIR="/opt/rh7.1/redhat/RPMS" #no spaces allowed in shell variable assignments NEWRPMDIR="/opt/rh7.1/redhat/newrpms" OLDRPMDIR="/opt/rh7.1/redhat/oldrpms" TMPDIR="/opt/rh7.1/redhat/tmp" export RPMDIR NEWRPMDIR OLDRPMDIR #exporting the values makes them available to subshells - eg all of the commands we call in this script. for i in $(ls $NEWRPMDIR) #yikes, a variable, for loop and command substitution in a single line! do #there are for, while, and until loops - all use the do-done grammar. t=$(echo $i | sed 's/-[0-9]\././g' | cut -f1 -d.) #another command substitution to set the value of $t. notice a couple of things here. #first, we don't use the $ notation when assigning the value of a variable, #only when calling it - as when we echo $i from within the command substitution. #also this syntax for command substitution - $(command) is the same as the #old-fashioned bourne shell syntax `command`. what is command substitution? #the output of the command in the parenthesis will be substituted for the #command itself. So the line 'echo "$(date)"' will run as 'echo "Thu Aug 9 14:27:30 PDT 2001"' #and produce the output 'Thu Aug 9 14:27:30 PDT 2001'. On this line we also see #a classic unix pipeline, where the standard out from one command becomes the #standard in for the next. echo $t >> $TMPDIR/names.tmp #redirection. >> redirects STDOUT to a file by concatenating to the end of the file. # > redirects STDOUT to file by overwriting the file. mv $RPMDIR/$t[-][0-9]*i386.rpm $OLDRPMDIR/ 2>>$TMPDIR/baddies.tmp #more redirection; this time we are redirecting STDERR, standard #error or any error messages that result from this command to #a file. It's important to use >> while you're in a loop #because otherwise you'll overwrite the contents of the file #on every iteration of the loop! The special variables 0 1 2 #are STDIN STDOUT and STDERR. By default they are your keyboard/mouse, #the screen, and the screen. But you can redirect them at will. done #closes the loop #checking for discrepencies ourold=$(ls $OLDRPMDIR | wc -l) #more command substitution ournew=$(ls $NEWRPMDIR | wc -l) if [ -s $TMPDIR/baddies.tmp ] || [ $ourold != $ournew ] #at last some logic! the if statement takes the basic form of #if (condition) then (commands) else (commands) fi (closes the if) #we also have the test command, which takes the form of # "test statement" or "[ statement ]" the space after the #square bracket is mandatory because the [ is considered a command. #read the test man page for lots of fun. #also notice the logical or (||) between two tests. If one OR # the other is true, the commands following 'then' will run. #there is also an optional elif. then mail -s "RPM update is screwed up!" brenda < $TMPDIR/baddies.tmp #redirecting STDIN with <. the standard in for the mail command #will be the contents of our baddies.tmp file mv $OLDRPMDIR/* $RPMDIR exit #bail out of the script now. else mv $NEWRPMDIR/* $RPMDIR mv $OLDRPMDIR $OLDRPMDIR$(date +\%y\%m\%d) ; rm -f $TMPDIR/*.tmp #commands can be seperated by ; mkdir $OLDRPMDIR fi #closes the if statement