|
Shell Programming CONTROL STRUCTURES The control flow commands
alter the order of execution of commands within a shell script. They include the
if...then, for...
in, while, until, and
case statements. In addition, the break and continue
statements
work in conjunction with the control flow structures to alter the order of
execution of commands within a script. if.. .then The syntax of the if…
then control structure is
If test-command
The test builtin returns a status of true if
the two words are the same and false if
they are not. Double quotation marks around $word1 and $word2 make sure that
test works properly if you enter a
string that contains a SPACE or other special character.
bash$ cat
ifi echo -n "word 1: " if test
"$word1" = "$word2" bash$ ifi
word1:
peach BUILTINS exec §
Runs a
command without creating a new process, therefore it makes program to run
faster. §
Redirects
standard input, output, or error of a shell script from within the script
When you run a command using the exec, exec executes the new command in
place of (overlays) the current process. Exec does not return control to the
original program, the exec bultin can only be used with the last command that
you want to run the script. $ cat
exec_demo who $ exec_demo [wmorales@rc33uxas01 wmorales]$ bash ~/exec_demo nwijerat pts/0 Mar 1
02:00 (ABDECEB6.ipt.aol.com) rhussein
pts/2 Mar 1 04:36 (ip1.portland12.or.pub-ip.psi.net) wmorales
pts/3 Mar 1 05:15 (net22-cust43.pdx.wantweb.net) Wed
Mar 1 05:28:38 PST 2000 [wmorales@rc33uxas01 wmorales]$
BUILTINS trap
The trap builtin
catches or traps a signal. A signal is a report to a process about a condition.
The Linux system uses signals to report interrupts generated by the user (ie.
Pressing the interrupt key). Using trap you can direct the action a script takes
when it receives a signal.
trap ['commands'] [singal-numbers] $cat
inter #!/bin/bash trap
'echo PROGRAM INTERRUPTED; exit 1' 2 while
true do
echo "Program running." done
[wmorales@rc33uxas01
wmorales]$ bash ~/inter Program
running.
FUNCTIONS A
shell function is similar to shell script. Shell functions are a way to group
commands for later execution using a single name for the group. They are
executed just like a "regular" command. Shell functions are executed
in the current shell context; no new process is created to interpret them. You
can: §
Declare a
funcition in your .bash_profile §
In a
script §
Or in the
command line §
Use unset
builtin to remove functions function-name ()
{ $cat
whofunction echo
This is an example of a function whoson() { whoson [wmorales@rc33uxas01
wmorales]$ ./whofunction This
is an exemple of a function Wed
Mar 1 06:37:05 PST 2000 Users
logged on wmorales
pts/0 Mar 1 06:32 (net22-cust43.pdx.wantweb.net) rhussein
pts/2 Mar 1 04:36 (ip1.portland12.or.pub-ip.psi.net) wmorales
pts/3 Mar 1 05:15 (net22-cust43.pdx.wantweb.net) rhussein
pts/4 Mar 1 06:08 (ip184.portland11.or.pub-ip.psi.net) [wmorales@rc33uxas01 wmorales]$
Links:
~ ~ ~
|
Back to
CS140U Homepage |