shell programming 1. understanding unix shell programming language: a. it has features of high-level...

23
Shell Programming • 1. Understanding Unix shell programming language: • A. It has features of high-level languages. • B. Convenient to do the programming. • C. It is a built-in programming language. • D. The price we have to pay --- speed.

Post on 22-Dec-2015

253 views

Category:

Documents


0 download

TRANSCRIPT

Shell Programming• 1. Understanding Unix shell programming

language:

• A. It has features of high-level languages.

• B. Convenient to do the programming.

• C. It is a built-in programming language.

• D. The price we have to pay --- speed.

Shell Script

• The shell program files are called shell-procedures, shell script or simply script.

• Example:

• $ cat won

• # won

• # display # of users currently logged in

• #

• who | wc –l (end of the script)

• #---remark or comment in the Unix shells.

More about echo Command

• The echo command special characters:• \b ---- A backspace• \c ---- Inhibit the default next line (return • key) at the end of the output string.• \n ---- A carriage return and a form feed.• \r ---- A carriage return without a linefeed.• \bt ---- A tab character.• \0n ---- A zero followed by 1-, 2-, or 3-digits

octal number representing the ASCII code of a character.

Examples• $ cat BOX1

• echo “The following is the output of $0 script

• echo “Total number of command line arguments: $# “

• echo “The first parameter : $1 “

• echo “ The second parameter : $2 “

• echo “ This is the list of all the parameters : $* “

• $-

• We run the script first by typing BOX1 [Enter]

• Then we run the script by typing BOX1 is empty [Enter] and pay attention to the differences.

The command line parameters

• The shell can read up to 10 command line parameters (also called arguments) from the command line into the special variables (also called positional variables, or parameters).

• The special variables are numbered in sequence from 0 to 9 (no number 10)

• i.e., $0, $1, $2, … $9.

The shell positional variables• $0 --- Contains the name of the script, as typed on the

command line

• $1, $2, … $9 --- contains the first through ninth command line parameters.

• $# --- Contains the number of command line parameters.

• $@ --- Contains all command line parameters: $1 $2 ….. $9.

• $? --- Contains the exit status of the last command.

• $* --- Contains all command line parameters: $1 $2… $9

• $$ --- Contain the PID number of the executing process. (week11 BOX1 , BOX2)

Assigning Values• 1. $ set One Two Three [Enter]

• $echo $1 $2 $3 [Enter]

• 2. $set `date` [Enter]

• $echo $1 $2 $3 [Enter]

• 3. $echo $1 $2 $3 $4 $5 $6 [Enter]

Terminating Programs: The exit• Exit will end the program and when a

number is included, the exit code will be the number you assigned. Otherwise, a 0 exit code will reflect a successful exit and a non zero will mean a failed script or command.

• Use echo $? To display the last exit code.

Conditions and Tests• The if-then construct

• The if-then-else Construct

• The if-then-elif Construct

Structure of the if-then construct

• if [condition]

• then

• commands

• …

• …

• last – command

• fi

Rules of Conditions and Tests

• 1. The if statement ends with the reserved word fi (if typed backward).

• 2. The indentation is not necessary, but it certainly makes the code more readable.

• 3. If the condition is true, then all the commands between the then and fi, what is called the body of the if, are executed. If the condition is false, the body of the if is skipped, and the line after fi is executed.

if – then – else Construct• if [condition]• then• true – commands• …• last – true – command else false – commands … last – false – command fi

Control Structures

• case.. in ..esac

• case expression in

• pattern { |pattern })

• list

• ::

• esac

Example• $ cat simple• # Sample• # A simple script to test the read command• #• echo "Give me a long sentence:\c"• read Word1 Word2 Rest• echo " $Word1 \n $Word2 \n $Rest"• echo " End of my act“• Echo `cal 2007`• (week13 simple)

Example• # The largest of three• #• # This program accepts three numbers and shows the largest of

them• echo "Enter three numbers and I will show you the largest of

them>> "• read num1 num2 num3• if test "$num1" -gt "$num2" -a "$num1" -gt "$num3"• then • echo "The largest number is: $num1"• elif test "$num2" -gt "$num1" -a “$num2” -gt "$num3"• then• echo "The largest number is: $num2"• else• echo "The largest number is: $num3"• fi• exit 0 (week13 Large3)

True or false: The test command• # test command example• echo "Are you okay?"• echo "input Y for yes or N for no:\c"• read answer• if test "$answer" = Y• then • echo "Glad to hear that"• else• echo "Go home or the hospital!"• fi (week13 test1 and test2)

Logical “and” “or” “not” • -a ---- Logical AND

• -o ---- Logical OR

• ! ---- Logical NOT

Test command numeric test operators

• -eq --- number1 –eq number2

• Is number1 equal number2?

• -ne ---

• -gt ---

• -ge ---

• -lt ---

• -le ---

String Values Testing• Test command can also be used in string

coma rations. However, different set of operators will be used.

• $ STRING5 [Enter]• $ test –z “$STRING5” [Enter]• Use echo $? To see the testing result.• 0--- True• Non-zero number --- False

Test Operator for string

• = ---string1 = string2 ; • Does string1 match string2?• != ---- • -n ---- • Does string contain characters(nonzero

length?) • -z ---- Is string an empty string (zero length)

Test file characteristics

• -r ---test -r fileName ;

• Does file exist,and is it readable?

• -w ---

• -s ---

• Does file exist, and a nonzero length?

• -f ---

• Does file exist, but is not a directory file?

• -d ---

• Does file exist, and is it a directory file?

Example

• $FILE=myfile

• $if test –r “$FILE” [Enter]

• > then [Enter]

• > echo “READABLE” [Enter]

• > then test –w “$FILE” [Enter]

• > echo “WRITEABLE” [Enter]

• > else [Enter]

• >Echo “Read and Write Access Denied”

• Fi [Enter]

Examples• $ cat svi

• #

• # svi: save and invoke vi (svi) program.

• # Adding the if statement

• #

• if [ $# = 1 ] # check for the number of command line arguments

• then

• cp $1 $HOME/keep # copy specified file to keep

• fi # end of the if statement

• vi $1 # invoke vi with the specified filename

• exit 0 end of program, exit

• $-