Programming Construct Examples

lists

#!/bin/bash

(echo "executing in a separate shell")

{echo "executing in the current shell"}


for

# 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

# tar up the doc, c and cc files in a directory.

for suffix in "doc" "c" "cc"
do
   tar -rf *.$suffix $1.tar
done


select

#!/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


case

#! /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


if-then-else

#!/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

#!/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


while,until

#!/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


functions