Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
Shells: User's Guide: HP 9000 Computers > Chapter 6 Advanced Programming

Other Commands

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Index

The . (dot) Command

Normally, when you execute a shell program, a subshell is created in which to execute it. Therefore, if you define variables in the program, they are only good for as long as the program is executing (when the program is done, you return to the current shell's environment). If you wish to have the shell program executed in the current shell (and thus make the defined variables good for the current shell's environment), use the "dot" command:

. scriptname

Make sure there is a space between the dot (.) and the script name (otherwise the system will assume it is part of the script name). Let's look at an example.

Create a file with the following commands:

echo $dog
dog=tired
echo $dog

Make the script executable with the chmod command ("chmod +x dogsample", where "dogsample" is the name of the script). Next, define the variable dog to be:

dog=rover

Run the script (by typing dogsample) without the dot command. The results will be:

rover
tired

Now, check to see what the value of dog is:

$ echo $dog
rover
$

The original value for dog appears. This is because the shell was executed in a subshell. Now, try the dot command:

$ . dogsample
rover
tired
$

and then test the value of dog:

$ echo $dog
tired
$

The value of dog was changed because the script was run in the current shell.

The eval Command

The eval command reads its arguments as input to the shell, and the resulting commands are executed. The format is:

eval [arg]...

where arg is an argument that is a shell command or shell program. Here is an example:

eval "grep jones $p_file | set | echo $1 $2 $4"

eval will execute the pipe contained in double quotes in the shell.

If you use the following:

s='date &'; $s

you would receive an error message from date. The "&" is ignored as a special character (due to the single quotes). So, to make the command function as expected, use eval:

s='eval date &'; $s

and the eval will reparse the string and thus attach the special meaning to "&".

Using Shell Expansions

You read about pattern matching in “File Name Generation”. Here are some examples which will simplify some of the constructs you just learned.

When you generate lists for your for constructs (or any other construct where you are trying to generate filenames without needing to type in each file name), you are free to use pattern-matching characters. For example:

for i in *.c
do
mv $i /tmp
done

Here we generate a loop in which i is set to each file name in the current directory that ends in ".c."

case $i in
?[dD].c ) echo $i ;;
*[!nN] ) mv $i .. ;;
* ) exit ;;
esac

This case construct will match the value of i on the first pattern line if it begins with any single character (?), followed by either "d.c" or "D.c". The second pattern line matches any string (including the null) ending in any letter other than "n" or "N". The last expression matches anything left over.

Helpful Tips

Let's wrap up this section with a couple of helpful items.

  • To print a character that will "beep" to alert a user, use CTRL-G in an echo command.

  • To add control characters to the vi editor, you must first type CTRL-V, then type the control string.

  • To break from a "for" or "while" loop, use the break statement. If you want to break out of a certain number of levels in a nested loop construct, add break n, where n in the number of levels of nesting. As an example, consider:

    for i
    do
    while true
    do
    ...
    break 2
    This "break 2" gets you beyond the second "done". It breaks you out of two enclosing loops (for and while).
    done
    done
  • To continue with the next iteration of the enclosing loop, use the continue statement. To continue at the next iteration of the nth enclosing loop, use continue n.

  • For more items, look in the sh(1) entry in the HP-UX Reference manual. Some of these features will be discussed in“Example: Groupcopy”.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-1991 Hewlett-Packard Development Company, L.P.