P. 1
Unix Shell Scripting With Sh Ksh

Unix Shell Scripting With Sh Ksh

|Views: 91|Likes:
Published by ypraju

More info:

Published by: ypraju on Dec 18, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

10/22/2011

pdf

text

original

A list in these descriptions is a simple command, or a pipeline. The value of the
list is the value of the last simple command run in it.

A list can also be a set of simple commands or pipelines separated by ";,&,&&,||,|
&". For the compound commands which branch on the success or failure of some
list, it is usually [ or [[, but can be anything.

Conditional execution: if/else

list && list

Execute the first list. If true (success), execute the second one.

list || list

Execute the first list. If false (failure), execute the second one.

Example:

mkdir tempdir && cp workfile tempdir

sshd || echo "sshd failed to start"
You can use both forms together (with care) - they are processed left to
right, and && must come first.

Example:

mkdir tempdir && cp workfile tempdir || \
echo "Failed to create tempdir"

if list; then list ; elif list; else list; fi

Execute the first list, and if true (success), execute the "then" list,
otherwise execute the "else" list. The "elif" and "else" lists are optional.

Example:

if [ -r $myfile ]
then

cat $myfile

else

echo $myfile not readable

fi

Looping: 'while' and 'for' loops

while list; do list; done
until
list; do list; done

Execute the first list and if true (success), execute the second list. Repeat
as long as the first list is true. The until form just negates the test.

Example: ex4 display, text

1: #!/bin/ksh
2: count=0
3: max=10
4: while [[ $count -lt $max ]]
5: do
6: echo $count
7: count=$((count + 1))
8: done

for identifier [ in words ]; do; list; done

Set identifier in turn to each word in words and execute the list. Omitting
the "in words" clause implies using $@, i.e. the identifier is set in turn to
each positional argument.

Example:

for file in *.dat
do

echo Processing $file

done
As with most programming languages, there are often several ways to express
the same action. Running a command and then explicitly examining $? can be
used instead of some of the above.

Compound commands can be thought of as running in an implicit subshell. They
can have I/O redirection independant of the rest of the script. Setting of variables
in a real subshell does not leave them set in the parent script. Setting variables in
implicit subshells varies in behaviour among shells. Older sh could not set
variables in an implicit subshell and then use them later, but current ksh can do
this (mostly).

Example: ex11 display, text
Reading a file line by line. The book by Randal Michael contains 12 example
ways to read a file line by line, which vary tremendously in efficiency. This
example shows the simplest and fastest way.

1: #!/bin/sh
2:
3: # Demonstrate reading a file line-by-line, using I/O
4: # redirection in a compound command
5: # Also test variable setting inside an implicit subshell.
6: # Test this under sh and ksh and compare the output.
7:
8: line="TEST"
9: save=
10:
11: if [ -z "$1" ]; then
12: echo "Usage: $0 filename"
13: else
14: if [ -r $1 ]; then
15: while read line; do
16: echo "$line"
17: save=$line
18: done < $1
19: fi
20: fi
21: echo "End value of \$line is $line"
22: echo "End value of \$save is $save"

(19)

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->