Process Execution

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents
Chapter 2.  Korn Shell Basics


This section provides a quick overview of how the Korn shell interacts with Unix. For the following sections, it is assumed that you are logged as a regular user, therefore you have the default command prompt - $.

Multiple Commands

Multiple commands can be given on the same line if separated with the ; character. Notice that the command prompt is not displayed until the output from all three commands is displayed:

 $ pwd ; ls dialins ; echo Hello  /home/anatole  dialins  Hello  $ 

This is also useful when you want to run something more complex from the command line, like rename all the files in the current directory using flow control commands:

 $ for i in $(ls); do mv $i $i.bak; done 

The for loop command is covered in more detail in Chapter 8.

Continuing Commands

If a command is terminated with a \ character, it is continued on the next line. Here, the echo arguments are continued onto the next line:

 $ echo a b \  > c  a b c 

This is often used to make Korn shell scripts mode readable. Refer to Appendix D for some good examples. The echo command itself can be continued onto the next line by using the \ character:

 $ ec\  > ho a b c  a b c 

Background Jobs

Commands terminated with a & character are run in the background. The Korn shell does not wait for completion, so another command can be given, while the current command is running in the background. In this example, the ls command is run in the background while pwd is run in the foreground:

 $ ls  lR /usr > ls.out &  [1] 221  $ pwd  /home/anatole/bin 

This feature is discussed in detail in Chapter 5.

Pipes

The output of a command can be directed as the input to another command by using the | symbol. Here, a pipe is used to see if root is logged on by connecting the output of who to grep:

 $ who | grep root  root  console  Sep 12 22:16 

It can also be used to count the number of files in a directory by connecting the ls and wc commands. This shows that there are eleven files in the current directory:

 $ ls | wc  l  11 

You can also have multiple pipes to connect a series of commands together. The name of each unique user that is logged on is displayed using this command:

 $ who | cut  f1  d' ' | sort  u  anatole  root 

You could even add another pipe to give you just the count of unique users:

 $ who | cut  f1  d' ' | sort  u | wc  l  3 

Conditional Execution

The Korn shell provides the && and || operators for simple conditional execution. First you need to know that when programs finish executing, they return an exit status that indicates if they ran successfully or not. A zero exit status usually indicates that a program executed successfully, while a non-zero exit status usually indicates that an error occurred.

If two commands are separated with &&, the second command is only executed if the first command returns a zero exit status. Here, the echo command is executed, because ls ran successfully:

 $ ls temp && echo "File temp exists"  temp  File temp exists 

Now, file temp is removed and the same command is run again:

 $ rm temp  $ ls temp && echo "File temp exists"  ls: temp: No such file or directory 

If two commands are separated with ||, the second command is only executed if the first command returns a non-zero exit status. In this case, the echo command is executed, because ls returned an error:

 $ ls temp || echo "File temp does NOT exist"  ls: temp: No such file or directory  File temp does NOT exist 

Remember that basic conditional execution using these operators only works with two commands. Appending more commands to the same command-line using ; does not cause these to also be conditionally executed. Here, the touch temp command is executed, regardless if ls temp failed or not. Only the echo command is conditionally executed:

 $ ls temp || echo "File temp does NOT exist"; \  touch temp  ls: temp: No such file or directory  File temp does NOT exist 

The next section talks about how you can conditionally execute more than one command by using {}'s or ()'s. As you can see, the && and || operators can come in handy. There are certainly many situations where they can be more efficient to use than the if command.

There is one more type of logic you can perform using these operators. You can implement a simple if command by using the && and || operators together like this:

 command1 && command2 || command3 

If command1 returns true, then command2 is executed, which causes command3 to not be executed. If command1 does not return true, then command2 is not executed, which causes command3 to be executed.

Confusing, right? Let's look at a real example to make sense out of this. Here, if the file temp exists, then one message is displayed, and if it doesn't exist, the another message is displayed:

 $ touch temp  $ ls temp && echo "File temp exists" || echo \  File temp does NOT exist  temp  File temp exists 

Now we remove file temp and run the same command:

 $ rm temp  $ ls temp && echo "File temp exists" || echo \  "File temp does NOT exist"  ls: temp: No such file or directory  File temp does NOT exist 

Although compact, this format may not be considered as readable as the if command. We look at comparing the && and || operators to the if command later in Chapter 8.

Grouping Commands

Multiple commands can be grouped together using {} or (). Commands enclosed in {} are executed in the current shell. This is useful for when you want to combine the output from multiple commands. Here is file temp:

 $ cat temp  The rain in Spain  falls mainly on the plain 

Let's say we want to add a header to the output, then line-number the whole thing with the nl command (or pr n if you don't have nl). We could try it like this:

 $ echo "This is file temp:" ; cat temp | nl  This is file temp:  1  The rain in Spain  2  falls mainly on the plain 

Only the output from cat temp was line numbered. By using {}'s, the output from both commands is line numbered:

 $ { echo "This is file temp:"; cat temp ;} | nl  1  This is file temp:  2  The rain in Spain  3  falls mainly on the plain 

There must be whitespace after the opening {, or else you get a syntax error. One more restriction: commands within the {}'s must be terminated with a semi-colon when given on one line. This keeps commands separated so that the Korn shell knows where one command ends, and another one begins.

This means that commands can be grouped within {}'s even when separated with newlines like this:

 $ { pwd ; echo "First line"  > echo "Last line"  > }  /usr/spool/smail  First line  Last line 

Another use for this feature is in conjunction with the && and || operators to allow multiple commands to be conditionally executed. This is similar to the example from the previous section. What we want this command to do is check if file temp exists, and if it does, display a message, then remove it. Unfortunately, the way it is written, rm temp is executed regardless of whether it exists or not.

 $ rm temp  $ ls temp && echo "temp exists.removing";rm temp  ls: temp: No such file or directory  rm: temp: No such file or directory 

Table 2.1. Command Execution Format

command1; command2

execute command1 followed by command2

command &

execute command asynchronously in the background; do not wait for completion

command1 | command2

pass the standard output of command1 to standard input of command2

command1 && command2

execute command2 if command1 returns zero (successful) exit status

command1 || command2

execute command2 if command1 returns non-zero (unsuccessful) exit status

command |&

execute command asynchronously with its standard input and standard output attached to the parent shell; use read ?p/print p to manipulate the standard input/output (see Chapter 8)

command \

continue command onto the next line

{command;}

execute command in the current shell

(command)

execute command in a subshell

By using {}'s, both the echo and rm commands are only executed if temp exists:

 $ touch temp  $ ls temp && { echo "temp exists..removing" ;  > rm temp ; }  temp  temp exists..removing 

Commands enclosed in () are executed in a subshell. This is discussed in detail in Chapter 7.


       
    Top
     



    Korn Shell. Unix and Linux Programming Manual, Third Edition
    Korn Shell. Unix and Linux Programming Manual, Third Edition
    ISBN: N/A
    EAN: N/A
    Year: 2000
    Pages: 177

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net