Simple shell script driving me nuts
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Simple shell script driving me nuts




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Simple shell script driving me nuts  
Trent Curry


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-15-04 07: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("h
2",$1
)!eg;print(reverse("$s")."\n");'







[ Post a follow-up to this message ]



    Re: Simple shell script driving me nuts  
SM Ryan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-15-04 08: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 with
in 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.





[ Post a follow-up to this message ]



    Re: Simple shell script driving me nuts  
Matthias Czapla


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-16-04 12:34 AM

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





[ Post a follow-up to this message ]



    Re: Simple shell script driving me nuts  
Ralf Fassel


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-17-04 10: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'





[ Post a follow-up to this message ]



    Re: Simple shell script driving me nuts  
Matthias Czapla


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-17-04 10: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





[ Post a follow-up to this message ]



    Re: Simple shell script driving me nuts  
Ralf Fassel


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-17-04 11: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'





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 05:31 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register