execute script

Hi everybody:

I would like to know how I can execute a script which to execute we have to follow different steps.
I have did that this script needs some users features. These features are introduced from screem, but ussually these are equal. for this reason I would to to know if it is possible reduce these inputs into a file that user created.

For example, if ijn each run of the script, this needs the next features:

7,name1,4,file1,1
7,name2,4,file1,2
7,name3,4,file1,3
7,name4,4,file1,4
7,name5,4,file1,5
7,name6,4,file1,6
.....

where I would that in each requesting the script read this new user file and one element of each line.

I don't know if I explained correctly

Thanks advance. :wink:

OK, correct me if i got you wrong:

you want to run a script "/somewhere/script.sh" and it should get some information from a user each run. Right now you ask the user to provide this information directly, by typing it in upon request during the run of the script. Now you want to have a file where the responses for several runs of the script are stored in advance and the user doesn't have to provide them interactively anymore.

If this is what you want, this is the solution:

you first have to change your script to accept commandline parameters. You call a script giving parameters directly on the commandline. These parameters can be used inside the script: using the reserved variables $1, $2, $3, ... They will be filled with the value of the first (second, third, ...) value given on the commandline. Here is an example:

#!/bin/ksh

print - "this is the first parameter:" $1
print - "this is the second parameter:" $2
print - "this is the third parameter:" $3

exit 0

Save this file as "params.sh" and call it:

./params.sh firstparam secondparam "third param"

This will yield the following output:

# ./params.sh firstparam secondparam "third param"
this is the first parameter: firstparam
this is the second parameter: secondparam
this is the third parameter: third param
#_

As you can see whitespace will separate the aprameters, but you can overcome this by grouping them with double quotes like in the third parameter.

So, to change your script, you will use this mechanism: somewhere in your script you fill variables with user input perhaps using read-statements. Instead of this fill the variables from the commandline like in the following code fragment:

firstvar="$1"
secondvar="$2"
thirdvar="$3"
fourthvar="$4"

Now you can test your script by providing the input you previously typed in on the screen directly on the commandline as parameters.

After having done this you do the next step: creating a second script, which calls the first script once for every line you have put into the configuration file. I suppose, for the example, your script is called "/somewhere/myscript.sh" and takes 2 parameters. The configuration script is in /path/to/config.file. Change the example to suit your needs.

#!/bin/ksh

# it is always good style to declare your variables up front
typeset infile=/path/to/config.file
typeset script=/somewhere/myscript.sh
typeset param1=""
typeset param2=""

cat $infile | while read param1 param2 ; do
     $script "$param1" "$param2"
done

exit 0

This will loop through the configuration file, where the input variables are separated by blanks, feed them to the two variables param1 and param2 and pass these to the script. This is done in a loop (while .... done) which is executed once for every line there is in the configuration file. The configuration file could look like this:

firstrun1 firstrun2
secondrun1 secondrun2

In this case the loop would get executed twice and /somewqhere/myscript.sh would be called two time, equivalent to manually executing the following commands:

/somewhere/myscript.sh firstrun1 firstrun2
/somewhere/myscript.sh secondrun1 secondrun2

bakunin