Assign script parameters to variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign script parameters to variables
# 1  
Old 04-14-2008
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  
Old 04-14-2008
Code:
for ((i=1;i<=$#;i++))
do
   eval VAR$i="\$$i"
done

Jean-Pierre.
# 3  
Old 04-14-2008
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  
Old 04-14-2008
Hmm, you named them in the first place? The first is $VAR1 and so forth.
# 5  
Old 04-14-2008
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  
Old 04-14-2008
You need another eval for that. The rest should come easily.
# 7  
Old 04-14-2008
Hammer & Screwdriver Real-world programming example

This is from the beginning of one of my scripts to find a zip file within a folder, and process the work for the job. This example has three parameters, and errors when it does not see three.

Code:
# verify correct number of parameters
if [ "$#" -ne 3 ]
   then
      echo "Incorrect number of arguments"
      echo "command jobnum zipfile fileloc -- examples are"
      echo "pcs211fid 51248 myzip.zip drop1  (assumes raw folder inside)"
      echo "pcs211fid 51248 myzip.zip raw"
      echo " "
      exit
fi

# read the parameters
jobn="$1"
zipf="$2"
locf="$3"
echo "Beginning process on job" $jobn "with zipfile" $zipf

Your last comment about not knowing how many variables... then they do not match up to specific functions as in my example? Are they a list of items to process against? I cannot imagine a realworld example of what you describe - thus cannot show sample code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

2. Shell Programming and Scripting

Find the year and assign to different parameters

Hi All, I have a unix directory and under which the below set of files(Years will change) will be there ##################################### TEST_DETAIL_HCR_ABC2015_T01152015.csv TEST_DETAIL_HCR_ABC2014_T01152015.csv TEST_DETAIL_HCA_ABC2013_T01152015.csv I need to assign years to... (2 Replies)
Discussion started by: weknowd
2 Replies

3. Shell Programming and Scripting

Assign Unknown Values to Variables

i have a program that spits out a certain number of values. i dont know the number of values. they can be 4, 10, 7, 20, no idea. but, i want to be able to assign each of the value returned by this program to a variable. in the latest instance, the program gave the following 6 values: 4... (8 Replies)
Discussion started by: SkySmart
8 Replies

4. Shell Programming and Scripting

A better way to assign values to variables - shell

so i've been used to doing it this way: SVAL=$(echo "7 3 2 38 3" | awk '{print $2}') 4VAL=$(echo "4:21:N:3" | awk -F":" '{print $4}') I know there's a way to do it by putting the value in an array and assigning it that way. but i'm not sure how to do it efficiently. any ideas? i dont... (9 Replies)
Discussion started by: SkySmart
9 Replies

5. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

6. Shell Programming and Scripting

match and assign variables

Hi guys, I'm currently writing a script for automating a FreeBSD ZFS setup (ZFSonRooT). I got stuck at one point for raidz(1,2 a.k.a raid5,6) and am in need of assistance. This is what I need. example: #!/bin/sh <- must stay sh echo -n "hdd list: " read hdd_list echo -n "hdd label list:... (2 Replies)
Discussion started by: da1
2 Replies

7. Shell Programming and Scripting

Assign value to external variables from awk

Hello I have a text file with the next pattern Name,Year,Grade1,Grade2,Grade3 Name,Year,Grade1,Grade2,Grade3 Name,Year,Grade1,Grade2,Grade3 I want to assign to external variables the grades using the awk method. After i read the file line by line in order to get the grades i use this ... (2 Replies)
Discussion started by: Spleshmen
2 Replies

8. Shell Programming and Scripting

Assign values to variables of a file

Hi, I have a file like the following... CUST= DIR= NULIST= name=philps_123 How can i add values to each of these unassigned variables using a shell script? say for eg: i have values for CUST as onida, dir as /dir/onida, NULIST as /tmp/onida_files. How can i add these values to... (11 Replies)
Discussion started by: Tuxidow
11 Replies

9. Shell Programming and Scripting

How do I assign values to variables made in a script?

How do I assign values to variables made in a script? e.g. for ((x=0;x<=5;i+=1)); do Xm$i=$var done (0 Replies)
Discussion started by: gelitini
0 Replies

10. Shell Programming and Scripting

Assign variables with cut

I need to read a file (a list) and assign the value to a variable (for each line), I'm looping until the end of the file. My problem is, I want to assign 2 separate variables from the list. The process I'm using is: awk '{print $3}' file1 > file2 awk '{print $4}' file1 > file3 cat file2... (2 Replies)
Discussion started by: douknownam
2 Replies
Login or Register to Ask a Question