LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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


Reply
  Search this Thread
Old 04-25-2003, 01:49 AM   #1
rameshvl
LQ Newbie
 
Registered: Mar 2003
Location: Bangalore
Posts: 12

Rep: Reputation: 0
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
 
Old 04-25-2003, 02:53 AM   #2
m0rl0ck
Member
 
Registered: Nov 2002
Distribution: A totally 133t distro :)
Posts: 358

Rep: Reputation: 31
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.
 
Old 04-25-2003, 03:38 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603
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 ${#@}
 
Old 04-25-2003, 07:39 AM   #4
rameshvl
LQ Newbie
 
Registered: Mar 2003
Location: Bangalore
Posts: 12

Original Poster
Rep: Reputation: 0
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
 
Old 04-25-2003, 08:46 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603
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.
 
Old 04-25-2003, 10:18 AM   #6
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
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
 
Old 04-28-2003, 02:07 AM   #7
rameshvl
LQ Newbie
 
Registered: Mar 2003
Location: Bangalore
Posts: 12

Original Poster
Rep: Reputation: 0
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
 
Old 04-28-2003, 03:42 AM   #8
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
Well for bash scripting I wouldn't know anything else besides the Advanced Bash Scripting Guide. You can get that at www.tldp.org
 
Old 04-28-2003, 07:17 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603
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.
 
Old 12-29-2008, 07:20 AM   #10
nayakss
LQ Newbie
 
Registered: Dec 2008
Posts: 1

Rep: Reputation: 0
The best answer would be

echo ${!#}

Thanks,
S S Nayak
 
Old 12-29-2008, 09:55 AM   #11
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Rep: Reputation: 57
Quote:
Originally Posted by Mik View Post
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
 
Old 12-29-2008, 10:07 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
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}'.
 
Old 12-29-2008, 11:58 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by colucix View Post
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?
 
Old 12-29-2008, 12:15 PM   #14
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by David the H. View Post
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.
 
Old 12-29-2008, 12:19 PM   #15
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
Quote:
Originally Posted by David the H. View Post
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:
Code:
${!variable}
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP pass argument to shell script monzter Programming 2 08-14-2004 07:16 AM
How to count number of argument received by a script? philipina Linux - General 2 07-05-2004 03:35 AM
Script Problem - Does not give expected results Jose Muņiz Linux - General 3 07-12-2003 11:10 PM
PHP Script argument passing error... lokee Linux - Software 5 04-24-2003 10:42 AM
Is it possible to give MSDOS commands in Unix script??? gopi_20us Programming 1 02-20-2002 01:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration