Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
04-25-2003, 01:49 AM
|
#1
|
LQ Newbie
Registered: Mar 2003
Location: Bangalore
Posts: 12
Rep:
|
a script to give me the last argument !!!
Hi
I just want a shell script which gives me the last argument passed.
For example,
if the script script.sh is executed
>script.sh one two three
it must give me "three"
Also,
>script.sh one two "three four"
must give me "three four"
Could u geeks help me ??????
thanks in advance,
rameshvl
|
|
|
04-25-2003, 02:53 AM
|
#2
|
Member
Registered: Nov 2002
Distribution: A totally 133t distro :)
Posts: 358
Rep:
|
Heres how in perl:
$rnum=$#ARGV;
print @ARGV[$rnum];
The $#ARGV just returns the number of elements in the input array of arguements. Im sure theres a comparable shell function. "man bash" would probably be a good start.
|
|
|
04-25-2003, 03:38 AM
|
#3
|
Moderator
Registered: May 2001
Posts: 29,415
|
I just want a shell script which gives me the last argument passed.
rameshvl, could you show us at least you made *some* effort trying to work it out yourself?
#All supplied args is $@, so # of args is ${#@}
|
|
|
04-25-2003, 07:39 AM
|
#4
|
LQ Newbie
Registered: Mar 2003
Location: Bangalore
Posts: 12
Original Poster
Rep:
|
Mr. unSpawn,
${#@} gives the no. of arguments ($# also gives this), but what i want is the "value" of last argument.
AND, I DID TRY before posting my problem, and I NEED NOT PROVE IT TO U.
Anyway, Im giving my code below. It is a round-about way of getting the last argument though
Here goes ...
n=$#
c=1
echo -n "LAST ARGUMENT IS "
for k in $*
do
if test $c -eq $n
then
echo $k
fi
c=`expr $c + 1`
done
This gives "three" for
>script.sh one two three
but fails for
>script.sh one two "three four"
thanks for ur suggestion anyway,
rameshvl
|
|
|
04-25-2003, 08:46 AM
|
#5
|
Moderator
Registered: May 2001
Posts: 29,415
|
AND, I DID TRY before posting my problem, and I NEED NOT PROVE IT TO U.
No need to shout, and IMNSHO, you do.
Why? It means we don't need to think for instance about stuff like this being a school project (I personally loathe contributing to those unless ppl tell it up front and match the criteria below). Showing what you did and where it went wrong is also easier cuz we don't have to reinvent the whole wheel again. Plus it's what I hope to be part of minimal netiquette, courtesy, respect or whatever you'd call it amongst LQ members.
Say args=( $@ ), then if you have $# (or ${#args[@]} in this case), then if you don't want to show the 1st 2 args, why not do
for n in $(seq 2 ${#args[@]}); do printf "%s${args[$n]}\n"; done
Btw, due to $IFS in the interactive Bash shell squishing "three four" together won't work.
|
|
|
04-25-2003, 10:18 AM
|
#6
|
Senior Member
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316
Rep:
|
Well there is a way of getting around the $IFS seperator. You could use the shift to get to the last parmeter. And then just use $1 to extract it. Something like:
#!/bin/bash
shift $(($# - 1))
echo $1
|
|
|
04-28-2003, 02:07 AM
|
#7
|
LQ Newbie
Registered: Mar 2003
Location: Bangalore
Posts: 12
Original Poster
Rep:
|
Hello
Thanks Mik, it worked !!! Can u suggest me some links where i can learn more abt these stuff ??
and Mr. unSpawn, i got pissed off for the WAY u had put it, not for anything else, I do understand ur intention, but u cud have been more polite (as being a moderator replying a poster atleast). thanx anyway.
thanx all
rameshvl
|
|
|
04-28-2003, 03:42 AM
|
#8
|
Senior Member
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316
Rep:
|
Well for bash scripting I wouldn't know anything else besides the Advanced Bash Scripting Guide. You can get that at www.tldp.org
|
|
|
04-28-2003, 07:17 AM
|
#9
|
Moderator
Registered: May 2001
Posts: 29,415
|
and Mr. unSpawn, i got pissed off for the WAY u had put it, .*but u cud have been more polite.*thanx anyway.
LOL! And I thought subtly posting it as a question saying "could" instead of "must" should do the trick...
Anyway, just posted these links in another thread.
|
|
|
12-29-2008, 07:20 AM
|
#10
|
LQ Newbie
Registered: Dec 2008
Posts: 1
Rep:
|
The best answer would be
echo ${!#}
Thanks,
S S Nayak
|
|
|
12-29-2008, 09:55 AM
|
#11
|
Senior Member
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561
Rep:
|
Quote:
Originally Posted by Mik
Well there is a way of getting around the $IFS seperator. You could use the shift to get to the last parmeter. And then just use $1 to extract it. Something like:
#!/bin/bash
shift $(($# - 1))
echo $1
|
Does it work with ps aux ?
Code:
ps aux | lastargument
|
|
|
12-29-2008, 10:07 AM
|
#12
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
You are resurrecting an almost 6 years old thread! frenchn00b, if you want to print the last field of a line passed through a pipe, just use awk '{print $NF}'.
|
|
|
12-29-2008, 11:58 AM
|
#13
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
Quote:
Originally Posted by colucix
You are resurrecting an almost 6 years old thread!
|
And for that I'm thankful. Because I'm able to learn something new from reading it.
I'm still trying to understand "${!#}" though. I've been looking through the Advanced Bash Scripting Guide, but I haven't been able to find any specific reference explaining what it does exactly. Could someone explain it to me?
|
|
|
12-29-2008, 12:15 PM
|
#14
|
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187
|
Quote:
Originally Posted by David the H.
And for that I'm thankful. Because I'm able to learn something new from reading it.
I'm still trying to understand "${!#}" though. I've been looking through the Advanced Bash Scripting Guide, but I haven't been able to find any specific reference explaining what it does exactly. Could someone explain it to me?
|
Look in info bash under "Basic Shell features -> Shell Expansions -> Shell Parameter Expansion" or in the index under "!" for details.
|
|
|
12-29-2008, 12:19 PM
|
#15
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by David the H.
I'm still trying to understand "${!#}" though. I've been looking through the Advanced Bash Scripting Guide, but I haven't been able to find any specific reference explaining what it does exactly. Could someone explain it to me?
|
To me it is simply the "new" notation for indirect referencing, introduced in Bash version 2:
instead of the classic \$$var notation. Assumed that $# is a special variable that gives the number of arguments, ${!#} is a reference to the last argument. Very similar to $NF in awk.
|
|
|
All times are GMT -5. The time now is 07:44 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|