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
Parameter problem in my script Laibro Shell Programming and Scripting 11 06-02-2008 03:51 PM
Pass parameter into script alfredo Shell Programming and Scripting 2 04-08-2008 10:40 PM
passing parameter from Shell-script to Sql-script subodhbansal Shell Programming and Scripting 0 09-21-2007 07:15 AM
Parameter to script mahabunta Shell Programming and Scripting 2 02-13-2007 05:33 PM
Pass Parameter to Another Script rvprod UNIX for Dummies Questions & Answers 4 04-05-2002 01:07 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 03-08-2004
sugarat sugarat is offline
Registered User
  
 

Join Date: Feb 2004
Posts: 4
Using last parameter within a script?

Hi All!

I cant seem to work out how to use the last parameter passed to a shell script within the script itself.

For example, if I dont know how many parameters will be passed to the script, but want to work on the last one, I know the last parameter will be $x with x being the number stored in $#

So, if I pass 4 parameters from the shell like so:

$myScript param1 param2 param3

And I want to use param three within the script, how can I do this?

Many thanks,

Adam
  #2 (permalink)  
Old 03-08-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
Try using...

eval last=$"$#"

or

eval echo $"$#"
  #3 (permalink)  
Old 03-09-2004
linuxpenguin's Avatar
linuxpenguin linuxpenguin is offline Forum Advisor  
Registered User
  
 

Join Date: May 2002
Location: India
Posts: 295
in general, u can use the $1, $2 as the positional parameter, and if you dont care about the number of parameters then just use the "shift" to pick up one by one parameter. for more on shift, check man shift.
  #4 (permalink)  
Old 03-09-2004
sugarat sugarat is offline
Registered User
  
 

Join Date: Feb 2004
Posts: 4
Quote:
Originally posted by Ygor
Try using...

eval last=$"$#"

or

eval echo $"$#"
That worked a treat, - thanks! I dont fully understand what 'eval' does but I'll try and read up some more on it.
Thanks again!
  #5 (permalink)  
Old 03-21-2009
fieldlab fieldlab is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 1
trouble for over 9 files

I realize this is a long dead post, but I found it useful..

Trouble is, it only works for me for up to 9 files. When a second digit appears it breaks, using the $"$first digit" appended by the second.

I'm using freebsd.
  #6 (permalink)  
Old 03-22-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by fieldlab View Post
I realize this is a long dead post, but I found it useful..

Trouble is, it only works for me for up to 9 files. When a second digit appears it breaks, using the $"$first digit" appended by the second.

When the index is greater than 9, you must enclose the parameter in braces:

Code:
eval "last=\${$#}"
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 10:17 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