Operating System - HP-UX
1752318 Members
5979 Online
108786 Solutions
New Discussion юеВ

Passed Parameters and Spaces

 
SOLVED
Go to solution
Ian Hepples
Occasional Contributor

Passed Parameters and Spaces

Hi,

I have the following script:

echo "**************** in scriptmcr001 $#"
for p in "$@"; do
echo "scriptmcr001 P:$p"
echo $p | grep " " > /dev/null
if [ $? -ne 0 ] ; then
command=${command}" "$p
else
command=${command}" "\"$p\"
fi
done

echo "scriptmcr001 - command:$command"

The last line shows the command I want built exactly as I require, however, I can't execute the command it as one of the passed paramaters has a space, so when I execute $command it ignores everything after the space.

Adding the following lines allows the script to work:

echo $command > runme.sh
chmod 777 runme.sh
runme.sh

But in the live environment the script will be run hundreds of times, so its not possible to write the output to a file.

Is there anyway to execute $command and not have the space issue. For example if it was possible to do something like:

echo $command | exec

Then that would be ideal, but this doesn't work.

Thanks in Advance
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: Passed Parameters and Spaces

Hi:

# cmd=find
# p="/tmp -type f -mtime +7"
# cmd="${cmd} ${p}"
# ${cmd}

Notice that the word 'command' is a standard UNIX command. Hence to avoid ambiguity, I'd use something like 'cmd' as your variable name.

Regards!

...JRF...
Ian Hepples
Occasional Contributor

Re: Passed Parameters and Spaces

Thanks for the reply, I did the changes as recomended but still have the same issue. The parameter which is passed "online report" is still only showing as online when ${cmd} is executed.
James R. Ferguson
Acclaimed Contributor

Re: Passed Parameters and Spaces

Hi (again):

You need to quote your arguments if you want strings with embedded spaces to be counted as a single argument.

# cat ./showme
#!/usr/bin/sh
for p in "$@"; do
echo "${p}"
echo ${p}
done

Compare the output of:

# ./showme online report
# ./showme "online report"
# ./showme " online report"

Regards!

...JRF...
Ian Hepples
Occasional Contributor

Re: Passed Parameters and Spaces

Thanks again for replying, the input string which is passed to the script in question is quoted:

test1 xraf run srcu/test.p "online report"

In this case I am trying to send 4 parameters, yet when I receive the parameters I get 5 as it splits online and report as 2 parameters.

when I echo what I have in $cmd it is correct as it will build the string with any ammount of parameters. But when I execute $cmd, it tries to execute just the online part and ignores report.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Passed Parameters and Spaces

Hi (again) Ian:

See if this helps you:

# cat ./passme
#!/usr/bin/sh
typeset -i n=1
set -A CMD
CMD[1]=$1
shift
for p in "$@"
do
((n=n+1))
echo ${p} | grep -c ' ' && CMD[${n}]=\"${p}\" || CMD[${n}]=${p}
done
echo ${CMD[*]}
eval ${CMD[*]}

# cat ./tester
#!/usr/bin/sh
echo "I see $# parameters"
for arg in "$@"
do
echo $arg
done

...and then:

# ./passme ./tester x y z "online report"
I see 4 parameters
x
y
z
online report

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Passed Parameters and Spaces

Hi (again) Ian:

Actually, there is no need _not_ to quote every argument (except the first, which is the object to execute). Hence, simplified:

# ./cat ./passme
#!/usr/bin/sh
typeset -i n=1
set -A CMD
CMD[1]=$1
shift
for p in "$@"
do
((n=n+1))
CMD[${n}]=\"${p}\"
done
echo ${CMD[*]}
eval ${CMD[*]}

Regards!

...JRF...
Ian Hepples
Occasional Contributor

Re: Passed Parameters and Spaces

Thanks for your help, needed to do some tweaking but everything is working now.

Thanks Again