0% found this document useful (0 votes)
40 views16 pages

Shell Bash Shell Scripts

The document discusses shell scripts in Linux. It covers various topics related to shell scripts including variables, condition tests, control flow statements like if/then/else and case/esac, loops like for/while/until, breaking and continuing loops, running shell scripts, passing arguments to scripts, exit statuses, and common scripting languages used in Linux like Bash, Perl, Python, C/C++, Java, FORTRAN and Ada.

Uploaded by

rakeshgm66
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views16 pages

Shell Bash Shell Scripts

The document discusses shell scripts in Linux. It covers various topics related to shell scripts including variables, condition tests, control flow statements like if/then/else and case/esac, loops like for/while/until, breaking and continuing loops, running shell scripts, passing arguments to scripts, exit statuses, and common scripting languages used in Linux like Bash, Perl, Python, C/C++, Java, FORTRAN and Ada.

Uploaded by

rakeshgm66
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

40.

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

$ MYAGE=35 $ echo $MYNAME $MYAGE smith 35

shell
$ NUMBER="10" $ expr $NUMBER + 5 15

shell script shell script


$ FILENAME="My Document" $ ls $FILENAME ls: My: No such file or directory ls ls: Document: No such file or directory $ ls -l "$FILENAME" My Document ls

$ 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

Shell script stdin

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

Linux shell exit status


$ cat myfile My name is Sandy Smith and I really like Fedora Linux $ grep Smith myfile My name is Sandy Smith and $ echo $? 0 $ grep aardvark myfile $ echo $? 1

return value $?

Linux 0 POSIX Linux

manpage

ts et
test 0

[
bash shell test 1

Shell Scripts

| 201

$ $ 1 $ $ 0

test 10 -lt 5 echo $? test -n "hello" echo $?

10 < 5

"hello" 0

test

test ]
$ $ 1 $ $ 0 [ 10 -lt 5 ] echo $? [ -n "hello" ] echo $?

$ [ 5 -lt 4] 4 bash: [: missing ']'

test ]

4]

test
-d name -f name -L name -r name -w name -x name

name name name name name 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

a -eq b a -ne b a -gt b a -ge b a -lt b a -le b

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

true echo $? false echo $?

Shell Scripts

| 203

40.5.
if if
if command then body fi command 0

if-then

if [ `whoami` = "root" ] then echo "You are the superuser" fi

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

;; ... exprN bodyN ;; * bodyelse ;; esac

string expr1 expr ... exprN info bash reserved case if ;;


case $letter in X echo "$letter is an X" ;; [aeiou] echo "$letter is a vowel" ;; [0-9] echo "$letter is a digit, silly" ;; * echo "I cannot handle that" ;; esac

$myvar * else

40.6.
while
while command do body done command 0

myscript script

206 | Linux

i=0 while [ $i -lt 3 ] do echo "again" i=`expr $i + 1` done

$ ./myscript 0 1 2

while

until while
until command do body done

until
command 0

i=0 until [ $i -gt 3 ] do echo "again" i=`expr $i + 1` done

$ ./myscript 0 1 2

for

Shell Scripts

| 207

for variable in list do body done

for name in Tom Jack Harry do echo "$name is my friend" done

$ ./myscript Tom is my friend Jack is my friend Harry is my friend

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"

$ ./myscript Tom again Jack again Harry again all done

break
for name in Tom Jack Harry do echo $name if [ "$name" = "Jack" ] then break fi echo "again" done echo "all done"

$ ./myscript Tom again Jack break 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"

$ ./myscript Tom again Jack continue Harry again all done

break continue

continue break script N

break N

continue N

40.8. Shell Scripts


Shell script script bash script

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

script script ...

subshell subshell shell login shell

login shell
shell
$ . myscript

script

shell

script script #!/bin/bash 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

$ ./myscript Bob ./myscript error: you must supply two args

for

$@

for arg in $@ do echo "I found the argument $arg" done

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

Pr el Python C/C++ Java FORTRAN Ada

perl python gcc javac java g77 gnat

http://www.perl.com/ http://www.python.org/ http://www.gnu.org/software/gcc/ http://java.sun.com/ http://www.gnu.org/software/ fortran/fortran.html http://www.gnu.org/software/ gnat/gnat.html

Fedora

Linux distribution

Shell Scripts

| 213

214 | Linux

You might also like