Embedded Commands   «Prev  Next»
Lesson 4 When to use embedded execution
Objective Identify when embedded commands are most useful.

When to use Embedded Execution in Unix

This lesson introduces you to the situations in your shell scripts where embedding commands are most useful. We will also look at examples where embedded commands are not useful. Shell programmers sometimes overuse this feature at first, resulting in commands that do not work at all or commands that force the shell to do extra work.

Saving Command output to a Variable

Embedded command execution is used to save the output of a command to a variable. This is especially useful when the command has output that will change frequently, as do the date or pwd commands. To save the starting time of a script to a variable you might run this command:

starttime=`date`

The UNIX date command outputs the current date and time. The assignment statement above will copy the output of the date command to the variable start time. The date command's output is constantly changing, and putting this statement at the beginning of the script allows you to capture the exact starting time of the script every time you execute it.

Enhancing the output of a command

Embedded command execution can also be used to enhance the output of a command, by making it clearer.
In the following example, the code makes it clear that date +%A returns the day of the week:

dayofweek=`date +%A`

Here are some other useful date command options you may want to use.
Useful Date Command Options
In this next example, the output of the echo command is clearer than would be the output of running the pwd command by itself.
echo "your current directory is `pwd`"

The output is enhanced by joining two commands, the echo command and the pwd command, in an embedded format, to make the output clearer.

Using output as part of another command

Embedded command execution is useful when the output of one command must be used as part of another command. In this example, we would like to search the /etc/hosts file for the current host.

grep `hostname` /etc/hosts

The example above uses the UNIX grep command, which searches a file for a string and prints out any lines containing the string. We want to search the /etc/hosts file for the current host name, so we embed the hostname command, which outputs the name of the host you are logged in to. In this command, the output of the hostname command is used as part of the surrounding grep command.

Generating useful file names

You might also use this feature to generate a useful file name. The following command captures the output of the ps –ef command to a file whose name contains the current day and month. This information is generated using the date +%b-%e command:

$ ps –ef > /tmp/psout.`date +%b-%e`
$ ls /tmp/psout.*
/tmp/psout.Sep-25

Inappropriate usage

The command below shows an embedded execution that is not useful:
$ echo `date`
Thu Sep 30 23:47:45 PDT 1999

This command echoes the output of the date command to the screen. However, this command is extra work for the shell.
It runs the date command to find the date and time, then it echoes this information to the screen. This is not necessary, and the shell must go through the 2-step embedded command execution process to run the command. You can run the date command by itself instead.

$ date
Thu Sep 30 23:47:45 PDT 1999

The following example shows e-e that will not work at all:
$ `ps`
ksh: PID:  not found

Look at the error message from this command. Because back quotes are used around a solo command, instead of an embedded command, the shell is trying to take the first word of the command output and run it as a command. Don’t use back quotes around solo commands. Only use them when embedding one command inside another.