The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 02:39 PM
Shell script $0 argument painulyarun UNIX for Dummies Questions & Answers 2 03-25-2008 07:32 PM
Problem when passing argument to a shell script sumesh.abraham Shell Programming and Scripting 9 12-13-2006 12:07 PM
Argument list too long - Shell error dad5119 Shell Programming and Scripting 8 03-03-2006 03:57 PM
shell script argument parsing rmjoe Shell Programming and Scripting 1 07-28-2005 04:37 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-03-2006
arunkumar_mca arunkumar_mca is offline
Registered User
  
 

Join Date: Oct 2004
Posts: 256
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.
  #2 (permalink)  
Old 10-03-2006
srikanthus2002's Avatar
srikanthus2002 srikanthus2002 is offline
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.
  #3 (permalink)  
Old 10-03-2006
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
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.
  #4 (permalink)  
Old 10-03-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,423
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.
  #5 (permalink)  
Old 10-03-2006
petebear petebear is offline
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.
  #6 (permalink)  
Old 10-03-2006
bhargav's Avatar
bhargav bhargav is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2004
Location: USA
Posts: 511
Code:
echo "$`echo $#`"
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0