Automate the passing of parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Automate the passing of parameters
# 1  
Old 02-21-2011
Automate the passing of parameters

I am writing a script that should read the csv file and pass the values in the file as parameters
to the script. The csv file looks like this:
Code:
TEST_1,20110221
TEST_2,20110220
TEST_3,20110218,20110219

Currently this is how i am running the script
Code:
./test.sh <param1> <date>

Ex: ./test.sh TEST_1 20110221 (from the above file)
Code:
./test.sh TEST_3 20110218 20110219

Now i should automate the script by reading through the file and pass the parameters to the script.

Please advice.
Thanks in Advance!!
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 02-21-2011 at 11:57 AM.. Reason: code tags, please!
# 2  
Old 02-21-2011
Something like this:

Code:
   
        for i in `cat CSV_FILE`
        do
        file_name=`echo $i | cut -d"," -f1`
        params=`echo $i | cut -d"," -f2- | sed 's/,/ /g'`
        ./script.sh $file_name $params
        done

# 3  
Old 02-21-2011
This would be simpler and faster:
Code:
eval "$(sed -e 's/,/ /g' -e 's/^/.\/script.sh /' file.csv)"

# 4  
Old 02-21-2011
Thanks for the reply. This works absolutely fine.But i have a question.
The input file sometime contains 3 fields,
Ex: TEST_3,20100221,20100220
Can this code handle 3 fields?

Also if the fields are separated by "space" then this is how the code would look like:
Code:
for i in `cat input.txt`
        do
        file_name=`echo $i | cut -d"" -f1`
        params=`echo $i | cut -d"" -f2- | sed 's/ / /g'`
        ./yc_compute.ksh $file_name $params
        done

Thank You!!

---------- Post updated at 01:17 PM ---------- Previous update was at 12:57 PM ----------

@jlliagre: This one works fine too. Thanks for the reply!!

Last edited by Franklin52; 02-22-2011 at 03:10 AM.. Reason: Please use code tags, thank you
# 5  
Old 02-21-2011
The panyam's cat solution don't work if there is spaces in the arguments.

As there is more than one way to do it, I'll post another solution Smilie

Code:
cat test.csv | while read line; do ./test.sh $(echo $line | tr ',' ' '); done

But always beware of spaces in your arguments Smilie
# 6  
Old 02-21-2011
This should handle spaces in fields:
Code:
eval "$(sed -e "s/,/\\\" \\\"/g" -e "s/^/.\/script.sh \\\"/" -e "s/$/\\\"/" file.csv)"

# 7  
Old 02-21-2011
This issue got resolved. Thanks a lot to each one of you for your help. Really appreciate!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with passing parameters from a file

Hello Everyone, I have developed a shell script which takes schema id and password as parameter to login into database using sqlplus,runs a query and mails the result. My requirement is that, I dont want to pass userid and password as parameters.Instead,I want to pass say Environment... (4 Replies)
Discussion started by: karthik adiga
4 Replies

2. Shell Programming and Scripting

Passing Parameters to Crontab

Hello Experts, I have a requirement to pass some parameters to Linux cron tab. For ex: My default cron entry looks like this as below: ------------------------------- 55 10 * * --... (7 Replies)
Discussion started by: MaheshChaudhari
7 Replies

3. Shell Programming and Scripting

passing parameters with spaces

we are using following script to execute stored procedue. The problem is run_pmcmd.ksh script is using $* parameter which is not taking in account 'Men Shirt' parameter which includes spaces. 1. Step 1 run_pmcmd.ksh CONVERT_TEST script for run_pmcmd.ksh /u01/$(whoami)/run_pmcmd.ksh... (11 Replies)
Discussion started by: sandy162
11 Replies

4. Shell Programming and Scripting

passing parameters to the script

how can i make a script to run only when parameters are given, if parameters are not given it should through an error , saying "please enter a parameter" for ex: i want a find command to run only when the parameters are given (4 Replies)
Discussion started by: knip
4 Replies

5. UNIX for Dummies Questions & Answers

Passing the parameters through a file

Hi All, I have written a shell script and the script is working fine, I am passing my MIT_ID(NUMBER VALUE) to the shell script from the command prompt and my script is executing as expected. Now I have to pass all the MIT_ID's from a .txt file to the shell script,as I am automating this I... (6 Replies)
Discussion started by: gaur.deepti
6 Replies

6. Shell Programming and Scripting

Passing the parameters using a function

Hi All, I am new to shell scripting required some help in passing the parameter value to the shell script. I am writing a shell script, in the script I have created two functions as below. first function get_trend_ids () { Here I am connecting to the database and getting all the... (3 Replies)
Discussion started by: shruthidwh
3 Replies

7. Shell Programming and Scripting

passing parameters using awk

Hi, The below script is working fine awk1.sh ======= awk BEGIN { FS="|" } FNR==NR { f1=$2; next } $1 in f1 && $2 =="xx" && $1 == "DAILY_JOB" {print $3} awk -f awk1.sh a.txt b.txt--Its working fine . When passing parameters its not working .Any help it should be appereciated. ... (4 Replies)
Discussion started by: akil
4 Replies

8. Shell Programming and Scripting

passing more than 9 parameters

hi, i am passing around 14 parameters for a script a=$1 b=$2 c=$3 d=$4 e=$5 f=$6 g=$7 h=$8 i=\"${9}\" shift j=\"${1}\" still for j it is displaying the 1st parameter value..how to make it take the 10th parameter (2 Replies)
Discussion started by: dnat
2 Replies

9. Shell Programming and Scripting

Passing parameters through a file

I have created a script "myscript.sh" I need to run this script with diffrent parameters. The parameters are stored in a file myparam.txt. I need to run myscript.sh script for every parameter supplied in myparam.txt. Example: If myparam.txt contains following lines: param1 param2 param3... (3 Replies)
Discussion started by: chawlaaman
3 Replies

10. UNIX for Dummies Questions & Answers

Passing parameters in script

I have 2 scripts: script1 and script2 Script1 passes 4 parameters to script2 as follows #script1 code ... ... script2 $var1 $var2 $var3 $var4 Script2 uses the export command to know to expect these values #script2 export $1 $2 $3 $4 code ... ... The problem that I am having is... (1 Reply)
Discussion started by: eliguy
1 Replies
Login or Register to Ask a Question