Operating System - HP-UX
1752749 Members
4911 Online
108789 Solutions
New Discussion юеВ

script doesn't execute the command but echo is okay

 
kholikt
Super Advisor

script doesn't execute the command but echo is okay

Hi,

I have tried this couple of times but still not sure why. The echo output show the command syntax is correct but if I treat the CMD as a command to execute it directly, the set -x show it has some extra character on it and this will cause the command to fail.

set -x
SERVERGROUP="myserver 1:myserver_2"
CLNID=1
IFS=:
for i in $SERVERGROUP
do
cg_grp="${cg_grp} '$i' $CLNID"
done
CMD="mycommand -a -d -c"$cg_grp" -o"
echo $CMD
$CMD


++ SERVERGROUP='myserver 1:myserver_2'
++ CLNID=1
++ IFS=:
++ for i in '$SERVERGROUP'
++ cg_grp=' '\''myserver 1'\'' 1'
++ for i in '$SERVERGROUP'
++ cg_grp=' '\''myserver 1'\'' 1 '\''myserver_2'\'' 1'
++ CMD='mycommand -a -d -c '\''myserver 1'\'' 1 '\''myserver_2'\'' 1 -o'
++ echo 'mycommand -a -d -c '\''myserver 1'\'' 1 '\''myserver_2'\'' 1 -o'
mycommand -a -d -c 'myserver 1' 1 'myserver_2' 1 -o
++ 'mycommand -a -d -c '\''myserver 1'\'' 1 '\''myserver_2'\'' 1 -o'

Correct command should be
mycommand -a -d -c 'myserver 1' 1 'myserver_2' 1 -o

abc
7 REPLIES 7
Jose Mosquera
Honored Contributor

Re: script doesn't execute the command but echo is okay

Hi,

When you try to executes $CMD. This "mycommand" is other script? Yes, then:
1.- Have enought execution permissions?
2.- Must begging with:
./mycommand...
-or-
sh mycommand...
This last one depend of shell interpreter that you are using (sh, ksh, etc).

Rgds.
kholikt
Super Advisor

Re: script doesn't execute the command but echo is okay

a quick and dirty fix of course is to put the command in a temporary file and run that file as script.

echo $CMD > tmp_cmd
chmod u+x tmp_cmd
./tmp_cmd
cg_grp=""
rm -f tmp_cmd
abc
kholikt
Super Advisor

Re: script doesn't execute the command but echo is okay

mycommand is just a binary and yes I have the permission.
abc
kholikt
Super Advisor

Re: script doesn't execute the command but echo is okay

quick and dirty I tired the following it will work

echo $CMD > tmp_cmd
chmod u+x tmp_cmd
./tmp_cmd
cg_grp=""
rm -f tmp_cmd
abc
Matti_Kurkela
Honored Contributor

Re: script doesn't execute the command but echo is okay

Looks like the "set -x" output uses single quotes to indicate expansion results, somehow. Which shell are you using to run this?

Because you're setting IFS=:, the $CMD is not expanded into command + arguments for execution in the normal way.

The normal way would be:
$0 (the command to execute) = mycommand
$1 = -a
$2 = -d
$3 = -c
$4 = 'myserver 1'1
$5 = 'myserver_2'1
$6 = -o

Because you've set IFS=:, space isn't a separator character anymore, and you're getting:
$0 = mycommand -a -d -c 'myserver 1'1 'myserver_2'1 -o

which obviously is unlikely to be found in the PATH as an executable command.

I would suggest getting rid of the custom IFS setting as soon as it is not needed any more: IFS is a classic way to shoot yourself in the foot.

set -x
SERVERGROUP="myserver 1:myserver_2"
CLNID=1
IFS=:
for i in $SERVERGROUP
do
cg_grp="${cg_grp} '$i' $CLNID"
done
unset IFS #resume standard behavior
CMD="mycommand -a -d -c"$cg_grp" -o"
echo $CMD
$CMD

My test of this modified script fragment produced this result. If I understand correctly, this is what you wanted:

+ SERVERGROUP=myserver 1:myserver_2
+ CLNID=1
+ IFS=:
+ cg_grp= 'myserver 1' 1
+ cg_grp= 'myserver 1' 1 'myserver_2' 1
+ unset IFS
+ CMD=mycommand -a -d -c 'myserver 1' 1 'myserver_2' 1 -o
+ echo mycommand -a -d -c 'myserver 1' 1 'myserver_2' 1 -o
mycommand -a -d -c 'myserver 1' 1 'myserver_2' 1 -o
+ mycommand -a -d -c 'myserver 1' 1 'myserver_2' 1 -o
ifstest.sh: 1: mycommand: not found

MK
MK
Jose Mosquera
Honored Contributor

Re: script doesn't execute the command but echo is okay

Hi,

Instead of execute $CMD try directly:
mycommand -a -d -c $cg_grp -o

Rgds.
James R. Ferguson
Acclaimed Contributor

Re: script doesn't execute the command but echo is okay

Hi:

> Matiti: IFS is a classic way to shoot yourself in the foot.

This is very true. In shell's I like to do:

...
OLDIFS=${IFS}
IFS=":"
...
IFS=${OLDIFS}
...

The default value for IFS consists of a space, a tab and a newline character.

Regards!

...JRF...