Operating System - HP-UX
1820389 Members
3864 Online
109623 Solutions
New Discussion юеВ

providing a shell script with parameters from a file

 
SOLVED
Go to solution
Maciej Szewczykowski
Frequent Advisor

providing a shell script with parameters from a file

hello all.
got a question. i've written a script which contains several variables. because i'd like to run a script each time with different variables values, the most convenient solution seems to be storing the values in a text file. is it possible that a script takes values for it's variables from a file (e.g. using '<' operator)?
thank you in advance.
6 REPLIES 6
Bill McNAMARA_1
Honored Contributor

Re: providing a shell script with parameters from a file

why don't you source the file depending on the arguments supplied:

ie

./script -op
sources file operator.env
and
./script -ad
sources file admin.env

where admin.env contains
export VARIABLE1=one
export VARIABLE2=two

etc
It works for me (tm)
Stefan Farrelly
Honored Contributor

Re: providing a shell script with parameters from a file

You store variables into a plain file like this;

VAR1=
STRING1=

Then you source this file with .
eg. put these 2 lines above into a file called VARIABLES then from your shell or from a script do;
. ./VARIABLES
This then sources the file and sets up these variables exactly as the are in the file, you can then run your script using these variables.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Graham Cameron_1
Honored Contributor

Re: providing a shell script with parameters from a file

Yes it's possible, but I wouldn't use "<" here.
Within your script, positional parameters are referenced as $1, $2 etc, with $0 the whole line.
You can run your script based on the contents of textfile, a line at a time, using the following shell command:
--
while read line
do
yourscript.sh "$line"
done < textfile
--
Note the double quotes are needed around "$line" to get the whole line into the script, without them you'd only get the first parameter, not the entire line.
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Maciej Szewczykowski
Frequent Advisor

Re: providing a shell script with parameters from a file

dear Graham,
could you please tell me what structure a text file should have?
e.g each variable in separate line?
Elmar P. Kolkman
Honored Contributor
Solution

Re: providing a shell script with parameters from a file

It depends on the variable values. For instance, if there is a formula for some of them, put that formula in your script instead of creating the parameter file by hand. Or create the parameter file with a script.

If you have a parameter file, make sure it contains all variables for one instance on one line. Then you can do it using the command read.

It will look something like this:
while read parm1 parm2 parm3 ...
do
do_actions
done < param_file

Good luck
Every problem has at least one solution. Only some solutions are harder to find.
Graham Cameron_1
Honored Contributor

Re: providing a shell script with parameters from a file

To quote the venerable A. Clay Stephenson, "it depends". In fact it's entirely up to you.
There are loads of examples on the system.

For example, have a look at
"/usr/sam/lbin/ioparser.sh" around line 474, where 19 variables are read from a single line.
--
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.