| United States-English |
|
|
|
![]() |
Shells: User's Guide: HP 9000 Computers > Chapter 6 Advanced ProgrammingOther Commands |
|
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:
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:
Run the script (by typing dogsample) without the dot command. The results will be:
Now, check to see what the value of dog is:
The original value for dog appears. This is because the shell was executed in a subshell. Now, try the dot command:
and then test the value of dog:
The value of dog was changed because the script was run in the current shell. 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 will execute the pipe contained in double quotes in the shell. If you use the following:
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:
and the eval will reparse the string and thus attach the special meaning to "&". 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:
Here we generate a loop in which i is set to each file name in the current directory that ends in ".c."
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. Let's wrap up this section with a couple of helpful items.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||