Using Commands Within Commands

Using Commands Within Commands

Now that you have used variables and arguments, you are probably wondering how to have your scripts do something simple like adding two numbers together and storing the results in a variable. The answer to this lies in using a powerful feature of Unix shells called backquoted commands . Backquoted commands enable you to incorporate one command line within another command line. The backquote (also called a backtick ) is the ` character, usually found in the upper left corner of your keyboard, just above . The backquote ` is not the same as the apostrophe, ' , so be careful not to confuse the two.

You use backquoted commands in scripts any time you want to store the output of a command line in a variable. You also use them when you want to create a command line in which part of the command line comes from the output of some other command.

We mentioned the use of backquotes briefly in Chapter 2, and now we will go into more detail about them.

To use backquotes in a command line:

  • Enclose part of a command line in backquotes. The part of the command line you enclose in backquotes must itself be a valid command line:

    file `which ls`

    Any time the shell sees a part of a command line that is enclosed in backquotes ( which ls in the example above), it executes the enclosed commands first, just as if they were a complete command line of their own. In this case, it will return the full path of the ls command.

    The shell takes the output ( /bin/ls , in the case of the example) and replaces the backquoted portion of the original command line with the output from the backquoted command. So the command line would then read

    file /bin/ls

    The shell executes this final command line, which in this case gives the output

    /bin/ls: Mach-O executable ppc

    This tells you that the actual executable file for the ls command is a "Mach-O executable ppc," which means that it is a binary file, not a text file, compiled for the PowerPC chip.

To save the output of a command in a variable:

  • Start a command line as you would for a normal variable assignment. For example,

    today=

  • Put the backquoted command right after the = sign. For example,

    today=`date`

    When the line is executed, the shell first executes the backquoted portion, and then stores the output of that portion in the variable. Figure 9.16 is a code listing of a script that stores the output of a backquoted command in a variable. The +%A, %B %d is a formatting argument for the date command. %A is the full weekday name , %B is the full month name, and %d is the two-digit day of the month. See man date and man strftime for details on the available formatting options. Notice that the backquoted command may include arguments (in this case a formatting string for the date command). Figure 9.17 shows the output from the script in Figure 9.16.

    Figure 9.16. Code listing of a script that stores the output of a backquoted command in a variable.
     #!/bin/sh # This script uses a backquoted command. today=`date "+%A, %B %d"` echo "Hello $USER, today is $today" 

    Figure 9.17. Output from the script in Figure 9.16.
     localhost:~ vanilla$  ./script.sh  Hello vanilla, today is Monday, June 17 [localhost:~] 

Tip

  • You can (and should) test whatever you put in backquotes by typing it at a shell prompt first.


Floating-Point Math

Two Unix commands do floating-point arithmetic at the command line or in a shell script: dc and bc . Both are fairly complex programs. The dc command uses "reverse Polish notation" (developed in 1920 by Jan Lukasiewicz; see the RPN page on MoHPC, www.hpmuseum.org/rpn.htm), in which you enter numbers and operators and then ask for a result. For example, to use dc to divide 23 by 5 with a precision of four decimal places, you would enter

echo "4 k 23 5 / p" dc

This is not pretty. The equivalent command line for bc is only slightly better:

echo "scale=4; 23 / 5" bc

Read the man pages for these commands for the full details.




Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

Similar book on Amazon

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