Assign script parameters to variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign script parameters to variables
# 8  
Old 04-14-2008
Thanks for the response!!

Ok... For example, I want this script to read a file and find some values I am interested in.... But I want to create a more flexible script (if possible) and set this values that I am interested in as input....

the file is like:

var1 value1 value2 value3
var2 value1 value2 value3
var3 value1 value2 value3
var4 value1 value2 value3
.....

I want to run the script like : script.sh var2 var4 or next time script.sh var1 var2 var3 var6 or etc....

The idea is to read the number of arguments (or parameters) and save them to a variable in order to use them later.... Unfortunately, the var{i} names may not be always the same....

Is it more clear now?

How it is possible to echo the variables in the example of aigles inside the for loop?

Last edited by giorgos193; 04-14-2008 at 03:26 PM..
# 9  
Old 04-14-2008
The other common idiom for a list of random length is of course

Code:
for arg in "$@"; do
  echo another $arg
done

# 10  
Old 04-14-2008
Thanks era, this is very interesting... but can I save these arguments to different variables?
# 11  
Old 04-14-2008
Hmm, this feels a bit like going in circles. Can you explain what problem you are trying to solve?

When you start a script, its arguments are available in $1 $2 $3 $4 etc. The variable $# will tell you how many they are (you obviously already knew this) and "$@" will contain them all.

aigles' loop with the eval or the code joeyg posted will allow you to copy their values to other variables, using slightly different techniques.

If you want to use the index in the variable's name, the syntax you tried doesn't work directly, because of the way the shell parses strings. The eval command exists for situations where you want the name of a variable to include the value of another variable, for example, so it's exactly the right tool for this. It can be used if you want to echo things, too.

Code:
for ((i=1; i<=$#; ++i))
do
  eval VAR$i=\$$i
  eval echo VAR$i is \$$i
done

My comment was more of a sidetrack, in case you were really only looking for a way to loop over them all, without necessarily caring exactly how many times you loop.
# 12  
Old 04-14-2008
First of all, really, thanks for your patience....

I think your last solution will work fine...

It does what I want... it reads all the arguments... but it doesn't save them to a variable... Maybe if there was a way to do something like:

Code:
i=0
for arg in "$@"; do
  let "  i = $i + 1 "
  VAR${i} = $arg
  echo VAR${i} 
done

I want to use the different names of the arguments as variables inside the script....
# 13  
Old 04-14-2008
But it does save them to a variable. The first is VAR1, and so forth.

The syntax in your latest attempt is almost correct, but the double quotes are wrong, and of course, you still need to use "eval" like above. There's also a minor other nit regarding the let statement. But the for loop I posted above already does exactly this.
# 14  
Old 04-14-2008
Ah, yeah... I think you're right!!!!! It works fine...

Thanks a lot, for both your patience and your valuable time!!!! I really appreciate it!!

cheers!
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