- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- script doesn't execute the command but echo is oka...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2011 12:47 AM
тАО03-05-2011 12:47 AM
script doesn't execute the command but echo is okay
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
- Tags:
- IFS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2011 02:09 AM
тАО03-05-2011 02:09 AM
Re: script doesn't execute the command but echo is okay
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2011 02:22 AM
тАО03-05-2011 02:22 AM
Re: script doesn't execute the command but echo is okay
echo $CMD > tmp_cmd
chmod u+x tmp_cmd
./tmp_cmd
cg_grp=""
rm -f tmp_cmd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2011 02:35 AM
тАО03-05-2011 02:35 AM
Re: script doesn't execute the command but echo is okay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2011 02:36 AM
тАО03-05-2011 02:36 AM
Re: script doesn't execute the command but echo is okay
echo $CMD > tmp_cmd
chmod u+x tmp_cmd
./tmp_cmd
cg_grp=""
rm -f tmp_cmd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2011 02:50 AM
тАО03-05-2011 02:50 AM
Re: script doesn't execute the command but echo is okay
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2011 03:00 AM
тАО03-05-2011 03:00 AM
Re: script doesn't execute the command but echo is okay
Instead of execute $CMD try directly:
mycommand -a -d -c $cg_grp -o
Rgds.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-05-2011 06:13 AM
тАО03-05-2011 06:13 AM
Re: script doesn't execute the command but echo is okay
> 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...