#!/bin/bash (echo "executing in a separate shell") {echo "executing in the current shell"}
# count the words in a group of file # wcount filelist, e.g. wcount "*.html" count=0 for filename in $1 do count=$[$count + `wc $filename | cut -c1-10`] done echo "The word count is" $count
bash -xv wcount "junk junk1" #!/bin/bash count=0 + count=0 for filename in $1 do count=$[$count + `wc $filename | cut -c1-10`] done wc $filename | cut -c1-10 ++ wc junk1 ++ cut -c1-10 + count=4 wc $filename | cut -c1-10 ++ wc junk ++ cut -c1-10 + count=15 echo $count + echo "The word count is" 15 The word count is 15
# tar up the doc, c and cc files in a directory. for suffix in "doc" "c" "cc" do tar -rf *.$suffix $1.tar done
#!/bin/sh # Select host and username for telnet login username=bozo:BozoC:BClown # host gets the string and REPLY gets the number from select select host in {bigtop ringling barnum} do echo "logging on to" $host uname=`echo $username | cut -d: -f$REPLY` echo "uname = " $uname (telnet $host -l uname &) return done
host> bash -xv login_source.sh #!/bin/sh username=bozo:BozoC:BClown + username=bozo:BozoC:BClown # host gets the string and REPLY gets the number from select select host in {bigtop ringling barnum} do echo "logging on to" $host uname=`echo $username | cut -d: -f$REPLY` echo "uname = " $uname telnet $host -l uname return done 1) {bigtop 2) ringling 3) barnum} #? 2 + echo logging on to ringling logging on to ringling echo $username | cut -d: -f$REPLY ++ echo bozo:BozoC:BClown ++ cut -d: -f2 + uname=BozoC + echo uname = BozoC uname = BozoC + (telnet ringling -l uname &) + return host> ringling: Unknown host child process
#! /bin/bash # Script to handle different login properties for different hosts # Output a prompt and get the hostname # an alternative is to do the following and not have the user input # host=`hostname | cut -d. -f1` echo Enter your hostname read host # Execute the startup stuff for the correct host case $host in trumpet) echo starting on trumpet set path=/usr/bin:/usr/opt/bin export $path ;; tuba) echo starting on tuba set path=/usr/bin:/opt/*/bin export $path ;; drum | trombone) echo starting on drum or trombone set path=/usr/bin:/usr/local/*/bin export $path ;; esac
#!/bin/sh # An improved move move commmand that always checks for existance # and creates a backup. if [ $# -lt 2 ] then echo "You need to enter two filenames" fi # If the file exists, move it to a new name. if [ -e $2 ] then mv $2 $2.1 fi mv $1 $2
bash -xv newmove junk junk2 #!/bin/sh # An improved move move commmand that always checks for existance # and creates a backup. if [ $# -lt 2 ] then echo "You need to enter two filenames" fi + [ 2 -lt 2 ] # If the file exists, move it to a new name. if [ -e $2 ] then mv $2 $2.1 fi + [ -e junk2 ] + mv junk2 junk2.1 mv $1 $2 + mv junk junk2
#!/bin/bash # script to mail a message if system load gets too high averages=`uptime | tr -s " " | cut -d " " -f11-13 | tr -d ","` avg1=`echo $averages | cut -d " " -f1 | cut -d. -f1` avg2=`echo $averages | cut -d " " -f2 | cut -d. -f1` avg3=`echo $averages | cut -d " " -f3 | cut -d. -f1` warn=0 # increment warning depending on length of load increase if [ $avg1 -gt 2 ] then warn=$[$warn+1] fi if [ $avg2 -gt 2 ] then warn=$[$warn+2] fi if [ $avg3 -gt 2 ] then warn=$[$warn+4] fi # depending on the level of warn, mail the appropriate message to # the admingroup mail group if [ $warn -gt 0 ] then if [ $warn -ge 6 ] then echo "Serious load average increase" \ | mail -s "Warning" admingroup@localhost elif [ $warn -gt 4 ] then echo "Moderate load average increase" \ | mail -s "Warning" admingroup@localhost elif [ $warn -gt 2 ] then echo "Minor load average increase" \ | mail -s "Warning" admingroup@localhost fi fi
harkin> bash -xv ./load_monitor.sh #!/bin/bash # script to mail a message if system load gets too high averages=`uptime | tr -s " " | cut -d " " -f11-13 | tr -d ","` uptime | tr -s " " | cut -d " " -f11-13 | tr -d "," ++ uptime ++ tr -s ++ cut -d -f11-13 ++ tr -d , + averages=average: 2.19 2.06 avg1=`echo $averages | cut -d " " -f1 | cut -d. -f1` echo $averages | cut -d " " -f1 | cut -d. -f1 ++ echo average: 2.19 2.06 ++ cut -d -f1 ++ cut -d. -f1 + avg1=average: avg2=`echo $averages | cut -d " " -f2 | cut -d. -f1` echo $averages | cut -d " " -f2 | cut -d. -f1 ++ echo average: 2.19 2.06 ++ cut -d -f2 ++ cut -d. -f1 + avg2=2 avg3=`echo $averages | cut -d " " -f3 | cut -d. -f1` echo $averages | cut -d " " -f3 | cut -d. -f1 ++ echo average: 2.19 2.06 ++ cut -d -f3 ++ cut -d. -f1 + avg3=2 warn=0 + warn=0 if [ $avg1 -gt 2 ] then warn=$[$warn+1] fi + [ average: -gt 2 ] ./load_monitor.sh: [: integer expression expected before -gt if [ $avg2 -gt 2 ] then warn=$[$warn+2] fi + [ 2 -gt 2 ] if [ $avg3 -gt 2 ] then warn=$[$warn+4] fi + [ 2 -gt 2 ] if [ $warn -gt 0 ] then if [ $warn -ge 6 ] then echo "Serious load average increase" \ | mail -s "Warning" admingroup@localhost elif [ $warn -gt 4 ] then echo "Moderate load average increase" \ | mail -s "Warning" admingroup@localhost elif [ $warn -gt 2 ] then echo "Minor load average increase" \ | mail -s "Warning" admingroup@localhost fi fi + [ 0 -gt 0 ]
#!/bin/bash # simple loop list=$1 while [ -n $list ] do file=`echo $list | cut -d" " -f1` list=`echo $list | cut -d" " -f2-` echo $file echo $list done
#!/bin/bash # check to make sure the argument was entered. if [ $# -lt 1 ] then echo "You must enter the loop limit" exit fi # counting loop ct=0; while [ $ct -lt $1 ] do echo $ct ct=$[$ct + 1]; done