Shell Bash Shell Scripts
Shell Bash Shell Scripts
Shell Scripts
shell bash shell scripts shell ... if-then-else shell script info bash shell learn_bashshell.html bash
http://www.oreilly.com.tw/chinese/linux/
40.1.
bash shell scripts keywords \n shell script shell shell
40.2.
variable
$ MYNAME="smith "
Shell Scripts
| 199
shell
$ NUMBER="10" $ expr $NUMBER + 5 15
$ HAT="fedora" $ echo "The plural of $HAT is $HATs" The plural of fedora is $ echo "The plural of $HAT is ${HAT}s" The plural of fedora is fedoras
HATs
40.3.
Script 34.
$ echo "Hello world" Hello world $ printf "I am %d years old\n" `expr 20 + 20` I am 40 years old
echo
printf
174
200 | Linux
read
$ read name Sandy Smith Enter $ echo "I read the name $name" I read the name Sandy Smith
40.4.
Boolean test false shell 0 true
return value $?
manpage
ts et
test 0
[
bash shell test 1
Shell Scripts
| 201
$ $ 1 $ $ 0
10 < 5
"hello" 0
test
test ]
$ $ 1 $ $ 0 [ 10 -lt 5 ] echo $? [ -n "hello" ] echo $?
test ]
4]
test
-d name -f name -L name -r name -w name -x name
202 | Linux
-s name
name f 1 f 1 f 2 f 2
f -nt f 1 2 f -ot f 1 2 s1 = s2 s1 ! s2 =
-z s1 -n s1
s1 s1 a a a a a a b b b b b b
0 0
t - t 1 a 2 t - t 1 o 2
!your_test
AND OR
t 1 t 1 your_test
t 2 t 2
\( your_test \)
tu re
bash
fle as
true 0 1 false
$ $ 0 $ $ 1
Shell Scripts
| 203
40.5.
if if
if command then body fi command 0
if-then
if-then-else
if command then body1 else body2 fi
if [ `whoami` = "root" ] then echo "You are the superuser" else echo "You are an ordinary dude" fi
if-then-elif-else
if command1 then body1 elif command2 then body2 elif ... ... else
command1
command2
204 | Linux
bodyN fi
if [ `whoami` = "root" ] then echo "You are the superuser" elif [ "$USER" = "root" ] then echo "You might be the superuser" elif [ "$bribe" -gt 10000 ] then echo "You can pay to be the superuser" else echo "You are still an ordinary dude" fi
if-thenelif-else case
echo 'What would you like to do?' read answer case "$answer" in answer eat echo "OK, have a hamburger" ;; sleep echo "Good night then" ;; * echo "I'm not sure what you want to do" echo "I guess I'll see you tomorrow" ;; esac
case
case string in expr1 body1 ;; expr2 body2
Shell Scripts
| 205
$myvar * else
40.6.
while
while command do body done command 0
myscript script
206 | Linux
$ ./myscript 0 1 2
while
until while
until command do body done
until
command 0
$ ./myscript 0 1 2
for
Shell Scripts
| 207
for
for file in *.doc do echo "$file is a stinky Microsoft Word file" done
while
until
while true do echo "forever" done until false do echo "forever again" done
break exit
208 | Linux
40.7. break
break
continue
myscript
for name in Tom Jack Harry do echo $name echo "again" done echo "all done"
break
for name in Tom Jack Harry do echo $name if [ "$name" = "Jack" ] then break fi echo "again" done echo "all done"
continue myscript
for name in Tom Jack Harry
Shell Scripts
| 209
do echo $name if [ "$name" = "Jack" ] then continue fi echo "again" done echo "all done"
break continue
break N
continue N
script
#!/bin/bash
$ chmod +x myscript
script script
~/bin
210 | Linux
/usr/local/bin script
$ myscript
script . script
$ ./myscript
./
shell
script
subshell
bash
$ bash myscript
script
login shell
shell
$ . myscript
script
shell
40.9.
Shell scripts Linux shell script Linux scripts. bash
Fedora
Linux distribution
Shell Scripts
| 211
$@
$1
$2
$3 ...
$ cat myscript #!/bin/bash echo "My name is $1 and I come from $2" echo "Your info : $@" $ ./myscript Johnson Wisconsin My name is Johnson and I come from Wisconsin Your info : Johnson Wisconsin
script bash $#
if [ $# -lt 2 ] then echo "$0 error: you must supply two arguments" else echo "My name is $1 and I come from $2" fi
$0
script
script
for
$@
40.10.
exit 0 script 0 script 1 exit shell shell
212 | Linux
if [ $# -lt 2 ] then echo "Error: you must supply two args" exit 1 else echo "My name is $1 and I come from $2" fi exit 0 $ ./myscript Bob ./myscript error: you must supply two args $ echo $? 1
40.11.
Shell scripts
Shell Scripting
...
Linux
Fedora
Linux distribution
Shell Scripts
| 213
214 | Linux