Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shell 编程 陈伟.

Similar presentations


Presentation on theme: "Shell 编程 陈伟."— Presentation transcript:

1 Shell 编程 陈伟

2 Index The shell of Linux Bourne Shell Programming
find.and.regular.expression text manipulation Shell 编程

3 The shell of Linux Bourne shell (sh), C shell (csh), Korn shell (ksh),
TC shell (tcsh), Bourne Again shell (bash) Shell 编程

4 Difference between programming and scripting languages
Programming languages are generally a lot more powerful and a lot faster than scripting languages. Programming languages generally start from source code and are compiled into an executable. This executable is not easily ported into different operating systems. A scripting language also starts from source code, but is not compiled into an executable. Rather, an interpreter reads the instructions in the source file and executes each instruction. Interpreted programs are generally slower than compiled programs. The main advantage is that you can easily port the source file to any operating system. Shell 编程

5 The first bash program We must know how to use a text editor. There are two major text editors in Linux: vi, emacs (or xemacs). So fire up a text editor; for example: $ vi & and type the following inside it: #!/bin/bash echo “Hello World” The first line tells Linux to use the bash interpreter to run this script. We call it hello.sh. Then, make the script executable: $ chmod 700 hello.sh $ ls –l -rwx hello.sh Shell 编程

6 The first bash program To execute the program: $ hello.sh
-bash: hello.sh: command not found The home directory (where the command hello.sh is located) is not in the variable PATH echo $PATH :bin:/usr/bin:… We must specify the path of hello.sh $/home/srinaldi/Scripts/hello.sh $./hello.sh Shell 编程

7 The second bash program
We write a program that copies all files into a directory, and then deletes the directory along with its contents. This can be done with the following commands: $ mkdir trash $ cp * trash $ rm -rf trash $ mkdir trash Instead of having to type all that interactively on the shell, write a shell program instead: $ cat trash #!/bin/bash # this script deletes some files cp * trash rm -rf trash mkdir trash echo “Deleted all files!” Shell 编程

8 Variables We can use variables as in any programming languages. Their values are always stored as strings, but there are mathematical operators in the shell language that will convert variables to numbers for calculations. We have no need to declare a variable, just assigning a value to its reference will create it. Example #!/bin/bash STR=“Hello World!” echo $STR Line 2 creates a variable called STR and assigns the string "Hello World!" to it. Then the value of this variable is retrieved by putting the '$' in at the beginning. Shell 编程

9 Warning ! The shell programming language does not type-cast its variables. This means that a variable can hold number data or character data. count=0 count=Sunday Switching the TYPE of a variable can lead to confusion for the writer of the script or someone trying to modify it, so it is recommended to use a variable for only a single TYPE of data in a script. \ is the bash escape character and it preserves the literal value of the next character that follows. $ ls \* ls: *: No such file or directory Shell 编程

10 Single and Double Quote
When assigning character data containing spaces or special characters, the data must be enclosed in either single or double quotes. Using double quotes (partial quoting) to show a string of characters will allow any variables in the quotes to be resolved $ var=“test string” $ newvar=“Value of var is $var” $ echo $newvar Value of var is test string Using single quotes (full quoting) to show a string of characters will not allow variable resolution $ var=’test string’ $ newvar=’Value of var is $var’ Value of var is $var Shell 编程

11 Examples mistake Not resolved $ pippo= pluto $ pippo =pluto $ ls [Pp]*
$ var=“’(]\\{}\$\”” $ echo $var $ echo “$var” $ echo ‘$var’ $ echo \z # z $ echo \\z # \z $ echo ‘\\z’ # \\z mistake Not resolved Shell 编程

12 Examples $ pippo=cat $ echo “comando = \” $pippo \” “ comando =“ cat ”
Shell 编程

13 The export command The export command puts a variable into the environment so it will be accessible to child processes. For instance: $ x=hello $ bash # Run a child shell. $ echo $x # Nothing in x. $ exit # Return to parent. $ export x $ bash $ echo $x hello # It's there. If the child modifies x, it will not modify the parent’s original value. Verify this by changing x in the following way: $ x=ciao $ exit hello Shell 编程

14 Environmental Variables
There are two types of variables: Local variables Environmental variables Environmental variables are set by the system and can usually be found by using the env command. Environmental variables hold special values. For instance, $ echo $SHELL /bin/bash $ echo $PATH /usr/X11R6/bin:/usr/local/bin:/bin:/usr/bin Environmental variables are defined in /etc/profile, /etc/profile.d/ and ~/.bash_profile. These files are the initialization files and they are read when bash shell is invoked. When a login shell exits, bash reads ~/.bash_logout Shell 编程

15 Environmental Variables
HOME: The default argument (home directory) for cd. PATH: The search path for commands. It is a colon-separated list of directories that are searched when you type a command. Usually, we type in the commands in the following way: $ ./trash.sh By setting PATH=$PATH:. our working directory is included in the search path for commands, and we simply type: $ trash.sh Shell 编程

16 Environmental Variables
LOGNAME: contains the user name HOSTNAME: contains the computer name. MACHTYPE: system harware PS1: sequence of characters shown before the prompt \t hour \d date \w current directory \W last part of the current directory \u user name \$ prompt character Example rinaldi]$ PS1=‘ciao \u *’ ciao rinaldi* _ UID: contains the id number of the user (cannot be changed). SHLVL: contains the shell level Shell 编程

17 Exit The exit command may be used to terminate a script. It can also return a value, which is available to the script’s parent process. When a script ends with exit nnn nnn=0-255 the exit status is nnn Shell 编程

18 Read command The read command allows you to prompt for input and store it in a variable. Example (read.sh) #!/bin/bash echo -n “Enter name of file to delete: ” read file echo “Type 'y' to remove it, 'n' to change your mind ... ” rm -i $file echo "That was YOUR decision!" Line 3 creates a variable called file and assigns the input from keyboard to it. Then the value of this variable is retrieved by putting the '$' in at its beginning. Shell 编程

19 Read command Options Example read –s (does not echo input)
read –nN (accepts only N characters of input) read –p “message” (prompts message) read –tT (accepts input for T seconds) Example $ read –s –n1 -p “Yes (Y) or not (N)?” answer Yes (Y) or not (N) ? Y $ echo $answer Y Shell 编程

20 Command Substitution The backquote “`” is different from the single quote “´”. It is used for command substitution: `command` $ LIST=`ls` $ echo $LIST hello.sh read.sh PS1=“`pwd`>” /home/rinaldi/didattica/> We can perform the command substitution by means of $(command) $ LIST=$(ls) rm $( find / -name “*.tmp” ) ls $( pwd ) ls $( echo /bin ) Shell 编程

21 Example $ a=`echo Hello` $ echo $a $ echo ‘$a’ $ b=`ls /home` $ echo $b $ echo $a $b $ echo “$a $b” Shell 编程

22 Arithmetic Operators + plus - minus * multiplication / division
** exponentiation % modulo Example $ a=(5+2)*3 $ echo $a $ b=2**3 $ echo $a+$b Shell 编程

23 Arithmetic Evaluation
The let statement can be used to do mathematical functions: $ let X=10+2*7 $ echo $X 24 $ let Y=X+2*4 $ echo $Y 32 An arithmetic expression can be evaluated by $[expression] or $((expression)) $ echo $((123+20)) 143 $ VALORE=$[123+20] $ echo $[123*$VALORE] 1430 $ echo $[2**3] $ echo $[8%3] Not necessary to use $X to refer to the value of X Shell 编程

24 Arithmetic Evaluation
Example (operations.sh) #!/bin/bash echo -n “Enter the first number: ”; read x echo -n “Enter the second number: ”; read y add=$(($x + $y)) sub=$(($x - $y)) mul=$(($x * $y)) div=$(($x / $y)) mod=$(($x % $y)) # print out the answers: echo “Sum: $add” echo “Difference: $sub” echo “Product: $mul” echo “Quotient: $div” echo “Remainder: $mod” Shell 编程

25 Conditional Statements
Conditionals let we decide whether to perform an action or not, this decision is taken by evaluating an expression. The most basic form is: if [expression]; then statements elif [expression]; else fi the elif (else if) and else sections are optional Shell 编程

26 Expressions An expression can be: String comparison, Numeric comparison, File operators and Logical operators and it is represented by [expression]: String Comparisons: = compare if two strings are equal != compare if two strings are not equal -n evaluate if string length is greater than zero -z evaluate if string length is equal to zero Examples: [ s1 = s2 ] (true if s1 same as s2, else false) [ s1 != s2 ] (true if s1 not same as s2, else false) [ s1 ] (true if s1 is not empty, else false) [ -n s1 ] (true if s1 has a length greater then 0, else false) [ -z s2 ] (true if s2 has a length of 0, otherwise false) Shell 编程

27 Expressions Number Comparisons: Examples:
-eq compare if two numbers are equal -ge compare if one number is greater than or equal to a number -le compare if one number is less than or equal to a number -ne compare if two numbers are not equal -gt compare if one number is greater than another number -lt compare if one number is less than another number Examples: [ n1 -eq n2 ] (true if n1 same as n2, else false) [ n1 -ge n2 ] (true if n1greater then or equal to n2, else false) [ n1 -le n2 ] (true if n1 less then or equal to n2, else false) [ n1 -ne n2 ] (true if n1 is not same as n2, else false) [ n1 -gt n2 ] (true if n1 greater then n2, else false) [ n1 -lt n2 ] (true if n1 less then n2, else false) Shell 编程

28 echo -n “Enter your login name: " read name if [ “$name” = “$USER” ];
#!/bin/bash # if0.sh echo -n “Enter your login name: " read name if [ “$name” = “$USER” ]; then echo “Hello, $name. How are you today ?” else echo “You are not $USER, so who are you ?” fi Shell 编程

29 #!/bin/bash # if1.sh echo -n “Enter a number 1 < x < 10: " read num if [ “$num” -lt 10 ]; then if [ “$num” -gt 1 ]; then echo “$num*$num=$(($num*$num))” else echo “Wrong insertion !” fi

30 Expressions Files operators: Examples:
-d check if path given is a directory -f check if path given is a file -s check if path given is a symbolic link -e check if file name exists -s check if a file has a length greater than 0 -r check if read permission is set for file or directory -w check if write permission is set for a file or directory -x check if execute permission is set for a file or directory Examples: [ -d fname ] (true if fname is a directory, otherwise false) [ -f fname ] (true if fname is a file, otherwise false) [ -e fname ] (true if fname exists, otherwise false) [ -s fname ] (true if fname length is greater then 0, else false) [ -r fname ] (true if fname has the read permission, else false) [ -w fname ] (true if fname has the write permission, else false) [ -x fname ] (true if fname has the execute permission, else false) Shell 编程

31 Example #!/bin/bash if [ -f /etc/fstab ]; then cp /etc/fstab . echo “Done.” else echo “This file does not exist.” exit 1 fi Exercise. Write a shell script which accepts a file name <pippo>: The script checks if file exists, and creates a directory Backup If file exists, copies the file to the same name + .bak (if the .bak file already exists ask if you want to replace it). If the file does not exist then exits with the message “The file <pippo> does not exist!!!” Shell 编程

32 Solution #!/bin/bash if [ ! –d ./Backup ] then mkdir ./Backup fi read –p “insert the name of a file” pippo if [ -f $pippo ] then cp $pippo ./Backup/$pippo.bak else echo “The file $pippo does not exist!!!” Shell 编程

33 Expressions Logical operators: ! negate (NOT) a logical expression
-a logically AND two logical expressions -o logically OR two logical expressions #!/bin/bash # if3.sh echo -n “Enter a number 1 < x < 10:” read num if [ “$num” -gt 1 –a “$num” -lt 10 ]; then echo “$num*$num=$(($num*$num))” else echo “Wrong insertion !” fi Shell 编程

34 Expressions Logical operators:
&& logically AND two logical expressions || logically OR two logical expressions #!/bin/bash # if4.sh echo -n "Enter a number 1 < x < 10: " read num if [ “$number” -gt 1 ] && [ “$number” -lt 10 ]; then echo “$num*$num=$(($num*$num))” else echo “Wrong insertion !” fi Shell 编程

35 Example 1 $ cat iftrue.sh # iftrue.sh #!/bin/bash
echo “Enter a path: ”; read x if cd $x 2> /dev/null then echo “I am in $x and it contains”; ls else echo “The directory $x does not exist”; exit 1 fi $ ./iftrue.sh Enter a path: /home srinaldi afrosini riccardo … Enter a path: pippo The directory pippo does not exist Shell 编程

36 Shell Parameters Special parameters
Positional parameters are assigned from the shell’s argument when it is invoked. Positional parameter “N” may be referenced as “${N}”, or as “$N” when “N” consists of a single digit. Special parameters $# is the number of parameters passed $0 returns the name of the shell script running as well as its location in the filesystem $* gives a single word containing all the parameters passed to the script gives an array of words containing all the parameters passed to the script $ cat sparameters.sh ( sparameters.sh ) #!/bin/bash echo “$#; $0; $1; $2; $*; $ sparameters.sh alba chiara 2; ./sparameters.sh; alba; chiara; alba chiara; alba chiara Shell 编程

37 Trash $ cat trash.sh ( trash.sh ) #!/bin/bash if [ $# -eq 1 ]; then
if [ ! –d “$HOME/trash” ]; mkdir “$HOME/trash” fi mv $1 “$HOME/trash” else echo “Use: $0 filename” exit 1 Shell 编程

38 Case Statement Used to execute statements based on specific values. Often used in place of an if statement if there are a large number of conditions. Value used can be an expression each set of statements must be ended by a pair of semicolons; a *) is used to accept any value not matched with list of values case $var in val1) statements;; val2) *) esac Shell 编程

39 Example #!/bin/bash ( case.sh )
echo -n “Enter a number 1 < x < 10: ” read x case $x in 1) echo “Value of x is 1.”;; 2) echo “Value of x is 2.”;; 3) echo “Value of x is 3.”;; 4) echo “Value of x is 4.”;; 5) echo “Value of x is 5.”;; 6) echo “Value of x is 6.”;; 7) echo “Value of x is 7.”;; 8) echo “Value of x is 8.”;; 9) echo “Value of x is 9.”;; 0 | 10) echo “wrong number.”;; *) echo “Unrecognized value.”;; esac Shell 编程

40 Iteration Statements The for structure is used when you are looping through a range of variables. for var in list do statements done statements are executed with var set to each value in the list. #!/bin/bash let sum=0 for num in let “sum = $sum + $num” echo $sum Shell 编程

41 Iteration Statements: <list>
#!/bin/bash for x in paper pencil pen; do echo “The value of variable x is: $x” sleep 1 done # The value of variable x is paper # The value of variable x is pencil # The value of variable x is pen #!/bin/bash for x in “paper A4” “pencil STADTLER” “pen BIC”; do # The value of variable x is paper A4 # The value of variable x is pencil STADTLER # The value of variable x is pen BIC Shell 编程

42 Iteration Statements: <list>
#!/bin/bash lista=“antonio michele paolo luca” for x in $lista do echo “The value of variable x is: $x” sleep 1 done # The value of variable x is antonio # The value of variable x is michele # The value of variable x is paolo # The value of variable x is luca Shell 编程

43 Iteration Statements: <list>
#!/bin/bash for x in * do ls -l “$x” sleep 1 done # Lists all files in current directory for x in /bin # Lists all files in /bin Shell 编程

44 Iteration Statements: <list>
#!/bin/bash read –p “Insert the name of a directory” directory echo "symbolic links in directory \“ $directory \“ " for file in $( find $directory -type l ) # -type l = symbolic links do echo "$file" done | sort # Otherwise file list is unsorted Shell 编程

45 Iteration Statements: <list>
if the list part is left off, var is set to each parameter passed to the script ( $1, $2, $3,…) $ cat for1.sh ( for1.sh ) #!/bin/bash for x do echo “The value of variable x is: $x” sleep 1 done $ for1.sh alba chiara The value of variable x is: alba The value of variable x is: chiara Shell 编程

46 Example 1 #!/bin/bash ( old.sh ) # Move the command line arg files to old directory. if [ $# -eq 0 ] #check for command line arguments then echo “Usage: $0 file …” exit 1 fi if [ ! –d “$HOME/old” ] mkdir “$HOME/old” echo The following files will be saved in the old directory: echo $* for p in $* #loop through all command line arguments do mv $p “$HOME/old/” chmod 400 “$HOME/old/$p” done ls -l “$HOME/old” Shell 编程

47 Example 2 #!/bin/bash ( args.sh ) # Invoke this script with several arguments: “one two three“ if [ ! -n “$1” ]; then echo “Usage: $0 arg1 arg2 ..." ; exit 1 fi echo ; index=1 ; echo “Listing args with \”\$*\”:” for arg in “$*” ; do echo “Arg $index = $arg” let “index+=1” # increase variable index by one done echo “Entire arg list seen as single word.” echo “Listing args with for arg in ; do let “index+=1” echo “Arg list seen as separate words.” ; exit 0 Shell 编程

48 Operations on vabiables
……. let “index += 5” #increment index by 5 …… += #increment variable -= # decrement variable *= # multiply variable /= # divide variable Shell 编程

49 Using Arrays with Loops
In the bash shell, we may use arrays. The simplest way to create one is using one of the two subscripts: pet[0]=dog pet[1]=cat pet[2]=fish pet[4]=apple pet=( dog cat fish apple ) We may have up to 1024 elements. To extract a value, type ${arrayname[i]} $ echo ${pet[0]} dog $ echo ${pet[2]} fish Shell 编程

50 Arrays To extract all the elements, use an asterisk as:
echo ${arraynames[*]} To see how many elements are in the array: echo ${#arraynames[*]} We can combine arrays with loops using a for loop: for x in ${arrayname[*]} do echo ${arrayname[$x]} done Shell 编程

51 A C-like for loop An alternative form of the for structure is
for (( EXPR1 ; EXPR2 ; EXPR3 )) do statements done First, the arithmetic expression EXPR1 is evaluated. EXPR2 is then evaluated repeatedly until it evaluates to 0. Each time EXPR2 is evaluates to a non-zero value, statements are executed and EXPR3 is evaluated. $ cat for2.sh #!/bin/bash echo –n “Enter a number: ”; read x let sum=0 for (( i=1 ; $i<$x ; i=$i+1 )) ; do let “sum = $sum + $i” echo “the sum of the first $x numbers is: $sum” Shell 编程

52 Debugging Bash provides two options which will give useful information for debugging -v : displays each line of the script as typed before execution -x : displays each line before execution (abbreviated) Usage: #!/bin/bash –v, or #!/bin/bash –x $ cat for3.sh #!/bin/bash –x echo –n “Enter a number: ”; read x let sum=0 for (( i=1 ; $i<$x ; i=$i+1 )) ; do let “sum = $sum + $i” done echo “the sum of the first $x numbers is: $sum” Shell 编程

53 Debugging $ for3.sh + echo –n ‘Enter a number: ’ Enter a number: + read x 3 + let sum=0 + (( i=0 )) + (( 0<=3 )) + let ‘sum = 0 + 0’ + (( i=0+1 )) + (( 1<=3 )) + let ‘sum = 0 + 1’ + (( i=1+1 )) + (( 2<=3 )) + let ‘sum = 1 + 2’ + (( i=2+1 )) + (( 3<=3 )) + let ‘sum = 3 + 3’ + (( i=3+1 )) + (( 4<=3 )) + echo ‘the sum of the first 3 numbers is: 6’ the sum of the first 3 numbers is: 6 Shell 编程

54 Where is the error? A buggy script #!/bin/bash -x # ex74.sh a=37
if [$a -gt 27 ] then echo $a fi exit Output from script: +a=37 +’[37’ -gt 37 ‘]’ + ./ex74.sh: [37: command not found …. Shell 编程

55 While Statements The while structure is a looping structure. Used to execute a set of commands while a specified condition is true. The loop terminates as soon as the condition becomes false. If condition never becomes false, loop will never exit. while expression do statements done $ cat while.sh ( while.sh ) #!/bin/bash echo –n “Enter a number: ”; read x let sum=0; let i=1 while [ $i –le $x ]; do let “sum = $sum + $i” i=$i+1 echo “the sum of the first $x numbers is: $sum” Shell 编程

56 Menu #!/bin/bash # menu.sh clear ; loop=y while [ “$loop” = y ] ; do
echo “Menu”; echo “====” echo “D: print the date” echo “W: print the users who are currently log on.” echo “P: print the working directory” echo “Q: quit.” echo read –s choice case $choice in D | d) date ;; W | w) who ;; P | p) pwd ;; Q | q) loop=n ;; *) echo “Illegal choice.” ;; esac done Shell 编程

57 Find a Pattern and Edit $ cat grep_edit.sh #!/bin/bash # grep_edit.sh # Edit argument files $2 ..., that contain pattern $1 if [ $# -le 1 ] then echo “Usage: $0 pattern file …” ; exit 1 else pattern=$1 # Save original $1 shift # shift the positional parameter to the left by 1 while [ $# -gt 0 ] # New $1 is first filename do grep “$pattern” $1 > /dev/null if [ $? -eq 0 ] ; then # If grep found pattern vi $1 # then vi the file fi shift done $ grep_edit.sh while ~ Shell 编程

58 Continue Statements The continue command causes a jump to the next iteration of the loop, skipping all the remaining commands in that particular loop cycle. #!/bin/bash LIMIT=19 echo echo “Printing Numbers 1 through 20 (but not 3 and 11)” a=0 while [ $a -le “$LIMIT” ]; do a=$(($a+1)) if [ “$a” -eq 3 ] || [ “$a” -eq 11 ] then continue fi echo -n “$a ” done Shell 编程

59 Break Statements The break command terminates the loop (breaks out of it). #!/bin/bash LIMIT=19 echo “Printing Numbers 1 through 20, but something happens after 2 … ” a=0 while [ $a -le “$LIMIT” ]; do a=$(($a+1)) if [ “$a” -gt 2 ] then break fi echo -n “$a ” done echo; echo; echo exit 0 Shell 编程

60 Until Statements The until structure is very similar to the while structure. The until structure loops until the condition is true. So basically it is “until this condition is true, do this”. until [expression] do statements done $ cat countdown.sh #!/bin/bash #countdown.sh #echo “Enter a number: ”; read x echo ; echo Count Down until [ “$x” -le 0 ]; do echo $x x=$(($x –1)) sleep 1 echo ; echo GO ! Shell 编程

61 Manipulating Strings Bash supports a surprising number of string manipulation operations. Unfortunately, these tools lack a unified focus. ${#string} gives the string length ${string:position} extracts sub-string from $string at $position ${string:position:length} Extracts $length characters of sub-string from $string at $position Example $ st= $ echo ${#st} 10 $ echo ${st:6} 6789 $ echo ${st:6:2} 67 Shell 编程

62 Parameter Substitution
Manipulating and/or expanding variables ${parameter-default}, If parameter not set, use default. $ echo ${username-`whoami`} rinaldi $ username=simone simone ${parameter=default}, If parameter not set, set it to default. $ echo ${username=`whoami`} $ echo $username ${parameter+value}, If parameter set, use value, else use null string. $ echo ${username+andrea} andrea $ echo ${pippo+andrea} # null string Shell 编程

63 Parameter Substitution
${parameter?msg}, If parameter set, use it, else print msg $ value=${total?’total is not set’} total: total is not set $ total=10 $ echo $value 10 Example: #!/bin/bash OUTFILE=symlinks.list directory=${1-`pwd`} for file in “$( find $directory -type l )” do echo “$file” done | sort >> “$HOME/$OUTFILE” exit 0 Shell 编程

64 Advanced operations on strings
${string#substring}, strips the shortest match of substring from the front of string. pippo=abbcaabccbcabcdbcdaba echo ${pippo#a*c} # aabccbcabcdbcdaba echo ${pippo##a*c} # strips the longest match # daba ${string%substring}, strips the shortest match of substring from the front of string. Shell 编程

65 Advanced operations on strings
${string/substring/replacement},strips the first match of substring in string with replacement. pippo=abbcaabccbcabcdbcdabab echo ${pippo/ca/11} # abb11abccbcabcdbcdabab echo ${pippo//ca/11} # abb11abccb11bcdbcdabab # replaces all matches echo ${pippo/[ab]?c/000} # a000aabccbcabcdbcdabab echo ${pippo/c*a/\!} # abbc!b echo ${pippo//b?/00} # a00caa00c00a00d00da00b Shell 编程

66 Functions Functions make scripts easier to maintain. Basically it breaks up the program into smaller pieces. A function performs an action defined by you, and it can return a value if you wish. #!/bin/bash hello() { echo “You are in function hello()” } echo “Calling function hello()…” hello echo “You are now out of function hello()” In the above, we called the hello() function by name by using the line: hello . When this line is executed, bash searches the script for the line hello(). It finds it right at the top, and executes its contents. Shell 编程

67 Functions #!/bin/bash function check() { if [ -e "/home/$1" ] then return 0 else return 1 fi } echo “Enter the name of the file: ” ; read x if check $x echo “$x exists !” echo “$x does not exists !” fi. Shell 编程

68 Greatest Common Divisor
#!/bin/bash # gcd.sh: greatest common divisor # Uses Euclid's algorithm # The "greatest common divisor" (gcd) of two integers is the largest integer # that will divide both, leaving no remainder. # Euclid's algorithm uses successive division. # In each pass, dividend <--- divisor, divisor <--- remainder # until remainder = 0. The gcd = dividend, on the final pass. ARGS=2 E_BADARGS=65 if [ $# -ne "$ARGS" ] then echo "Usage: `basename $0` first-number second-number" exit $E_BADARGS fi Shell 编程

69 Greatest common divisor
gcd () { dividend=$1 divisor=$2 remainder=1 until [ "$remainder" -eq 0 ] do let "remainder = $dividend % $divisor" dividend=$divisor divisor=$remainder done } # Last $dividend is the gcd. gcd $1 $2 echo; echo "GCD of $1 and $2 = $dividend“ exit 0 Shell 编程

70 Script 1: Picking a random card from a deck
#!/bin/bash # Count how many elements. Suites=“Clubs Diamonds Hearts Spades” Denominations=“ Jack Queen King Ace” # Read into array variable. suite=($Suites) denomination=($Denominations) num_suites=${#suite[*]} num_denominations=${#denomination[*]} echo -n "${denomination[$((RANDOM%num_denominations))]} of " echo ${suite[$((RANDOM%num_suites))]} exit 0 Shell 编程

71 Script 2: Changes all filenames to lowercase
#!/bin/bash for filename in * # Traverse all files in directory. do fname=`basename $filename` # Change name to lowercase. n=`echo $fname | tr A-Z a-z` if [ “$fname” != “$n” ] # Rename only files not already lowercase. then mv $fname $n fi done exit 0 Shell 编程

72 Script 3: Compare two files with a script
#!/bin/bash ARGS=2 # Two args to script expected. if [ $# -ne “$ARGS” ]; then echo “Usage: `basename $0` file1 file2” ; exit 1 fi if [[ ! -r "$1" || ! -r "$2" ]] ; then echo “Both files must exist and be readable.” ; exit 2 cmp $1 $2 &> /dev/null # /dev/null buries the output of the “cmp” command. # Also works with 'diff', i.e., diff $1 $2 &> /dev/null if [ $? -eq 0 ] # Test exit status of “cmp” command. then echo “File \“$1\” is identical to file \“$2\”.” else echo “File \“$1\“ differs from file \“$2\”.” exit 0 Shell 编程

73 Exercise #!/bin/bash MAX=10000 for((nr=1; nr<$MAX; nr++)) do let "t1 = nr % 5" if [ "$t1" -ne 3 ] then continue fi let "t2 = nr % 7" if [ "$t2" -ne 4 ] let "t3 = nr % 9" if [ "$t3" -ne 5 ] break # What happens when you comment out this line? Why? done echo "Number = $nr" exit 0 Shell 编程

74 Bourne Shell Programming

75 Bourne Shell Programming
Certainly the most popular shell is “bash”. Bash is the shell that will appear in the GNU operating system. Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). bash is not only an excellent command line shell, but a scripting language in itself. Shell scripting allows us to use the shell's abilities and to automate a lot of tasks that would otherwise require a lot of commands. Shell 编程

76 Borne Shell Background
Early Unix shell that was written by Steve Bourne of AT&T Bell Lab. Basic shell provided with many commercial versions of UNIX Many system shell scripts are written to run under Bourne Shell A long and successful history Shell 编程

77 Bourne Shell Programming
Control structures if … then for … in while until case break and continue Shell 编程

78 if … then Structure if test-command then commands fi Example:
if test “$word1” = “$word2” echo “Match” Shell 编程

79 test Command test is a built-in command Syntax Argument
test expression [ expression ] The test command evaluate an expression Returns a condition code indicating that the expression is either true (0) or false (not 0) Argument Expression contains one or more criteria Logical AND operator to separate two criteria: -a Logical OR operator to separate two criteria: -o Negate any criterion: ! Group criteria with parentheses Separate each element with a SPACE Shell 编程

80 Test Criteria Test Operator for integers: int1 relop int2 Relop
Description -gt Greater than -ge Greater than or equal to -eq Equal to -ne Not euqal to -le Less than or equal to -lt Less than Shell 编程

81 Exercise Create a shell script to check there is at least one parameter Something like this: if test $# -eq 0 then echo “ you must supply at least one arguments” exit 1 fi Shell 编程

82 Test Criteria The test built-in options for files Option
Test Performed on file -d filename Exists and is a directory file -f filename Exists and is a regular file -r filename Exists and it readable -s filename Exists and has a length greater than 0 -u filename Exists and has setuid bit set -w filename Exists and it writable -x filename Exists and it is executable … … Shell 编程

83 Exercise Check weather or not the parameter is a non-zero readable file name Continue with the previous script and add something like if [ -r “$filename” –a –s “$filename” ] then … … fi Shell 编程

84 Test Criteria String testing Criteria meaning String
True if string is not the null string -n string True if string has a length greater than zero -z string True if string has a length of zero String1 = string2 True if string1 is equal to string2 String1 != string2 True if string1 is not equal to string2 Shell 编程

85 Exercise Check users confirmation Frist, read user input
echo -n “Please confirm: [Yes | No] “ read user_input Then, compare it with standard answer ‘yes’ if [ “$user_input” = Yes ] then echo “Thanks for your confirmation!” Fi Shell 编程

86 if…then…else Structure
if test-command then commands else fi You can use semicolon (;) ends a command the same way a NEWLINE does. if [ … ]; then … … if [ 5 = 5 ]; then echo "equal"; fi Shell 编程

87 if…then…elif Structure if test-command then commands elif test-command
. else fi Shell 编程

88 Debugging Shell Scripts
Display each command before it runs the command Set the –x option for the current shell $set –x Use the –x to invoke the script $sh –x command arguments Add the set command at the top of the script set –x Then each command that the script executes is preceded by a plus sign (+) Distinguish the output of trace from any output that the script produces Turn off the debug with set +x Shell 编程

89 for… in Structure for loop-index in argument_list do commands done
Example: for file in * if [ -d “$file” ]; then echo $file fi Shell 编程

90 for Structure for loop-index do commands done Automatically takes on the value of each of command line arguments, one at a time. Which implies for arg in Shell 编程

91 while Structure while test_command do commands done Example:
while [ “$number” –lt 10 ] … … number=`expr $number + 1` Shell 编程

92 until Structure until test_command do commands done Example:
secretname=jenny name=noname until [ “$name” = “$secretname” ] echo “ Your guess: \c” read name Shell 编程

93 break and continue Interrupt for, while or until loop
The break statement transfer control to the statement AFTER the done statement terminate execution of the loop The continue statement Transfer control to the statement TO the done statement Skip the test statements for the current iteration Continues execution of the loop Shell 编程

94 Example: for index in do if [ $index –le 3 ]; then echo continue continue fi echo $index if [ $index –ge 8 ]; then echo “break” break done Shell 编程

95 case Structure default case: catch all pattern * ) case test_string in
commands_1 ;; pattern-2 ) commands_2 … … esac default case: catch all pattern * ) Shell 编程

96 case Special characters used in patterns Pattern Matches *
Matches any string of characters. ? Matches any single character. […] Defines a character class. A hyphen specifies a range of characters | Separates alternative choices that satisfy a particular branch of the case structure Shell 编程

97 Example #!/bin/sh echo “\n Command MENU\n” echo “ a. Current data and time” echo “ b. Users currently logged in” echo “ c. Name of the working directory\n” echo “Enter a,b, or c: \c” read answer echo case “$answer” in a) date ;; b) who c) pwd *) echo “There is no selection: $answer” esac Shell 编程

98 echo and read The backslash quoted characters in echo
\c suppress the new line \n new line \r return \t tab Read read variable1 [variable2 …] Read one line of standard input Assign each word to the corresponding variable, with the leftover words assigned to last variables If only one variable is specified, the entire line will be assigned to that variable. Shell 编程

99 Example: bundle #!/bin/sh
#bundle: group files into distribution package echo "# To Uble, sh this file" for i do echo "echo $i" echo "cat >$i <<'END of $i' " cat $i echo "END of $i" done Will this program work for sure? Shell 编程

100 Built-in: exec Execute a command: Syntax: exec command argument
Run a command without creating a new process Quick start Run a command in the environment of the original process Exec does not return control to the original program Exec can be the used only with the last command that you want to run in a script Example, run the following command in your current shell, what will happen? $exec who Shell 编程

101 Built-in: exec Redirect standard output, input or error of a shell script from within the script exec < infile exec > outfile 2> errfile Example: sh-2.05b$ more redirect.sh exec > /dev/tty echo "this is a test of redirection" sh-2.05b$ ./redirect.sh 1 > /dev/null 2 >& 1 this is a test of redirection Shell 编程

102 Catch a signal: builtin trap
Syntax: trap ‘commands’ signal-numbers Shell executes the commands when it catches one of the signals Then resumes executing the script where it left off. Just capture the signal, not doing anything with it trap ‘ ‘ signal_number Often used to clean up temp files Signals SIGHUP 1 disconnect line SIGINT 2 control-c SIGKILL 9 kill with -9 SIGTERM 15 default kill SIGSTP 24 control-z Shell 编程

103 Example [ruihong@dafinn ~/cs3451]$ more inter #!/bin/sh
trap 'echo PROGRAM INTERRUPTED' 2 while true do echo "programming running." sleep 1 done Shell 编程

104 A partial list of built-in
bg, fg, jobs job control break, continue change the loop cd, pwd working directory echo, read display/read eval scan and evaluate the command exec execute a program exit exit from current shell export, unset export/ remove a val or fun test compare arguments Shell 编程

105 A partial list of builtin
kill sends a signal to a process or job set sets flag or argument shift promotes each command line argument times displays total times for the current shell and trap traps a signal type show whether unix command, build-in, function umask file creation mask wait waits for a process to terminate. ulimit print the value of one or more resource limits Shell 编程

106 functions A shell function is similar to a shell script
It stores a series of commands for execution at a later time. The shell stores functions in the memory Shell executes a shell function in the same shell that called it. Where to define In .profile In your script Or in command line Remove a function Use unset built-in Shell 编程

107 functions Syntax function_name() { commands } Example:
sh-2.05b$ whoson() > { > date > echo "users currently logged on" > who > } sh-2.05b$ whoson Tue Feb 1 23:28:44 EST 2005 users currently logged on ruihong : Jan 31 08:46 ruihong pts/ Jan 31 08:54 (:0.0) ruihong pts/ Jan 31 09:02 (:0.0) Shell 编程

108 Example sh-2.05b$ more .profile setenv() { if [ $# -eq 2 ] then
eval $1=$2 export $1 else echo "usage: setenv NAME VALUE" 1>&2 fi } sh-2.05b$. .profile sh-2.05b$ setenv T_LIBRARY /usr/local/t sh-2.05b$ echo $T_LIBRARY /usr/local/t Shell 编程

109 Exercise Let’s look at some system scripts /etc/init.d/syslog
/etc/init.d/crond Shell 编程

110 Summary Shell is a programming language
Programs written in this language are called shell scripts. Variable Built-in Control structure Function Call utilities outside of shell find, grep, awk Shell 编程


Download ppt "Shell 编程 陈伟."

Similar presentations


Ads by Google