I117 II I117 PROGRAMMING PRACTICE II SCRIPT LANGUAGE 1 Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara yasu@jaist.ac.jp
/ SCHEDULE 1. 2011/06/07(Tue) / Basic of Programming 2. 2011/06/09(Thu) / Fundamental data structures 3. 2011/06/14(Tue) / Memory Management 1 4. 2011/06/16(Thu) / Memory Management 2 5. 2011/06/21(Tue) / Debugging 6. 2011/06/23(Thu) / Software Development Env. 1 7. 2011/06/28(Tue) / Software Development Env. 2 8. 2011/06/30(Thu) / Data Structure : Tree 9. 2011/07/05(Tue) / Data Structure: Hash 10. 2011/07/07(Thu) / Understanding Programs 1 11. 2011/07/12(Tue) / Understanding Programs 2 12. 2011/07/14(Thu) / Script Language 1 13. 2011/07/19(Tue) / Script Language 2 14. 2011/07/21(Thu) / Other Languages 15. 2011/07/26(Tue) / Examination
/ FINAL REPORT free topic : The deadline: 7/26(Tue) Submit the source code, and the report. by e-mail: i117report@jaist.ac.jp by 7/26(Tue) 23:59. / Sound file converter / web crawler (CalDAV) calendar program new shell, editor, window manager rsync encrypt/decrypt, rsync markov-chain program The final report must have deep consideration
/ TODAY'S INDEX sh shell builtins test, expr sed awk autoconf
/ SCRIPT LANGUAGE: SHELL
/ WHAT IS "SHELL"? UNIX / The interface between the users and the system. / specification POSIX 1003.2 X/Open 4.2 / more extended one shell X Window Other programs UNIX kernel
SHELL / KINDS OF SHELLS sh (Bourne) / original shell csh, tcsh, zsh Berkley UNIXBill JoyC JAISTSparctcsh / C shell is created by Bill Joy who made Berkeley Unix. JAIST's default shell is tcsh. ksh, pdksh bash KornDavid Korn / korn shell and it's public version. rc GNULinuxDarwin(MacOS) / shell by GNU Project. cshc / more C-like shell, a shell in the Plan 9 system.
/ VARIABLES; ASSIGNMENT AND PRINT / no need to define variables / assignment and print =$ / assignment by '=', reference by '$' / example / in command line $ age=22 $ echo $age 22 / in shell script $ cat age.sh age=22 echo $age $ sh age.sh 22
/ QUOTE, ESCAPE SEQUENCE (\) / you can use "'" or "\" when you use special character. $ words= hello world $ echo $words hello world $ words=hello\ world $ echo $world hello world $ words=hello world sh: world: command not found :wordshelloworld This assigns "hello" to "words", and execute a command "world" which does not exist (error).
) SINGLE QUOTATION, DOUBLE QUOTATION. $ / "$" is not evaluated in single quotations. $ name=george $ words= hello $name $ echo $words hello $name $ / "$" is evaluated in double quotations. $ name=george $ words= hello $name $ echo $words hello George
READ / ASSIGNMENT BY 'READ' COMMAND read / interactive assignment by 'read'. $ read name george $ echo $name george
/ SCRIPT EXECUTION AND PERMISSIONS. / give files permissions to be executed. chmod +x / do it by 'chmod +x' $ chmod +x args.sh $ cat args.sh #!/bin/sh words= hello world echo $words echo $words echo $words echo \$words echo Enter your name read name echo hello $name $./args.sh hello world hello world $words $words Enter your name George hello George
/ COMMENT OUT # / '#' by comment out # / comment last from '#' to the end of line. 1 / the first line's comment in the script file has a special purpose. / specify the command to execute the file. #!/bin/sh #!/usr/bin/perl #!/usr/local/bin/ruby 1 / specify the command and attach executable permission; the script will become executable.
/ ENVIRONMENTAL VARIABLE / some initialized in command execution / typical example $HOME / home directory $PATH / command path list $0 / the name of the script $$ / pid of the script $? / return values $C_INCLUDE_PATH Cinclude / list of paths the compiler searches include files $LD_LIBRARY_PATH / list of paths the loader searches the libraries $LIBRARY_PATH / list of paths the linker searches the libraries env / 'env' command will display them.
/ PARAMETER VARIABLE / the argument from command line $1, $2, $3 / the arguments $* $IFS / The array using IFS $@ $*$IFS / The array without IFS $ IFS=: $ set foo bar bam $ echo $1 foo $ echo $@ foo bar bam $ echo $* foo:bar:bam $ unset IFS $ echo $* foo bar bam
TEST([]) / TEST COMMAND / conditions in the shell / comparison of strings $ test hoge = hoge $ echo $? 0 $ test hoge = foo $ echo $? 1 $ test hoge!= foo $ echo $? 0 $ name=geroge $ test n $name # null $ echo $? 0 $ test n $ echo $? 1
TEST / ARITHMETIC COMPARISON / greater or less? $ test 100 eq 100 # $ echo $? 0 $ test 100 eq 200 $ echo $? 1 $ test 100 gt 200 # 100 > 200 $ echo $? 1 $ test 100 le 200 # 100 <= 200 $ echo $? 0 ge, lt, ne,!
TEST / FILE CHECK / existence and file type checks $ touch hoge.txt $ mkdir foo $ test e hoge.txt # $ echo $? 0 $ test f hoge.txt # $ echo $? 0 $ test f foo $ echo $? 1 $ test d foo # $ echo $? 0 g, r, s, w, u, x
IF / IF STATEMENT iftest / use test for condition test[]test / '[ ]' is equivalent to test if test f file.txt; then if [ -f file.txt ]; then [ condtion ] if / if statement if condition then statements elif condition statements else statements fi
IF / EXAMPLE / lock file example lock=/tmp/$0.lock if [ "$1" = "lock" ]; then if [ -f $lock ]; then exit 1 else touch $lock fi elif [ "$1" = "unlock" ]; then if [ -O $lock ]; then rm $lock else exit 1 fi fi
AND, OR & concatenation of conditions if [ condition1 ] && [ condition2 ]; then statements fi if [ condition1 ] [ condition2 ]; then statements fi
FOR / REPETITION BY FOR for / syntax for variable in values do statements done example $ cat for.sh for var in foo bar bam 1234 do echo $var done $ sh for.sh foo bar bam 1234
FORVAR / USE COMMAND'S RESULTS.old / backup (copy) all files to <file>.old $ cat old.sh for var in $(ls); do cp $var $var.old done $ ls bar.txt foo.txt old.sh $ sh old.sh $ ls bar.txt foo.txt bar.txt bar.txt.old foo.txt.old bar.txt.old
WHILEUNTIL / WHILE AND UNTIL while / syntax condition repeated while the condition is true while condition do statements done until / syntax condition / repeated until the condition becomes true until condition do statements done
WHILEN / N-TIMES REPETITION BY WHILE $ cat count.sh i=1 while [ $i le 20 ]; do echo $n i=$(($i+1)) # $(()) done $ sh count.sh 1 2 3 4 5 6...
CASE / PATTERN MATCH BY CASE case / syntax case variable in pattern [ pattern]...) statements pattern [ pattern]...) statements *) statements esac case / example $ cat hello.sh case $1 in morning ) echo good morning! night ) echo good evening! noon evening ) echo hello! esac $ sh hello.sh morning good morning! $ sh hello.sh noon hello!
SHIFTPOP / POPPING ARGUMENTS BY SHIFT shiftpop / an argument is popped by shift $ set foo bar bam $ echo $* foo bar bam $ shift $ echo $* bar bam $ shift $ echo $* bam / process all arguments while [ $1!= ]; do echo $1 done
/ OTHERS break for, while, until / breaks loops continue for, while, until / skip one step in repetitions. source / sources shell script files exec exec / the current shell is replaced with the command man exec expr / arithmetic calculation $(()) / slow than $(())
/ OTHERS trap / signal handler eval / evaluate arguments / generate a script in a script and execute it
SED
AWK
/ LOG COUNT SCRIPT calendar.jaist.ac.jp