stadt@cs.utwente.nl (Richard van de Stadt) (11/30/90)
Is there some sort of variable substitution possible in a shell script to get the value of the last argument supplied to the script? I don't mean shifting the arguments until one is left. I'd like to know if something like awk's $NF, in which NF means the number of fields, and $NF means the value of the last field, is possible. ${$#} results in an error message. -- R.R. van de Stadt (Richard) Email: stadt@cs.utwente.nl
maart@cs.vu.nl (Maarten Litmaath) (12/01/90)
In article <1990Nov30.092424@cs.utwente.nl>, stadt@cs.utwente.nl (Richard van de Stadt) writes: )Is there some sort of variable substitution possible in a shell script )to get the value of the last argument supplied to the script? I don't )mean shifting the arguments until one is left. I'd like to know if something )like awk's $NF, in which NF means the number of fields, and $NF means the )value of the last field, is possible. ${$#} results in an error message. # How to get the last argument of a shell script reliably. # If you are sure there are fewer than 10 arguments, you can use this: # # eval echo \"\$$#\" # # Instead of ``echo "$foo"'' you could use the portable echo hack: # # expr "$foo" : '\(.*\)' # # Alternatively: # # cat << EOF # $foo # EOF set a b c d e f g h i j k l m -n # for example case $# in 0) last= ;; *) last=` n=$# set 0 ${1+"$@"} shift $n echo -n "$1" ` esac echo "last=|$last|" -- "Please DON'T BREAK THE CHAIN! Terry Wood broke the chain and ended up writing COBOL PROGRAMS. Three days later, he found his Blue Star Tatoo Letter, made 20 copies and mailed them out. He found a good job writing compilers." -- tjw@unix.cis.pitt.edu (Terry J. Wood)
jim@segue.segue.com (Jim Balter) (12/01/90)
In article <8393@star.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes: ># eval echo \"\$$#\" Or eval last=\$$# echo "$last"
jeff@onion.pdx.com (Jeff Beadles) (12/02/90)
In <1990Nov30.092424@cs.utwente.nl> stadt@cs.utwente.nl (Richard van de Stadt) writes: >Is there some sort of variable substitution possible in a shell script >to get the value of the last argument supplied to the script? I don't >mean shifting the arguments until one is left. I'd like to know if something >like awk's $NF, in which NF means the number of fields, and $NF means the >value of the last field, is possible. ${$#} results in an error message. Well, this will work in the bourne shell: #!/bin/sh last="`eval echo \\$$#`" echo "Last field = $last" exit 0 And, when run: % ./t jeff beadles Last field = beadles This might not be the most elegant solution, but it does work. It's even been somewhat tested, :-) -Jeff -- Jeff Beadles jeff@onion.pdx.com
maart@cs.vu.nl (Maarten Litmaath) (12/04/90)
In article <8393@star.cs.vu.nl> I wrote:
)...
)# Instead of ``echo "$foo"'' you could use the portable echo hack:
)#
)# expr "$foo" : '\(.*\)'
Better make that:
expr "xxx$foo" : 'xxx\(.*\)'
Then you won't get ``expr: syntax error'' if $foo equals `match' or `+'
or `-' or... :-(
Grrrrr!
--
Q: "Is there a newsgroup for astrology/para-normal?"
A: "Why don't you consult the stars and find out ??"
-- Phil Watson <pwatson@sunb.mqcc.mq.oz.au>
jimr@hp-lsd.COS.HP.COM (Jim Rogers) (12/05/90)
/ hp-lsd:comp.unix.questions / jim@segue.segue.com (Jim Balter) / 8:05 am Dec 1, 1990 / In article <8393@star.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes: ># eval echo \"\$$#\" Or eval last=\$$# echo "$last" ---------- for ksh, to ensure the ability to read arguments past $9: eval last=\${${#}} echo "$last" Jim Rogers Some of us deal with more than 9 arguments at a time.
maart@cs.vu.nl (Maarten Litmaath) (12/05/90)
A slight improvement of my original solution. # How to get the last argument of a shell script reliably. # If you are sure the number of arguments is in [1-9], you can use this: # # eval last=\$$# # # The general solution works for ANY number of arguments. set a b c d e f g h i j k l m -n # For example. case $# in 0) # So the user tried to be funny; let's give him a little surprise. exec kill -SYS $$ ;; [1-9]) eval last=\$$# ;; *) last=` n=$# set '' ${1+"$@"} shift $n echo -n "$1" ` esac echo "last=|$last|" -- Q: "Is there a newsgroup for astrology/para-normal?" A: "Why don't you consult the stars and find out ??" -- Phil Watson <pwatson@sunb.mqcc.mq.oz.au>
maart@cs.vu.nl (Maarten Litmaath) (12/07/90)
In article <7370019@hp-lsd.COS.HP.COM>, jimr@hp-lsd.COS.HP.COM (Jim Rogers) writes: )... )for ksh, to ensure the ability to read arguments past $9: ) ) eval last=\${${#}} ...just like in POSIX-compatible shells. BTW, the braces around the `#' are unnecessary. -- In the Bourne shell syntax tabs and spaces are equivalent almost everywhere. The exception: here documents. :-( Does anyone remember the famous mistake Makefile-novices often make?