Goal: I'd like to have a wrapper script for ssh that takes the name of
the destination host and puts it in the xterm title before running ssh
with the exact argument it's given.
Problem: ssh takes a variable number of arguments, but it's easy to
detect which argument is the host name since it's always the last
one. Well, it would be if I can figure out how to get the last member
of the argv array in a Bourne shell script. Since it is just a
wrapper, I do not want it to be deconstructing the arguments using
getopt or what-have-you.
The problem is trivially easy to solve in perl (the last argument is
$ARGV[$#ARGV]), but it seems rather extravagant to have a perl process
running every time you run ssh--the wrapper process has to stay
running throughout the ssh session because it must clean up after the
xterm title after you log off. I suppose I can invoke perl within the
script just for parsing the arguments, but that's *extremely* kludgy.
Is there any way to do this just by using Bourne shell constructs?
As you might be able to tell, I'm not that good with shell scripts-- I
usually try to do everything in perl, but the machine I'm running this
on is a bit short of memory, so I'd like to save the gratuitous
overhead of perl invocation if I can. Can anybody help?
PS Well, yes, I could do it in C if I'm really concerned about the
memory overhead. But there is something obscene about writing a C
program for what is essentially a glorified shell macro....
--
Shimpei Yamashita <http://www.patnet.caltech.edu/%7Eshimpei/>
The address I have listed in the headers is a working address. It just
looks like it's an invalid address in order to fool spammers collecting
addresses from newsgroups.
See man sh
# is the decimal number of arguments
so $# is the last one. Pretty easy, right. :-)
Often one uses the shift function to shift arguments as they are
processen in a loop until # indicates that you're done.
Chuck Demas
Needham, Mass.
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
de...@tiac.net | \___/ | http://www.tiac.net/users/demas
eval last=\$$# # sets last to value of last positional parameter
(I wish I could take credit for this cute trick. I saw it in a post of
Brian McCauley's some time ago. ) $# always holds the number of positional
parameters and eval causes the shell to evaluate its argument list, thus
in effect causing the shell to parse the argument list twice. After the
first parse the argument of eval becomes last=$n where n is the number
of parameters. When this is evaluated by the shell it does the right thing.
In some versions of the shell this will only work if $# < 10.
--
************************************************************************
Terry R. McConnell Mathematics/304B Carnegie/Syracuse, N.Y. 13244-1150
trmc...@syr.edu http://barnyard.syr.edu/~tmc
************************************************************************
Well, like I had mentioned, I *didn't* want to destroy the arg list
using shift because taking apart a list and reconstructing it just
because I wanted the last member of that list seemed very
wasteful. But since sh doesn't have arrays, I guess there isn't much I
can do. Oh well. I guess perl isn't so bad after all.
Just iterate through the arguments with `for' (which is
non-destructive).
#!/bin/sh
for last
do :; done
echo "args = $*"
echo "last = $last"
Regards,
--
Brendan O'Dea b...@compusol.com.au
Compusol Pty. Limited (NSW, Australia) +61 2 9809 0133
$# will give you the index of the last argument.
if you want to print the last argument, heres how :
for i in $*
do
dummyvar=dummy; #some dummy statement.
done
echo $i
Hope this helps.
------
Preeti Gowaikar e-mail : gpr...@cse.iitb.ernet.in
IIT, Bombay.
:
: Chuck Demas
: Needham, Mass.
:
:
: >Goal: I'd like to have a wrapper script for ssh that takes the name of
: >the destination host and puts it in the xterm title before running ssh
: >with the exact argument it's given.
: >
: >Problem: ssh takes a variable number of arguments, but it's easy to
: >detect which argument is the host name since it's always the last
: >one. Well, it would be if I can figure out how to get the last member
: >of the argv array in a Bourne shell script. Since it is just a
: >wrapper, I do not want it to be deconstructing the arguments using
: >getopt or what-have-you.
: >
: >The problem is trivially easy to solve in perl (the last argument is
: >$ARGV[$#ARGV]), but it seems rather extravagant to have a perl process
.......
: >on is a bit short of memory, so I'd like to save the gratuitous
: >overhead of perl invocation if I can. Can anybody help?
: >
: >PS Well, yes, I could do it in C if I'm really concerned about the
: >memory overhead. But there is something obscene about writing a C
: >program for what is essentially a glorified shell macro....
: >
: >--
: >Shimpei Yamashita <http://www.patnet.caltech.edu/%7Eshimpei/>
: >The address I have listed in the headers is a working address. It just
: >looks like it's an invalid address in order to fool spammers collecting
: >addresses from newsgroups.
:
:
: --
: $# will give you the index of the last argument.
: if you want to print the last argument, heres how :
: for i in $*
: do
: dummyvar=dummy; #some dummy statement.
FYI ":" (the NOP command) is the dummy statement you were looking
for. If you are examining the command line parameters, this
rather odd syntax works:
for last do :;done
echo $last
: done
: echo $i
--
Dan Mercer
Reply To: dame...@mmm.com
Opinions expressed herein are my own and may not represent those of my employer.