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....
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..
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.
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.
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)