Quantcast
Channel: Unix Scripting – wikiconsole
Viewing all articles
Browse latest Browse all 10

The (…) and { …; } Constructs in Unix Shell script

$
0
0

Sometimes you may want to group a set of commands together for some reason. For example, you may want to send a sort followed by execution of your plot data program into the background for execution. You can group a set of commands together by enclosing them in a set of parentheses or braces. The first form causes the commands to be executed by a sub shell, the latter form by the current shell.

Here are some examples to illustrate how they work:

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ x=50</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ (x=100)                        Execute this in a subshell</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ echo $x</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">50                            Didn't change</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ { x=100; }                     Execute this in the current shell</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ echo $x</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">100</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ pwd                            Where am I?</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">/users/steve</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ (cd bin; ls)                   Change to bin and do an ls</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">add</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">greetings</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">lu</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">number</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">phonebook</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">rem</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">rolo</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ pwd</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">/users/steve                  No change</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ { cd bin; }                   This should change me</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ pwd</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">/users/steve/bin</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

If the commands enclosed in the braces are all to be typed on the same line, a space must follow the left brace, and a semicolon must appear after the last command.

As the example

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">(cd bin; ls)</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

shows, the parentheses are useful for doing some commands without affecting your current environment. You can also use them for other purposes:

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ (sort 2002data -o 2002data; plotdata 2002data) &amp;</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">[1]    3421</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

The parentheses group the sort and plotdata commands together so that they can both be sent to the background for execution, with their order of execution preserved.

Input and output can be piped to and from these constructs, and I/O can be redirected. In the next example, a

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">.ls 2</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

nroff command (for double-spaced output) is effectively tacked to the beginning of the file memo before being sent to nroff.

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ { echo ".ls 2"; cat memo; } | nroff -Tlp | lp</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

In the command sequence

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ { prog1; prog2; prog3; } 2&gt; errors</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

all messages written to standard error by the three programs are collected into the file errors.

As a final example, let’s return to the mon program from Chapter 9, “‘Round and ‘Round She Goes.” As you’ll recall, this program periodically checked for a user logging on to the system. One of the comments we made back then is that it would be nice if the program could somehow automatically “send itself” to the background for execution because that’s how it’s really meant to be run. Now you know how to do it: You simply enclose the until loop and the commands that follow inside parentheses and send it into the background:

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ cat mon</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">#</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"># Wait until a specified user logs on -- version 4</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">#</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"># Set up default values</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">mailopt=FALSE</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">interval=60</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"># process command options</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">while getopts mt: option</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">do</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">       case "$option"</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">       in</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">                   m)    mailopt=TRUE;;</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">                   t)    interval=$OPTARG;;</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">                  ?)    echo "Usage: mon [-m] [-t n] user"</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">                         echo" -m means to be informed by mail"</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">                         echo" -t means check every n secs."</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">                         exit 1;;</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">       esac</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">done</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"># Make sure a user name was specified</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">if [ "$OPTIND" -gt "$#" ]</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">then</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">       echo "Missing user name!"</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">       exit 2</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">fi</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">shiftcount=$(( OPTIND – 1 ))</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">shift $shiftcount</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">user=$1</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">#</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"># Send everything that follows into the background</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">#</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">(</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">   #</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">   # Check for user logging on</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">   #</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">   until who | grep "^$user " &gt; /dev/null</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">   do</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">          sleep $interval</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">   done</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">   #</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">   # When we reach this point, the user has logged on</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">   #</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">   if [ "$mailopt" = FALSE]</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">   then</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">          echo "$user has logged on"</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">   else</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">          runner=$(who am i | cut -c1-8)</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">          echo "$user has logged on" | mail $runner</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">   fi</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">) &amp;</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

The entire program could have been enclosed in parentheses, but we arbitrarily decided to do the argument checking and parsing first before sending the remainder to the background.

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ mon fred</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$                             Prompt comes back so you can continue working</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">  ...</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">fred has logged on</span>

<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>

Note that a process id number is not printed by the shell when a command is sent to the background within a shell program.


Viewing all articles
Browse latest Browse all 10

Trending Articles