You are not logged in.
I was trying to make a script that did a few things and then executed a command with the rest of
the arguments passed to the script. However I'm having a problem with spaces in command arguments.
This script demonstrates the problem. The first part tries to execute a command as a string.
However it fails (there is a file 'this is' in the current directory.) The second part directly
executes the same command and it succeeds. How can I make something like the command
built up from arguments, to work as the second part does ?
The script is this:
#!/bin/sh
command="ls -l 'this is'"
echo "this fails"
$command
echo "this passes"
ls -l 'this is'
The output is:
S% doit
this fails
ls: 'this: No such file or directory
ls: is': No such file or directory
this passes
-rw-rw-r-- 1 krolnik zsp 0 Apr 10 10:58 this is
Thanks.
Adam Krolnik
Co-author "Assertion-Based Design"
Offline
Use:
command=$(ls -l 'this is')
echo $command
Offline
This suggestion $(ls -l 'this is') won't work.
Here's the context:
% pre_cmd_script <the command to be executed>
This script should capture the arguments, as the command line to be executed, in the original form
(with quoting, etc.)
Then it should set a few things and then execute the command...
E.g.
----------------------------------------
# save the command
command=$1; shift
for cmd in "$@"; do
oneword=`perl -e 'print shift;' -- $cmd`
if [ "$oneword" = "$cmd" ]; then
command="$command $cmd"
else
command="$command '$cmd'"
fi
done
# do some stuff...
NEWVAR=true
# Execute the command now.
$command
------------------------------------
But it seems that this doesn't work for a simple command of "ls -l 'this is'"
Adam Krolnik
Co-author "Assertion-Based Design"
Offline
use:
eval "$command"
Last edited by bboozzoo (2007-04-10 19:17:08)
Offline
Thanks... The eval did it.
Adam Krolnik
Director of Design Verification
Verisilicon, Inc.
Plano TX. 75074
Co-author "Assertion-Based Design"
Adam Krolnik
Co-author "Assertion-Based Design"
Offline
This should/would have worked also:
command=$(ls -l "this is")
echo ${command}
Last edited by crouse (2007-04-10 21:06:04)
Offline
It does, but the "this is" is not within the script, but something that comes in as an arguments.
Thanks.
Adam Krolnik
Co-author "Assertion-Based Design"
Offline
so... then it would be
command=$1 # Command *must* be quoted for $1 to work, otherwise use $*
output=$($command)
and that would get the output in a variable..... is that what you need?
It might be easier if you explain what you want to happen.
James
Offline