Presentation is loading. Please wait.

Presentation is loading. Please wait.

#!/bin/sh 2002/07/26 Jaeho Shin. Interface between user and the operating system sh, bash, csh, tcsh, ksh, … What is a “Shell”? OSuser Shell.

Similar presentations


Presentation on theme: "#!/bin/sh 2002/07/26 Jaeho Shin. Interface between user and the operating system sh, bash, csh, tcsh, ksh, … What is a “Shell”? OSuser Shell."— Presentation transcript:

1 #!/bin/sh 2002/07/26 Jaeho Shin

2 Interface between user and the operating system sh, bash, csh, tcsh, ksh, … What is a “Shell”? OSuser Shell

3 Shell Script? A command programming language that executes commands read from a terminal or a file Uses built-in commands/syntax and OS programs Why use shell scripts? –Can do repetitive tasks easily & quickly

4 Basic Syntax # comments… list; list; list; … list | list | list … list && list || list … list out <>io list n>&n list & (list) { list;} name=value name () { list;} ${name} $(command) $((expr))

5 Expansion ${parameter} ${parameter:-word} ${parameter:=word} ${#parameter} ${parameter%word} ${parameter#word} $(command) or `command` $((arithmetic expr))

6 Flow control - if if list ; then list ; [ elif list ; then list ; ] [ else list ; ] fi #!/bin/sh myvar="myvalue" if [ "$myvar" = "" ]; then echo "nothing!" else echo "you’ve got $myvar" fi

7 Flow control - for for name [ in word … ] do list done #!/bin/sh for i in 5 4 3 2 1; do echo "countdown: $i"; sleep 1 done

8 Flow control – while while list; do list done #!/bin/sh unit="x" while [ "$string" != "xxxxxx" ]; do string=$string$unit echo "not yet~ ($string)" done echo "ok~ got $string"

9 Flow control - case case word in [ pattern [ | pattern ] ) list ;; ] … esac #!/bin/sh case $1 in hi) echo "hello~";; bye) echo "byebye~";; *) echo “what?!”;; esac

10 Test expression – [ (1/2) Usage: [ expr ] expr1 -a expr2 : expr1 and expr2 expr1 -o expr2 : expr1 or expr2 ! expr : not expr -f path : path is a file -s path : file at path is larger than 0 -r path : path is readable

11 Test expression – [ (2/2) -w path : path is writeable -x path : path is executable str1 == str2 : str1 is equal to str2 str1 != str2 : str1 is not equal to str2 arg1 OP arg2 : OP is one of -eq, -ne, -lt, -gt, -le, -ge. Arithmetic binary operation. …

12 Predefined variables # : number of arguments ? : last command’s return value $ : process id of this shell ! : process id of last background command 1, 2, 3, … 9 : n-th argument …

13 Example script #!/bin/sh # shortcut script case $1 in a) telnet ara;; s) telnet ska;; l) telnet loco;; n) telnet noah;; m) mutt t) if [ -f $HOME/TODO ]; then cat $HOME/TODO | more fi;; esac

14 Pipelining Syntax : command1 | command2 Connecting one process(command1)’s output to another process(command2)’s input Can do complex jobs by combining many small programs Examples –ls -l | sort –finger | cut -d " " -f 3,4,5 | wc

15 Redirection Syntax : command |>|>> path Connecting process’s output/input to a given file Can save any output to a file and can load any file to a program input Examples –du -sh * > du_dump.file –find ~ -name.*rc > rcfiles –sort sorteddata –date >> mydatelogfile

16 Job control Job control is a way to do multitasking with the command-line interface Syntax and commands –command & –bg [%n] –fg [%n] –jobs –kill [%n]

17 References man pages for sh/ksh/bash/csh/tcsh


Download ppt "#!/bin/sh 2002/07/26 Jaeho Shin. Interface between user and the operating system sh, bash, csh, tcsh, ksh, … What is a “Shell”? OSuser Shell."

Similar presentations


Ads by Google