Top Banner
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.
23

Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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.

Page 2: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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.

Page 3: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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.

Page 4: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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.

Page 5: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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.

Page 6: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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)

Page 7: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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]

Page 8: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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.

Page 9: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

Conditions and Tests• The if-then construct

• The if-then-else Construct

• The if-then-elif Construct

Page 10: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

Structure of the if-then construct

• if [condition]

• then

• commands

• …

• …

• last – command

• fi

Page 11: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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.

Page 12: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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

Page 13: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

Control Structures

• case.. in ..esac

• case expression in

• pattern { |pattern })

• list

• ::

• esac

Page 14: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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)

Page 15: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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)

Page 16: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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)

Page 17: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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

• -o ---- Logical OR

• ! ---- Logical NOT

Page 18: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

Test command numeric test operators

• -eq --- number1 –eq number2

• Is number1 equal number2?

• -ne ---

• -gt ---

• -ge ---

• -lt ---

• -le ---

Page 19: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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

Page 20: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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)

Page 21: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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?

Page 22: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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]

Page 23: Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.

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

• $-