Reading arguments for a shell script from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading arguments for a shell script from file
# 1  
Old 01-11-2012
Reading arguments for a shell script from file

I have a shell script that takes 2 arguments. I will have to execute this script multiple times with different values for the arguments.

for example,
Code:
./shscript env1 value1
./shscript env1 value2
./shscript env2 value3
./shscript env3 value4
./shscript env1 value5
./shscript env3 value6
./shscript env2 value7
.
.
./shscript env2 valuen

I want to put the above in a master shell and i want to put the arguments in another file.

I would like my shell to read the arguments from the file IN ORDER and then execute the ./shscript. It is important that the arguments are read in order.

Can someone please tell me how this is done? thanks in advance
# 2  
Old 01-11-2012
Option 1:
Code:
$ cat parameters.txt
env1 value1
env1 value2
env2 value3
env3 value4
env1 value5
env3 value6
env2 value7

Code:
while read x y
do
    ./shscript $x $y
done < parameters.txt

Option 2:
Code:
$ cat master.sh
./shscript env1 value1
./shscript env1 value2
./shscript env2 value3
./shscript env3 value4
./shscript env1 value5
./shscript env3 value6
./shscript env2 value7
$
$ ./master.sh

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 01-11-2012
Code:
#!/bin/bash

for x  in `cat parameter.config`
do
  sh script.sh $x 
done

thanks,
venkat

Last edited by Franklin52; 01-11-2012 at 07:23 AM.. Reason: Please use code tags for code and data samples, thank you
This User Gave Thanks to venkatareddy For This Post:
# 4  
Old 01-11-2012
Quote:
Originally Posted by balajesuri
Option 1:
Code:
$ cat parameters.txt
env1 value1
env1 value2
env2 value3
env3 value4
env1 value5
env3 value6
env2 value7

Code:
while read x y
do
    ./shscript $x $y
done < parameters.txt

Thanks lads.. That worked a charm.
I have another question. The same master all the environment variables in a file and then read the file from my shell. How do i go about calling the different variables from the one file. Thanks in advance guys

The following is the sample of the environment variables.
Code:
Host = servername
Port = 13122
site = sitename
Javahome = /bin/java
platform= pb
filelist=x.guiclient.download
boot=bot.jar


Last edited by goddevil; 01-11-2012 at 11:30 AM..
# 5  
Old 01-11-2012
Code:
Host="servername"
Port="13122"
site="sitename"
Javahome="/bin/java"
platform="pb"
filelist="x.guiclient.download"
boot="bot.jar"

If you hold your parameter file in correct unix Shell format (no spaces either side of the equals sign and variables in quotes) you can set all the variables with.

Code:
. ./parameter_file_name

That is dot-space-dot-slash-filename.
This User Gave Thanks to methyl For This Post:
# 6  
Old 01-11-2012
Quote:
Originally Posted by methyl
Code:
Host="servername"
Port="13122"
site="sitename"
Javahome="/bin/java"
platform="pb"
filelist="x.guiclient.download"
boot="bot.jar"

If you hold your parameter file in correct unix Shell format (no spaces either side of the equals sign and variables in quotes) you can set all the variables with.

Code:
. ./parameter_file_name

That is dot-space-dot-slash-filename.
Cheers Methyl.

I understand that i will be able to set the variables by using . ./parameter_file_name

But i will also need some of the variables further down in the script. For example:

Code:
#!/bin/sh
Host=servername
Port=13122
site=sitename
Javahome=/bin/java
policy=jv.policy
platform=pb
filelist=x.guiclient.download
boot=bot.jar
.
.
.
java -cp $boot -Djava.security.policy=$policy-codebase=http://$Host:$Port/$Filelist   /Sitename:$Site /Platform_name:$platform

I am not great with awk / sed so any help would be greatly appreciated.

I have other requirements to read from a file as well(i am trying to get rid of all hardcoded data) and this will help me out greatly with respect to others as well. Thanks
# 7  
Old 01-11-2012
I don't understand the problem. Variables you set in the external file the way methyl suggests can indeed be used in your program.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing Arguments to shell script from file is not working as expected.

Hi All, I have below simple shell script in cloudera quick start vm cenos 6 which copy file from source to destination. # file_copy.sh source_dir = ${source_dir} target = ${target_dir} cp source_dir target and my parameter file is like below #parameter_file.txt source_dir =... (4 Replies)
Discussion started by: Narasimhasss
4 Replies

2. UNIX for Beginners Questions & Answers

Shell script file check throws [: too many arguments

While I am trying to check the filename/s in IF statement of a shell script (RedHat Linux 6) I am getting below error: File check: filename_time2=`date --date='yesterday' +%Y-%m-%d` cd /location/of/the/files/to/copy if then cp server.log-$filename_time2* ../archive/new... (5 Replies)
Discussion started by: Dip
5 Replies

3. Shell Programming and Scripting

php file unable to run shell script with arguments

echo $result=exec("./permit.sh".$_FILES); pls suggest some other method to run shell script in php .:wall::mad: (0 Replies)
Discussion started by: upvan111
0 Replies

4. Shell Programming and Scripting

Error while reading from a file in shell script

Hi All, I'm writing a script to read a file line by line and then perform awk function on it. I am getting an error . My file has one name in it "James". I'm expecting my o/p to be youareJamesbond James ./users.sh: line 7: =: command not found #script to read file line by line #adding... (5 Replies)
Discussion started by: Irishboy24
5 Replies

5. Shell Programming and Scripting

send arguments to a .exe file from a shell script

Folks , can anyone post a sample showing a way to parse a variable containing a string to a .exe file . Thanks Venu (2 Replies)
Discussion started by: venu
2 Replies

6. Shell Programming and Scripting

How to pass arguments to SQL file passed in shell script?

Hi, I am using SYBASE database. in my script i am connecting to DB via using isql. isql -U${S_USER} -S${S_SERV} -D${S_DB} -P${S_PWD} -b0 -w3000 -h0 -s"|" -i${MYDIR}/ABC.sql -oXYZ.txt << FINSQL i am taking a ABC.sql file to use the queries written in it and storing the output in... (3 Replies)
Discussion started by: dazdseg
3 Replies

7. Shell Programming and Scripting

file reading through shell script

For reading a file through shell script I am using yhe code : while read line do echo $line done<data.txt It reads all the line of that file data.txt. Content of data.txt looks like: code=y sql=y total no of sql files=4 a.sql b.sql c.sql d.sql cpp=n c=y total no of c files=1 (4 Replies)
Discussion started by: Dip
4 Replies

8. Solaris

Passing arguments to a shell script from file while scheduling in cron

Hi, I have a shell script Scp_1.sh for which I have to pass 2 arguments to run. I have another script Scp_2.sh which in turns calls script Scp_1.sh inside. How do I make Scp_1.sh script to read arguments automatically from a file, while running Scp_2.sh? -- Weblogic Support (4 Replies)
Discussion started by: weblogicsupport
4 Replies

9. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies

10. UNIX for Dummies Questions & Answers

Reading runtime arguments from a file

Hi All, I am running a script which wud take a long time to complete execution ... In between, it asks for yes/no sort of confirmation from user for doing some acivities .... Now that I dont want to wait till the script executes upto this point where it needs user confirmation, is there any way... (4 Replies)
Discussion started by: Sabari Nath S
4 Replies
Login or Register to Ask a Question