Linux Section 6 - BASH Shell Scripting                   

   Contact
   C
   C++
   Visual Basic
   Java
   JavaScript
   DHTML
   Style Sheets
   About
   Normalization
   Active X
   TDC Binding
   PHP
   Perl and CGI
   Flash
   XML
   SQL
   Chat
   MCSE
   Linux
   Cabling   
 

   
 
    
    

1. Batch File

#! /bin/bash

# System Command Batch File
#This script will display a list of currently logged in users followed by
# the system hostname, time and date, disk usage, current working
# directory and pathname to the BASH shell.

echo “Users currently on the system: "
who

echo “\nThe system’s hostname is:  \c"
echo $HOSTNAME

echo “\nThe current date and time is: \c"
date

echo “\nCurrent disk usage is: "
df

echo “\nYour present working directory is:  \c“
echo $PWD

echo “\nThe pathname to the bash shell is: \c“
echo $BASH
 

Note: Save this file as “MyBatchFile”.  When you run this file, use “./” before the file.

 

2. Grades (if/elif/fi)

#!/bin/bash

# Decision structure, example of if/elif/fi. This program
# calculates a grade from a number entered by the user.

echo “Please enter a grade between 0 and 100:"
read GRADE

if [ $GRADE –lt 50 ]
then
            echo “The grade is F”

elif [ $GRADE –lt 60 –a $GRADE –ge 50 ]
then
           echo “The grade is D”

elif [ $GRADE –lt 70 –a $GRADE –ge 60 ]
then
          echo “The grade is C”

elif [ $GRADE –lt 80 –a $GRADE –ge 70 ]
then
         echo “The grade is B”

elif [ $GRADE –le 100 –a $GRADE -ge 80 ]
then
        echo “The grade is A”

else
        echo "Invalid number – please try again"

            fi
 

Note: Save this file as “IfGrades”.  To run a script file, use the “./” before the script name.



3. Menu Decision Structure
(if/elif/fi)

#!/bin/bash

# Decision structure, example of if/elif/fi. This program
# creates a menu of choices.

Echo –e “    MENU
What would you like to see?

(d) The Current Date
(u) Users Who Are Currently Logged In
(r)  Directory Listing

Enter your choice of (d/u/r) ”
read TheirChoice
 

if [ $TheirChoice = “d” –o $TheirChoice = “D” ]
then
           echo –e “Today’s date is: ”
           date

elif [ $TheirChoice = “u” –o $TheirChoice = “U” ]
then
          echo –e “At present, the people logged into the system are: “
          who

elif [ $TheirChoice = “r” –o $TheirChoice = “R” ]
then

          echo –e “The listed contents of the current directory are: “
          ls -l

else
         echo "That is an invalid choice for this menu."

fi

 

Note: Save this file as “IfMenu”.  When you run this file, use “./” before the file.

 

4. Menu Decision Structure (Case Statement)

#!/bin/bash

# Decision structure, example of if/elif/fi. This program
# creates a menu of choices.

Echo –e “    MENU

What would you like to see?

(d) The Current Date
(u) Users Who Are Currently Logged In
(r)  Directory Listing

Enter your choice of (d/u/r)”'
read TheirChoice

case $TheirChoice in

        d | D )  echo –e “Today’s date is: \c”
                    date
                    ;;

        u | U )  echo –e “\n\nAt present, the people logged into the system are: “
                    who
                    ;;

        r | R )  echo –e “\n\nThe listed contents of the current directory are: “
                   ls –l
                   ;; 

        * )  echo “That is an invalid choice for this menu."
 
esac
 

Note: Notice the “*)” above.  That constitutes a default statement, to be
executed if none of the other conditions are met.

 Special Characters:

-o = “OR”

&& = command on right is executed only if command on left executes successfully.

-a = “AND”

|| = command on right is executed only if command on left did NOT execute successfully.

! = “NOT”

-lt = less than

-gt = greater than

-eq = equals

!= - not equal to

-f A = is a file that exists.

 

5. Decision Structure

#!/bin/bash

echo -e “What is your favorite color? :”   
read REPLY

          if [ “$REPLY” = “red”  –o  “$REPLY” = “blue” ]
          then
                    echo “The answer is red or blue.”
          else  
                    echo “The answer is not red nor blue.”
          fi
 

 

6. Simple Text File Database (if/fi/elif)


#!/bin/bash

echo ' Do you wish to add a contact to the address book? '
read choice1

if [ $choice1 = “y” –o $choice1 = “Y” ]
then
        echo ' Please enter the contact name: '
        read contactName

        echo ' Please enter the contact company or organization: '
        read contactCompany

        echo ' Please enter the contact telephone number: '
        read contactPhone

        echo ' Please enter the contact street address: '
        read contactStreet

        echo ' Please enter the contact state and zip code: '
        read contactZip

        # Now write the contents to a file via redirection.  Below should all be on same line.
        # Note that you need to also have created an empty text file called "ContactDatabase".
        # You could use the "touch" command for this.

        echo -e “Name: $contactName   Company: $contactCompany   Phone: $contactPhone  Street:  
        $contactStreet   Zip: $contactZip” >> ContactDatabase

fi

echo ' Would you like to search for an entry in the contact database? '
read choice2

if [ $choice2 = “y” –o $choice2 = “Y” ]
then
              echo ' What word do you wish to search for? '
              read searchItem

              grep $searchItem ContactDatabase

elif [ $choice2 = “n” –o $choice2 = “N” ]
then
             echo ' O.k., I guess not. '

else
            echo ' That was not an option. '

fi
 

 

7. Simple Text File Database (Case Statement and While/Do/Done loop)


#!/bin/bash

#Below we assign a variable to be used as a sentinel value.
loopit="y"
AreYouSure="n"

while [ $AreYouSure = "n" ]
do

     echo
     echo "What would you like to do now?

     (a) Add a contact entry
     (s) Search for a contact entry
     (x) Exit "

     read menuChoice1

     case $menuChoice1 in

     a | A )

            #Example of a do/while loop. Executes all code between "do" and "done" till falls out.
            while [ $loopit = "y" ]
            do

            echo ' Please enter the contact name: '
            read contactName

            echo ' Please enter the contact company or organization: '
            read contactCompany

            echo ' Please enter the contact telephone number: '
            read contactPhone

            echo ' Please enter the contact street address: '
            read contactStreet

            echo ' Please enter the contact state and zip code: '
            read contactZip

            #Now write the contents to a file via redirection. Need to have created file.
            echo -e Name: $contactName Company: $contactCompany Phone: $contactPhone Street:   
$contactStreet Zip: $contactZip >> ContactDatabase

             echo '\n\n'
             echo 'Would you like to enter another? (y/n)'
             read loopit
             done
             ;;

     s | S )
             echo ' What word do you wish to search for? '
             read searchItem

             grep $searchItem ContactDatabase
             ;;

     x | X )
             echo ' You have chosen to exit. Bye bye. '
             AreYouSure="y"
             exit
             ;;

     * )
             echo ' For shame, that was an invalid response on your part! '


     esac

done

©2005 C. Germany