NEED MY WEEK 4 LAB COMPLETE ASAP> LIKE TODAY!!!!
Introduction to Unix - POS420
Unix Lab Exercise Week 4
Parameter Variables
These are some of the variables you have to know. All these variables having a prefix $ are called parameter variables.
These are some of the variables we have to know.
$# number of arguments on the command line
$0 the name of the current shell or program
$1 the first argument
$2 the second argument
..
..
$9 the ninth argument
$* all arguments on the command line ("$1 $2 ... $9")
$@ all arguments on the command line, each separately quoted ($1 $2 ..)
$ cat script3.sh
if [ $# -lt 3 ]
then
echo "Please enter at least three names"
exit
fi
echo "You entered $# names"
echo "The first name is $1"
echo "The second name is $2"
echo "The third name is $3"
echo "this script name is $0"
echo "You have entered these names: $*"
echo "You have entered these names: $@"
Let us run
$ chmod +x script3.sh
$ ./script3.sh tom
Please enter at least three names
$ script3.sh tom smith kay scott
You entered 4 names
The first name is tom
The second name is smith
The third name is kay
This script name is script3.sh
You have entered these names: tom smith kay scott
You have entered these names: tom smith kay scott
Let us run one more time with quotes for a name since there is a space in one name
$ script3.sh kay ron 'tom smith'
You entered 3 names
The first name is kay
The second name is ron
The third name is tom smith
this script name is script3.sh
You have entered these names: kay ron tom smith
You have entered these names: kay ron tom smith
Another example :Enter the following shell script in vi editor.
$ cat variables.sh
echo "$#:" $#
echo '$#:' $#
echo '$-:' $-
echo '$?:' $?
echo '$$:' $$
echo '$!:' $!
echo '$3:' $3
echo '$0:' $0
echo '$*:' $*
echo '$@:' $@
When executed with some arguments it displays the values for the shell variables, e.g.:
$ ./variables.sh one two three four five
5: 5
$#: 5
$-:
$?: 0
$$: 12417
$!:
$3: three
$0: ./variables.sh
$*: one two three four five
$@: one two three four five
Before we go into if-else and loops , let us study some useful topics.
Integer Conditions
|
n1 -eq n2 |
n1 equals n2 |
|
n1 -ne n2 |
n1 not equal to n2 |
|
n1 -gt n2 |
n1 greater than n2 |
|
n1 -ge n2 |
n1 greater than or equal to n2 |
|
n1 -lt n2 |
n1 less than n2 |
|
n1 -le n2 |
n1 less than or equal to n2 |
1. The characters like [ , ] are operators and they need space on both sides like
If [ $1 -eq 1 ] then
2. expr command :
Shell can't do math, rely on expr command to do it for us.
$ expr 4 + 1
5
$ expr 5 \* 4 # note the quoting
20
$ expr '1 * 4' # can't just quote blindly
1 * 4
$ expr 1+1 # can't bunch them up
1+1
$ i=7
$ i=`expr $i + 1` # command subst. to change var
$ echo $i
8
A. if , elif construct
Example 1 : Type the following script ifscript.sh
$ vi ifscript.sh
# ifscript.sh
echo "Enter a number " read num if [ $num -eq 0 ] # Note that space between [ and $num is a must then echo "The value is 0" echo "Some other message " elif [ $num -eq 1 ] then echo "The value is 1" else echo "The value is neither 0 nor 1" fi
Now let us run the script
$ chmod +x ifscript.sh
$ ./ifscript.sh Enter a number 0 The value is 0
Let us run one more time
$ ./ifscript.sh Enter a number 1 The value is 1
One more time
$ ./ifscript.sh Enter a number
5 The value is neither 0 nor 1
Let us check to see whether a variable has a value?
There are several methods for determining this.
$ vi var1.sh
if [ -z "$THEDATE" ]
then
THEDATE=`date`
fi
echo $THEDATE
$ var1.s
Mon Jul 4 11:23:51 EDT 2011
Exit Status :
Whenever any program completes execution, it returns an exit status back to the operating system. The status is a number that usually indicates whether the program successfully ran or not. In unix, by convention an exit status of zero is used to indicate that a program is succeeded and non-zero to indicate that it failed.
In a pipeline, the exit status is that of the last command in the pipe, So in
$ Who | grep fred
The exit status of the grep is used by the shell as the exit status of the pipeline.
The shell variable $? Is automatically set by the shell to the exit status of the last command.
On command line, we can test it. Let us search for fred
$ who | grep fred
$
$ echo $? -- grep failed
1 -- exit status of last grep command
$ echo $?
0 -- exit status of last echo command
$ cp nofile somefile
cp: cannot access nofile
$ echo $?
2 -- Copy failed
Let us create a shell program logged_in
$ vi logged_in.sh
Type the following :
# Checks if a specified user is logged in
user=$1 # Note that there is no space on either side of '='
if who | grep $user
then
echo $user is logged in
fi
$
$ logged_in.sh tony
tony tty10 Jul 4 08:30 -- from where this has come
tony is logged in
The first line of output has come from who command. We dont want this. We just wanted whether user is logged or not
Let us change the script
$ vi logged_in.sh
if [ $# -lt 1 ]
then
echo "Please enter user name you want to search"
exit
fi # Checks if a specified user is logged in
user=$1
if who | grep $user >/dev/null # We don't want error messages, so redirect to /dev/null
then
echo $user is logged in
fi
$
Let us run
$ logged_in.sh tony
tony is logged in
Example : Let us write another program called greetings that would print a friendly message Good Morning, Good Afternoon, Good Evening whenever you logged in.
We know date program shows todays date.
$ date
Mon Jul 4 11:23:58 EDT 2011
$
Let use date in our shell script
$ vi greetings.sh
Type the following:
# This program prints a greeting.
hour=`date | cut -c12-13` # Note that they are back quotes around date and cut command, but not single quotes.
if [ $hour -ge 0 -a $hour -le 11 ]
then
echo Good morning
elif [ $hour -ge 12 -a $hour -le 17 ]
then
echo Good afternoon
else
echo Good evening
fi
$ greetings.sh
Good evening
We can use date + %H also for this.
Another Example :
We have a file phonebook in our previous labs. If you do not find, please Create a file phonebook in your home directory.
$ cat phonebook
Tom Smith 732-330-1111 Kay George 732-300-0000 Penny Smith 973-560-1234 David Korn 908-444-0987
Mary Lisa 732-666-6789 Ashwin Patel 732-786-4567 Tom Kerry 732-456-1456
We write a shell script which removes some one from phonebook
$ vi remove.sh
Type the following
# This program removes someone from the phone book
if [ $# -ne 1 ]
then
echo Incorrect number of arguments
echo Usage: remove name
exit 1
fi
name=$1
# Find number of matchings
matches=`grep $name phonebook | wc -l`
# if more than one match, issue messsage, else remove
if [ $matches -gt 1 ]
then
echo More than one match, can not remove
exit 1
elif [ $matches -eq 1 ]
then
grep -v $name phonebook > /tmp/phonebook
mv /tmp/phonebook phonebook
else
echo Name not found
fi
$ remove.sh Tom
More than one match, can not remove
$ remove.sh Lisa
$ cat phonebook
Tom Smith732-330-1111 Kay George 732-300-0000 Penny Smith973-560-1234 David Korn 908-444-0987 Ashwin Patel732-786-4567 Tom Kerry 732-456-1456
See Lisas entry is remvoed.
Case Command :
The case command allows you to compare a single value against other values and to execute one or more commands when a match is found.
Let us see some examples.
Example 1:
Create a script convert_num with vi editor. The program takes single digit and translates it to its English equivalent.
$ vi convert_num.sh
Type the following program
# This program translates a digit to English
#
if [ $# -ne 1 ]
then
echo Usage: convert_num.sh digit
exit 1
fi
case $1 in
0) echo zero;;
1) echo one;;
2) echo two;;
3) echo three;;
4) echo four;;
5) echo five;;
6) echo six;;
7) echo seven;;
8) echo eight;;
9) echo nine;;
esac
$
Now let us run
$ convert_num.sh 0
zero
$ convert_num.sh 8
eight
$ convert_num.sh --- Try No arguments
Usage: convert_num.sh digit
$ convert_num.sh 12 --- Try a two digit-number
$
Let us refine. We will use special pattern matching characters.
We know ? matches a single character, * matches anything, [] matches range etc.
Let us change our script
$ vi convert_num.sh
Type the following program
# This program translates a digit to English
#
if [ $# -ne 1 ]
then
echo Usage: convert_num.sh digit
exit 1
fi
case $1 in
0) echo zero;;
1) echo one;;
2) echo two;;
3) echo three;;
4) echo four;;
5) echo five;;
6) echo six;;
7) echo seven;;
8) echo eight;;
9) echo nine;;
*) echo "Bad argument; Please enter a single digit only";;
esac
$ convert_num.sh 8
eight
$ convert_num.sh 17
Bad argument; Please enter a single digit only.
Example 2 :
Let us create another script chartype that prints the type of the single character given as argument.
Character types recognized are digits, uppercase, lowercase letters, and special characters other than the first three categories.
$ vi chartype.sh
Type the following program
# This program displays the character type if given as argument.
#
if [ $# -ne 1 ]
then
echo Usage: $0 char # $0 is program name
exit 1
fi
# If the user enters more than one character, we will have problems. Make sure that the only one character is typed.
#
chars=$1
numchars=`echo $chars | wc -c` # back quotes to execute
if [ $numchars -ne 1 ]
then
echo Please type a single character
exit 1
fi
# If the program survived from the above two exits means, the user entered a single character
case $chars in
[0-9] ) echo digit;;
[a-z] ) echo lowercase letter;;
[A-Z] ) echo Uppercase letter;;
* ) echo special character;;
esac
$ chmod +x chartype.sh
Let us run
$ chartype.sh
Usage: chartype char
$ chartype.sh a
Please type a single character
$ chartype.sh 7
Please type a single character
$
We have problems, our program bombed.
How can we catch the error, let us debug the shellscript
$ sh x chartype.sh a
+ [ 1 -ne 1 ]
chars=a
+ wc -c
+ echo a
numchars= 2
+ [ 2 -ne 1 ]
+ echo Please type a single character
Please type a single character
+ exit 1
Aha! numchars is 2 when we enter only a single character. Means wc c is finding 2 characters instead of 1.
How can be it possible? Yes. The input given to wc are two characters, one the letter a and an invisible newline character.
Let us go back and change the line
if [ $numchars -ne 1 ]
to
if [ $numchars -ne 2 ]
Now let us run:
$ chartype.sh a
lowercase letter
$ chartype.sh P
Uppercase letter
$ chartype.sh 9
digit
It looks line it is working. But wc is giving touble, let us rewrite without wc command.
$ vi chartype.sh
Type the following program
# This program displays the character type if given as argument.
#
if [ $# -ne 1 ]
then
echo Usage: $0 char # $0 is program name
exit 1
fi
# If the user enters more than one character, we will have problems. Make sure that the only one character is typed.
#
chars=$1
case $chars in
[0-9] ) echo "digit";;
[a-z] ) echo "lowercase letter";;
[A-Z] ) echo "Uppercase letter";;
? ) echo "Special character";;
* ) echo "Please enter a single character";;
esac
$
Let us run
$ chartype.sh
Usage: chartype char
$ chartype.sh a
lowercase letter
Syam Tangirala University of Phoenix Online Faculty [email protected]