Download presentation
Presentation is loading. Please wait.
Published byElliott Rhodes Modified over 10 years ago
1
#!/bin/sh 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 output list & (list) `list` { list;} name=value name () { list;} $name ${name}
5
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 "youve got $myvar" fi
6
Flow control - for for name [ in word … ] do list done #!/bin/sh for i in 5 4 3 2 1; do echo "countdown: $i"; done
7
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"
8
Flow control - case case word in [ pattern [ | pattern ] ) list ;; ] … esac #!/bin/sh case $1 in hi) echo "hello~";; bye) echo "byebye~";; esac
9
Predefined variables # : number of arguments ? : last commands return value $ : process id of this shell ! : process id of last background command 1, 2, 3, … 9 : n-th argument
10
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
11
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
12
Redirection Syntax : command |>> path Connecting processs 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
13
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]
14
References man pages for sh/ksh/bash/csh/tcsh
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.