You are not logged in.

#1 2007-04-10 16:03:14

adamk
Member
Registered: 2006-03-09
Posts: 36

Confused by bourne shell operation.

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

#2 2007-04-10 16:18:40

Snowman
Developer/Forum Fellow
From: Montreal, Canada
Registered: 2004-08-20
Posts: 5,212

Re: Confused by bourne shell operation.

Use:
command=$(ls -l 'this is')
echo $command

Offline

#3 2007-04-10 17:06:07

adamk
Member
Registered: 2006-03-09
Posts: 36

Re: Confused by bourne shell operation.

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

#4 2007-04-10 19:16:48

bboozzoo
Member
From: Poland
Registered: 2006-08-01
Posts: 125

Re: Confused by bourne shell operation.

use:

eval "$command"

Last edited by bboozzoo (2007-04-10 19:17:08)

Offline

#5 2007-04-10 19:29:17

adamk
Member
Registered: 2006-03-09
Posts: 36

Re: Confused by bourne shell operation.

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

#6 2007-04-10 21:05:17

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Re: Confused by bourne shell operation.

This should/would have worked also:

command=$(ls -l "this is")
echo ${command}

Last edited by crouse (2007-04-10 21:06:04)

Offline

#7 2007-04-10 21:10:37

adamk
Member
Registered: 2006-03-09
Posts: 36

Re: Confused by bourne shell operation.

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

#8 2007-04-10 22:22:11

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: Confused by bourne shell operation.

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

Board footer

Powered by FluxBB