Newsletters Subscriptions Forums Media Kit About Us Contact Search Home

mgo
OS/400 Edition
Volume 3, Number 69 -- October 10, 2003

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.]


Sponsored By
ITERA

Echo2 High Availability -
Affordable, Reliable, Easy-to-Use

Reliable, state-of-the-art high availability isn't as expensive and complex as you might think. Not only does Echo2 cost about half of other solutions, it so automated and self-managing that operation of the system can be done in less than an hour a day and production-to-backup switchovers can be accomplished in minutes.

For your Free whitepaper, "An Introduction to iSeries High Availability," call
1-800-957-4511, email us at info@iterainc.com, or visit www.iterainc.com.



THIS ISSUE
SPONSORED BY:

WorksRight Software
iTera


BACK ISSUES

TABLE OF
CONTENTS

Converting XML Attributes to Elements

Executing Qshell Commands Dynamically

Reader Feedback and Insights: Unix Is Not All Bad


Editors
Howard Arner
Joe Hertvik
Ted Holt
David Morris

Managing Editor
Shannon Pastore

Publisher and
Advertising Director:

Jenny Thomas

Advertising Sales Representative
Kim Reed

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.