1.19k likes | 1.29k Views
Week 5. 2/15/2005. Topics. Linux CD Demo ? Review (IF statement, script arguments) Numeric Variables Relational and Arithmetic Operators Loops Unix Utilities (?). Lab Exercise.
E N D
Week 5 2/15/2005 Linux-1
Topics • Linux CD Demo ? • Review (IF statement, script arguments) • Numeric Variables • Relational and Arithmetic Operators • Loops • Unix Utilities (?) Linux-1
Lab Exercise • Write a shell script called “mywho” that when executed, creates a file called username_list in your home directory containing a list of all users currently logged into the system. NOTE: You should be able to execute mywho from anywhere in the system. Linux-1
if statement Used to implement “decision-logic” Used to check value of Shell variables Syntax: ifconditionthendo_actionfi condition should return true or false Linux-1
Example - if statement if [ “$password” = “magic” ] then echo “You got it right!” fi Above lines check if value of “password” shell variable is “magic” Linux-1
Exercise • Write a shell script called pass that, when executed, does the following: • Prompts user for password • If user enters “abc123” then displays the message – “You got it right” Linux-1
echo “Enter password” read xyz if [ “$xyz” = “abc123” ] then echo “You got it right” fi Linux-1
if statement (contd.) • The if statement can be extended to include an “else” clause • General Syntax: ifconditionthenaction-1elseaction-2fi Linux-1
Advanced if statement ifconditionthendo_actionelif condition-2 thendo_other_actionfi Linux-1
Advanced if - example if [ “$animal” = “dog” ] then echo “Animal is dog” elif [ “$animal” = “cat” ] then echo “Animal is cat” else echo “Unknown animal” fi Linux-1
Exercise • Write a shell script that, when executed, does the following: • Prompts user for name of state • If user enters MD displays message – “Capital of MD is Annapolis” • If user enters VA displays message – “Capital of VA is Richmond” Linux-1
echo “Enter state” read st if [ “$st” = “MD” ] then echo “Capital of MD is Annapolis” elif [ “$st” = “VA” ] then echo “Capital of VA is Richmond” fi Linux-1
Exercise • Write a shell script that, when executed, does the following: • Prompts user for name of state • If user enters MD displays message – “Capital of MD is Annapolis” • If user enters VA displays message – “Capital of VA is Richmond” • For all other responses, displays message – “Wrong state” Linux-1
echo “Enter state” read st if [ “$st” = “MD” ] then echo “Capital of MD is Annapolis” elif [ “$st” = “VA” ] then echo “Capital of VA is Richmond” else echo “Wrong state fi Linux-1
Shell Script Arguments • Unix commands $ cp oldfile newfile $ cd /tmp • Shell script • $ myscript alpha beta gamma3 arguments alpha is first argument beta is second argument gamma is third argument Linux-1
Shell Script Arguments • Shell scripts can take arguments just like any other Unix commands. • Inside the script, the arguments are referred in a special way $1 first argument $2 second argument …. etc • $* all arguments from $1 • $# number of arguments • $0 name of the script itself Linux-1
Arguments (contd.) • Strings passed after the script name while executing the script • Shell script $ myscript dog cat fish Inside “myscript” $0 myscript $1 dog, $2 cat, $3 fish $* dog cat fish $# 3 Linux-1
Exercise Write a shell script, which when executed, does the following: • If first argument is “secret” then displays message – “first argument is secret” • If first argument is not “secret” then displays the message – “first argument is not secret” Linux-1
if [ “$1” = “secret” ] then echo “First argument is secret” else echo “First argument is not secret” fi myscript myscript secret Linux-1
Exercise • Write a script, which when executed, does the following: • Displays name of the script • Displays number of arguments passed • Displays first argument passed • Displays all the arguments passed Linux-1
echo Script name is $0 echo Number of arguments is $# echo First argument is $1 echo All arguments: $* Linux-1
Arguments (example) • Write a shell script that takes a filename as an argument and creates a backup of the file with the extension .bak • Sample execution $ ./backup myfile Created backup of myfile as myfile.bak Linux-1
cp $1 $1.bak echo Created backup of $1 as $1.bak Linux-1
Arguments • Write a shell script that takes 3 arguments. The script should display an appropriate message if incorrect number of arguments are passed Linux-1
if [ “$#” != “3” ] then echo Incorrect number of arguments fi Linux-1
Numerical Variables • Use the let command to store numerical (integer) values • Syntax:let variable=value • e.g. let no_of_guests=10 let tax=gross-net • Shell does NOT support decimal numbers Linux-1
Arithmetic Operators • The Shell provides operators for the basic arithmetic functions: addition ( + ) subtraction ( - ) multiplication ( * ) division ( / ) remainder( % ) Linux-1
Arithmetic Operators (contd.) • Addition ( + ) cat+dog returns sum of cat and dog • Subtraction ( - ) orange–apple returns difference of orange and apple • Product bus*car returns the product of bus and car Linux-1
Arithmetic Operators (contd.) • Division ( / ) door/chair returns the quotient of door divided by chair • Remainder ( % ) tea%coffee returns the remainder of tea divided by coffee e.g. 10%3 = 1 Linux-1
Arithmetic Operators - Examples • let total=salary+tips • let total=( salary + tips ) • let refund=( tax_paid – tax ) • let tax=( 30 * gross_pay ) / 100 • let no_of_tables=( no_of_guests / 10 ) Linux-1
Numerical Variables • Write a script that takes 2 numbers as arguments and displays the sum of the 2 numbers Linux-1
let sum=$1+$2 echo Sum of $1 and $2 is $sum mysum 10 34 Linux-1
Exercise • Write a script that prompts the user for 2 numbers and displays their product Linux-1
echo “Enter 1st number” read num1 echo “Enter 2nd number” read num2 let p=num1*num2 echo “Product of $num1 and $num2 is $p” Linux-1
Relational Operators • Greater than X -gt Y Returns TRUE if X is greater than Y else returns FALSE • Less than X -lt Y Returns TRUE if X is less than Y, else returns FALSE • Equal to X -eq Y Returns TRUE if X is equal to Y Linux-1
Relational Operators - Examples if [ $net_pay -gt 40000 ] then # net pay > 40k echo Net pay is greater than 40k let tax_rate=30 fi Linux-1
Relational Operators (contd.) • Greater than or Equal to X -ge Y Returns TRUE if X is greater than OR equal to Y, else returns FALSE • Less than or Equal to X -le Y Returns TRUE if X is less than OR equal to Y, else returns FALSE Linux-1
Relational Operators (contd.) • Inequality E -ne F Returns true if E is not equal to F Linux-1
Exercise • Write a shell script that when executed, does the following • Prompts user for age • If user enters a number greater than 18 then displays message – “adult” • If user enters a number less than or equal to 18 then displays message – “kid” Linux-1
echo “Enter age” read age if [ $age –gt 18 ] then echo “Adult” else echo “Kid” fi Linux-1
Exercise • Write a shell script that when executed, does the following If argument passed is greater than 18 displays message – “adult” Linux-1
if [ $1 –gt 18 ] then echo “Adult” else echo “Kid” fi Linux-1
Exercise • Write a shell script that takes 2 numbers as its arguments and displays the greater of the 2 numbers. If more than 2 arguments are passed, the script should exit, displaying an appropriate message. Linux-1
if [ $# -eq 2 ] then if [ $1 –gt $2 ] then echo $1 is greater than $2 elif [ $1 –eq $2 ] then echo $1 is equal to $2 else echo $2 is greater than $1 fi else echo Incorrect number of arguments fi Linux-1
Multiple Conditions • The if statement can be used to check for multiple conditions at the same time • e.g. Can check if variable-1 = value-1 AND variable2 = value-2 Check if variable-1 = value-1 OR variable-2 = value-2 Linux-1
Binary and Operator -a Binary and Operator expr1 –a expr2 Returns TRUE if expr1 AND expr2 are both TRUE expr1 expression1 expr2 expression2 The shell evaluates expr1 and expr2 separately. Linux-1
and operator examples if [ $age –ge 13 –a $age –le 19 ] then … fi if [ $car = “Chevy” –a $color = “red” ] then Linux-1
Another form of and - && if [ $age –ge 13 ] && [ $age –le 19 ] then … fi if [ $car = “Chevy” ] && [ $color = “red” ] then Linux-1
Binary or Operator -o Binary or Operator expr1 –o expr2 Returns TRUE if expr1 OR expr2 is TRUE Linux-1
or operator examples if [ $response = “Y” –o $response = “y” ] if [ $age –lt 3 –o $age –gt 65 ] Linux-1