|
||||||||
|
|
![]() |
|
|
|
|
||
|
Executing Qshell Commands Dynamically Hey, Ted: I hope you can help another Qshell newbie. I would like to load a variable with a value like ">output.txt" or "|sort -r >anotherfile.txt" in order to specify the destination of output of a command at runtime. Qshell always responds with error 001-2113. Is it possible to specify redirection of output in a variable? --Brad Qshell is young, so by definition, everyone who uses it is a newbie. Qshell processes redirection operators before expanding variables. To understand the problem, consider this command: ls $FileSpec $dest Assume that the FileSpec and dest variables have the values *.txt and >data.dat respectively. You would think that the command would expand to this: ls *.txt >data.dat Well, it does expand to that, but only after redirection has been taken care of. Therefore, ls reads the value >data.dat as if it were a file parameter. The solution to your problem is the eval utility. eval ls $FileSpec $dest Eval processes a command twice. The first time, it forces variable expansion. The second time it runs the expanded command. In this example, the first pass through the command expands the two variables. The second pass executes the generated command. Another time you may want to use eval is when a variable contains the name of another variable. For example, suppose that variable field has the value $name, and variable name has the value Joe. Do you see why the following code produced the results it did? echo $field $name eval echo $field Joe --Ted This article has been updated since it was first published. The code snippet for the eval utility has been changed from "eval $FileSpec $dest" to "eval ls $FileSpec $dest" to include "ls" in the snippet. Guild Companies regrets the error. [Correction made 10/10/03.]
|
Editors
Contact the Editors |
| Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. |