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
Assign Values to variables from a text file sarsani Shell Programming and Scripting 3 06-26-2009 07:05 PM
Awk/shell question: Read from file and assign to variables. akbar Shell Programming and Scripting 3 05-07-2008 06:10 PM
how do u assign the values to different variables when it is presneted in one line?? SwetaShah High Level Programming 2 10-24-2005 09:56 PM
Assign variables with cut douknownam Shell Programming and Scripting 2 09-27-2004 09:18 AM
declare, assign variables using array, counter, loop egkumpe Shell Programming and Scripting 3 08-09-2004 10:56 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-14-2008
giorgos193 giorgos193 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 17
Assign script parameters to variables

Hi,

I have a bash script that accepts some parameters as input, like:

sh script.sh first second third .....

I want to save these parameters as different variables, something like:
VAR1=first
VAR2=second
etc

I tried this, but apparently it didn't worked....
------------------------------
for ((i=1;i<=$#;i=i+1))
do
VAR${i} = $"${i}"
done
------------------------------

I hope I made myself clear....
Thank you in advance.
  #2 (permalink)  
Old 04-14-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
Code:
for ((i=1;i<=$#;i++))
do
   eval VAR$i="\$$i"
done
Jean-Pierre.
  #3 (permalink)  
Old 04-14-2008
giorgos193 giorgos193 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 17
Thanks a lot... It looks to work... but how can I call these different variables?

echo $"VAR$i" (???????)


It's a trivial question, but it would be appreciated if you can help on this...

Thanks again, for your help!
  #4 (permalink)  
Old 04-14-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Hmm, you named them in the first place? The first is $VAR1 and so forth.
  #5 (permalink)  
Old 04-14-2008
giorgos193 giorgos193 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 17
thanks for the reply!

Yes, obsiously, this could be a way.... but can I, for example, to call them inside the for loop?

Because the number of arguments is not known apriori...
  #6 (permalink)  
Old 04-14-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
You need another eval for that. The rest should come easily.
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 09:07 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