Cynosure.X International LLC

: :

Add Comment | Related Links | TrackBack
Related Content

Executing a String from the UNIX Shell Script

Sometimes, in your shell script, you'd want to read in a command--from a file, an user input, etc.--and executing it. It's easy if the command is a simple command that doesn't involve pipe and redirection. But when you involve pipe and redirection, the execution fails. Take the following example script for example:

cmd="cat /etc/hosts.deny | grep \"waldo\""

This is a simple shell script that want to send the /etc/hosts.deny file through the grep function. It's stored in the cmd variable to be executed at a later time. The following is the output of this script:

cat: |: No such file or directory
cat: grep: No such file or directory
cat: "waldo": No such file or directory

When executed, cat sends the /etc/hosts.deny file to the stdout and report that "|", "grep", and "waldo" are not found. cat thinks those arguments are file name specifications.

One way to deal with this problem is to write the command string into a temporary executable script and executing it. GetTempPathName.ksh posted elsewhere on this site is useful for this purpose. The following shell script demonstrates this practical example:


cmd="cat /etc/hosts.deny | grep \"waldo\""

tempExec=`GetTempPathName.ksh "waldo"`
echo "${cmd}" > "${tempExec}"
chmod 700 "${tempExec}"


rm "${tempExec}"

Chieh Cheng
Tue, 27 Feb 2007 16:32:08 -0800

Add Comment | Related Links | TrackBack
Related Content

Did your message disappear? Read the Forums FAQ.

Add Comment

Spam Control | * indicates required field
Your Name: *
Remember Me!
Comment: *
File attachment is optional. Please do not attach a file to your submission unless it is relevent.
Attach File:
(20 MB Max)
Spam Protection: * Answer of 6 + 9?
Click button only once, please!


TrackBack only accepted from WebSite-X Suite web sites. Do not submit TrackBacks from other sites.

Send Ping | TrackBack URL | Spam Control

No TrackBacks yet. TrackBack can be used to link this thread to your weblog, or link your weblog to this thread. In addition, TrackBack can be used as a form of remote commenting. Rather than posting the comment directly on this thread, you can posts it on your own weblog. Then have your weblog sends a TrackBack ping to the TrackBack URL, so that your post would show up here.

Messages, files, and images copyright by respective owners.

Products | Services
Forums | Latest | RSS
Library | Search | Wiki
Help | Licenses

Login | Register

45 Users Online

Hacking Digital Cameras
Fun for Photographers

Amazon Associate

Copyright © 1996 - 2024. All Rights Reserved.