Unix Programming - Simple shell script driving me nuts

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2004 > Simple shell script driving me nuts





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Simple shell script driving me nuts
Trent Curry

2004-05-15, 2:34 pm

I know I am missing something really trivial here. I've been away from
shell scripting over the years, for the most part been working with the
likes of Perl, C++, and Java, and I seem to have forgotten some aspect
of shell programming it seems thats biting me in the arse, when I wanted
to write an utterly simple script to save me some time.

sendcmd:
1 #!/bin/sh
2
3 CMD="./sendcommand blah 127.0.0.1 2701 '$*'"
4 echo -n "CMD: $*"
5 echo
6 $CMD
7 echo

Say I run it like this, the echo looks fine

$ ./sendcmd do this
CMD: do this

but sendcommand ask as if no params were passed. If I remove the ''
around $* on line 3, then it works, but I need literal single quotes
aroudn the params being pased, so to allow spaces. I wanted to use $* so
I don't have type any surrounding quotes when I use my script, and have
the script put singles around that. (Notice I didnt put any '' around
"do this" on the command line.

Thanks.

--
Trent Curry - trentcurryREM0VE@hotmail.com

perl -e
'($s=qq/e29716770256864702379602c6275605/)=~s!([0-9a-f]{2})!pack("h2",$1
)!eg;print(reverse("$s")."\n");'


SM Ryan

2004-05-15, 3:35 pm

# but sendcommand ask as if no params were passed. If I remove the ''
# around $* on line 3, then it works, but I need literal single quotes
# aroudn the params being pased, so to allow spaces. I wanted to use $* so

Propagating quotes on a shell script is a real pain in the posterior.
#!/bin/bash
set -x
echo create auxillary script
cat <<':eof' >s
#!/bin/bash
echo in auxillary script
for arg in "$@"
do
echo $arg
done
echo quit auxillary script
:eof
chmod u+rx s
echo define CMD
CMD="s ${@}"
echo $CMD
$CMD
echo try again
s "${@}"
echo quitting
yields
/ t a b c 'd e f'
+ echo create auxillary script
create auxillary script
+ cat
+ chmod u+rx s
+ echo define CMD
define CMD
+ CMD=s a b c d e f
+ echo s a b c d e f
s a b c d e f
+ s a b c d e f
in auxillary script
a
b
c
d
e
f
quit auxillary script
+ echo try again
try again
+ s a b c 'd e f'
in auxillary script
a
b
c
d e f
quit auxillary script
+ echo quitting
quitting

So that quoting work of "${@}" is lost when the string is expanded within another
string: it has to be handed along separately.

When I have a complicated quoting problem, I don't try to use shell scripts:
#!/bin/bash
#\
exec tclsh $0 "$@"
puts "creating auxillary script"
set ch [open s w]
puts $ch {#!/bin/bash
echo in auxillary script
for arg in "$@"
do
echo $arg
done
echo quit auxillary script
exit 0
}
close $ch
file attributes s -permissions u+rx
puts "define CMD"
set CMD [concat s $argv]
puts $CMD
puts [eval exec $CMD]
puts quitting
yields
/ t a b c 'd e f'
creating auxillary script
define CMD
s a b c {d e f}
in auxillary script
a
b
c
d e f
quit auxillary script
quitting

--
SM Ryan http://www.rawbw.com/~wyrmwif/
The little stoner's got a point.
Matthias Czapla

2004-05-15, 7:34 pm

Trent Curry wrote:
> 1 #!/bin/sh
> 2
> 3 CMD="./sendcommand blah 127.0.0.1 2701 '$*'"
> 4 echo -n "CMD: $*"
> 5 echo
> 6 $CMD
> 7 echo
>
> Say I run it like this, the echo looks fine
>
> $ ./sendcmd do this
> CMD: do this
>
> but sendcommand ask as if no params were passed. If I remove the ''
> around $* on line 3, then it works, but I need literal single quotes
> aroudn the params being pased, so to allow spaces. I wanted to use $* so
> I don't have type any surrounding quotes when I use my script, and have
> the script put singles around that. (Notice I didnt put any '' around
> "do this" on the command line.


I think this is caused by the shell (at least bash) not removing the
single quotes when $CMD is executed because they result from a previous
expansion (man bash -> EXPANSION -> Quote Removal).

Maybe you could do the following:

CMD="./sendcommand blah 127.0.0.1 2701"
$CMD "$*"

Regards
Matthias
Ralf Fassel

2004-05-17, 5:34 am

* "Trent Curry" <trentcurryREM0VE@hotmail.com>
| 3 CMD="./sendcommand blah 127.0.0.1 2701 '$*'"
| 4 echo -n "CMD: $*"
| 5 echo
| 6 $CMD

Try

CMD="./sendcommand blah 127.0.0.1 2701"
$CMD "$@"

Check the manpage for the difference between $* and $@.

R'
Matthias Czapla

2004-05-17, 5:34 am

Ralf Fassel wrote:
> * "Trent Curry" <trentcurryREM0VE@hotmail.com>
> | 3 CMD="./sendcommand blah 127.0.0.1 2701 '$*'"
> | 4 echo -n "CMD: $*"
> | 5 echo
> | 6 $CMD
>
> Try
>
> CMD="./sendcommand blah 127.0.0.1 2701"
> $CMD "$@"
>
> Check the manpage for the difference between $* and $@.


If I remember correctly, the OP needs to quote the parameters in order
to give them as a _single_ argument to $CMD. So "$*" would be more
appropriate.

Regards
Matthias
Ralf Fassel

2004-05-17, 6:37 am

* Matthias Czapla <derlalert@netscape.net>
| If I remember correctly, the OP needs to quote the parameters in
| order to give them as a _single_ argument to $CMD. So "$*" would be
| more appropriate.

"$@" takes care of that *if* the parameters were given as a single
parameter on the script commandline.

After re-reading the original article it seems the OP wants to treat
_all_ commandline parameters as _one_ argument, no matter how many
arguments originally were there. I would strongly recommend against
such practice in shell scripts.

If you need this to save interactive typing, code it as an shell
alias. In csh, this would be something along the line
alias shortcmd "longcmd '\!*'"
which has other disadvantages, though.

BTW, one should not forget about the backslash as a means to quote
special characters (especially spaces in file names):

echo "foo bar"
==
echo foo\ bar

Some shells automagically insert the latter when enabling file name
completion.

R'
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com