The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem printing the property of xml file via shell script neeto Shell Programming and Scripting 4 04-16-2008 10:39 AM
Shell script $0 argument painulyarun UNIX for Dummies Questions & Answers 2 03-25-2008 03:32 PM
Problem when passing argument to a shell script sumesh.abraham Shell Programming and Scripting 9 12-13-2006 08:07 AM
Argument list too long - Shell error dad5119 Shell Programming and Scripting 8 03-03-2006 11:57 AM
shell script argument parsing rmjoe Shell Programming and Scripting 1 07-28-2005 12:37 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-03-2006
Registered User
 

Join Date: Oct 2004
Posts: 228
printing last argument in shell script

All,

I am having a shell script and i will pass different argument diferent time . Please tell me how can i find the last argument that i passsed each time when i exec the script.

Thanks,
Arun.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-03-2006
srikanthus2002's Avatar
Registered User
 

Join Date: Sep 2006
Location: Can u guess...!
Posts: 160
I think, you have to redirect the value of argument to file
and then echo(by command cat) it where do you want.
Reply With Quote
  #3 (permalink)  
Old 10-03-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Quote:
Originally Posted by srikanthus2002
I think, you have to redirect the value of argument to file
and then echo(by command cat) it where do you want.
why?

'man ksh' yields the following:
Code:
  Parameters Set by Shell
     The following parameters are automatically set by the shell:

     #         The number of positional parameters in decimal.
Reply With Quote
  #4 (permalink)  
Old 10-03-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
You can do something like this :
Code:
echo "Argument count = $#"
if [ $# -gt 0 ]
then
   eval last_arg=\$$#
else
   last_arg='<none>'
fi
echo "Last argument  = $last_arg"

Jean-Pierre.
Reply With Quote
  #5 (permalink)  
Old 10-03-2006
Registered User
 

Join Date: Jul 2006
Posts: 23
With bash version 3.0 or higher you can use
Code:
${BASH_ARGV[0]}
This little explanation is of courtesy Dark_Helmet over at linxquestions.org
Quote:
BASH_ARGV is a special built-in variable. It's an array of the command line arguments. More specifically, the man page says it's the arguments located on the stack. So it's safe to read them, but I strongly suggest not modifying them. To access the array, use [X] to refer to a specific argument. The arguments are put on the stack "backwards" and explains why the last argument is indexed with 0. Lastly, BASH_ARGV will change inside a function call (because the stack changes). So be careful where/how you access the variable.
Reply With Quote
  #6 (permalink)  
Old 10-03-2006
bhargav's Avatar
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 511
Code:
echo "$`echo $#`"
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0