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,

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

Option 1:

$ cat parameters.txt
env1 value1
env1 value2
env2 value3
env3 value4
env1 value5
env3 value6
env2 value7
while read x y
do
    ./shscript $x $y
done < parameters.txt

Option 2:

$ 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
1 Like
#!/bin/bash

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

thanks,
venkat

1 Like

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.

Host = servername
Port = 13122
site = sitename
Javahome = /bin/java
platform= pb
filelist=x.guiclient.download
boot=bot.jar
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.

. ./parameter_file_name

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

1 Like

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:

#!/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

I don't understand the problem. Variables you set in the external file the way methyl suggests can indeed be used in your program.

I am sorry. May be i am not making myself clear here.

I have the following in a file called envfile.sh :

Host="servername"
Port="13122"
site="sitename"
Javahome="/bin/java"
platform="pb"
filelist="x.guiclient.download"
boot="bot.jar"

Then i am executing the following script that is setting the env variable as corona suggessted. But i am getting an error that boils down to the missing the hostname. What am i missing. How do i call the variables from the envfile.sh?

 #!/bin/sh
. ./envfile.sh
java -cp $boot -Djava.security.policy=$policy-codebase=http://$Host:$Port/$Filelist   /Sitename:$Site /Platform_name:$platform

Leading question:
What is in the variable $policy ?

Apologies methyl.

The envfile.sh does contain the variable policy

Host="servername"
Port="13122"
site="sitename"
Javahome="/bin/java"
platform="pb"
policy="jv.policy"
filelist="x.guiclient.download"
boot="bot.jar"

Try sticking an echo in front of the java line just to see what the expanded line looks like?

Should be

Site="sitename"
1 Like

This is the error that i get :

---------- ATTACHED CAUSE ----------
java.lang.Exception: XmlServer does not exist in site mxeod

The script executes if i define all the variables inside the script(without the double quotes). But if i move the variables to the envfile.sh and add the double quotes and then call the file using . ./envfile.sh , i am getting an error.

Thank you very much for your time Methyl. it is very much appreciated.

There is no difference between having them in a file and having them in your file, ergo there is a typo.

Methyl identified it above for you.

1 Like

Thanks a mil Methyl. I found the error and fixed it. The envfile.sh was pointing to a different host than it was supposed to. Thanks for your time
---------- Post updated at 06:07 PM ---------- Previous update was at 05:57 PM ----------

Cheers.. I looked it up and it seems really cool. I'm loving the idea of it.

try this

xargs -i sh -c './shscript {}' < parameters.txt
1 Like

I have run into problem.
I am using the following code to read from parameters.txt

while read x y
do
    ./shscript $x $y
done < parameters.txt
$ cat parameters.txt
env value1
env value2
env value3
env value4
env value5
env value6
env value7
env value8

This works perfectly.But if i make a small tweak to the code and include a if condition inside the while loop, only the alternate arguments are being read. For e.g, if i use the following code:

while read x y
do
    ./shscript $x $y
if [$y -gt 0 ] ; then
 echo The execution was successful
else
 echo Execution failed
fi
done < parameters.txt

The arguments that are passed to the shell are as follows

./shscript env value1
./shscript env value3
./shscript env value5
./shscript env value7

Can someone tell me why the alternate arguments are being missed? Thanks

Typo in this line. Should be a space character after the [ character.
Also, this is an integer comparison. Is $y and integer?

if [ "$z" == "MX" ] ; then

I use the code above and the condition works.

I tried inserting a newline character in the file paramers.txt as below and the program works perfectly. I dont use any breaks in my if.. else command.. I dont want to have to insert a new line in the parameters.txt file. I cant see where the logic is going wrong. :wall:

env value1
 
env value2
 
env value3
 
env value4
 
env value5
 
env value6
 
env value7
 
env value8

The following is my code:

while read x y z
do
if [ "$z" == "GOD" ] ; then
./shell.sh $x $y
a= parse $y
echo bla bla
if [ $a -gt 0 ] ; then
echo "`read -p "user i/p"`"
else
echo Executing the value............
fi
fi
if [ "$z" == "DEVIL" ] ; then
./ggmu.sh
fi
done<parameters.txt

If i remove the bolded out code above, the script reads the arguments properly!! Why is that if condition screwing up the reading of the parameters.txt file?

It's not the "if" condition, its the "read -p" (which was not mentioned in your previous post). It's in backticks so it still functions within the "echo" line.
There is only one STDIN channel and that "read -p" is eating data from you main read loop. Every alternate record. QED.

Ps. Please take care to post the script and data which matches the symptoms.

1 Like