#! /bin/bash # # This is a comment - script1.bash # count=5 # put YOUR first and last name in the variable name # name="your name" echo "This is my first script" echo $name echo $count
Check your path with echo $PATH and if "." is not in your path add it with export PATH=$PATH:. OR export PATH=$PATH:
Be sure that the working directory is NOT the first element in the path statement.
Commands can be run from within your shell script. You can also access environment variables. For the second shell script enter the lines below. This shows how command substitution works. The output of the date and pwd commands are substituted into the strings being echoed.
#! /bin/bash # # script2.bash # echo echo "The current date and time is $(date)." echo "My current directory is $(pwd)" echo "My terminal type is $TERM." echo
When passing arguments to a program or script, it is sometimes helpful to see what they are. The variables $1, $2, $3 , etc stand for the arguments passed to the script in order on the command line. $0 is the name of the command, script or program, $# is the number of parameters passed in, and $* and/or $@ list all the command line parameters. Consult your text for the differences between $* and $@.
Add an appropriate construct that will display a usage message if the correct number of arguments (3) aren't entered on the command line. Remember that arg(0) is not part of the total argument count.
#! /bin/bash # # script3.bash arg1 arg2 arg2 # echo echo "The name of the program is: $0" echo "The first argument is: $1" echo "The second argument is: $2" echo "The third argument is: $3" echo "There were $# arguments passed to this program." echo "The list of arguments contains : $*" echo
Background:
The shell variable $? provides the return code from the last command executed.
Run the following commands to see how it works:
When there was an error, $? provided the error number
This return code can be used to put some error handling in a shell script. The backslash character ( \ ) is a continuation character. It must be the last character on that line, not even spaces are allowed after the continuation character
#! /bin/bash # # script four.bash # # error handling demonstration # # Notice the variable $$ in the first non-comment line of the script # The $ (dollar sign) is the pid (process id) of the script while it's running # Putting the pid in the filename of the output makes it unique. # That way you can run it multiple times or have multiple copies running at once # and the output goes to a different file each time. # # Uses line breaks to make code easier to read # ls two.bash if [ $? -eq 0 ] then echo "two.bash DOES EXIST in the current directory" \ >>two.out.$$.txt 2>&1 else echo "two.bash DOES NOT EXIST in this directory" \ >>two.out.$$.txt 2>&1 fi
#!/bin/bash # # script5.bash # # An example with the case statement # # Reads a command from the user and processes it # echo -n "Enter your command from the following list (who, list, or cal) at the prompt: " read command case "$command" in who) echo "Running who..." who who > fivewho.out ;; list) echo "Running ls..." ls ls > fivels.out ;; cal) echo "Running cal..." cal cal > fivecal.out ;; *) echo "Bad command, your choices are: who, list, or cal" > fivebadcommand.out ;; esac exit 0
#!/bin/bash # # scrip6.bash # # An example with the case statement # # Reads a command from the user and processes it # # Execute with one of # six.bash who # six.bash ls # six.bash cal # echo "Took command from the argument list: '$1'" case "$1" in who) echo "Running who..." who who > sixwho.out ;; list) echo "Running ls..." ls ls > sixls.out ;; cal) echo "Running cal..." cal cal > sixcal.out ;; *) echo "Bad command, your choices are: who, list, or cal" > sixbadcommand.out ;; esac exit 0
#!/bin/bash # # script7.bash # # Illustrates implementing a counter with a while loop # ((num=1)) while [[ $num -le 10 ]] do echo "the value of num is $num" ((num++)) done echo "loop completed"
intel cel 466 $95.00 intel cel 500 $105.00 intel cel 600 $129.00 intel cel 700 $205.00 intel p-iii 500 $159.00 intel p-iii 600 $189.00 intel p-iii 650 $199.00 intel p-iii 700 $219.00 intel p-iii 750 $389.00 intel p-iii 800 $399.00 intel p-iii 850 $539.00 intel p-iii 933 $759.00 amd k6-2 533 $125.00 amd k6-2 550 $145.00 amd k7 600 $155.00 amd k7 650 $169.00 amd k7 700 $175.00 amd k7 750 $199.00 amd k7 800 $249.00 amd k7 850 $339.00 amd k7 900 $434.00
#!/bin/bash # # script8.bash # # Illustrates use of a while loop to read a file # # Use the line break to make code more readable cat microp.dat | \ while read line do echo "Found line: $line" done
#!/bin/bash # # script9.bash # # Illustrates implementing a counter with an until loop # ((i=1)) until [[ $i -gt 10 ]] do echo "the value of i is $i" ((i++)) done echo "Looping complete"
#!/bin/bash # # script10.bash # # Illustrates implementing a for loop with a list # for food in burgers fries shakes do echo "I like $food" done echo "ahhhhhhhh...a complete meal"