The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to execute a batch script from shell script lakshmis10 Shell Programming and Scripting 1 10-17-2007 09:43 AM
script execute or no execute Kespinoza97 Shell Programming and Scripting 4 06-23-2007 06:27 AM
Can't execute a script ssmiths001 Shell Programming and Scripting 3 06-02-2006 02:50 PM
Is it possible to execute shell script with PHP? Micz Shell Programming and Scripting 1 05-27-2004 08:52 AM
execute a script tinydejong UNIX for Dummies Questions & Answers 2 07-07-2002 11:44 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-17-2007
Registered User
 

Join Date: Apr 2005
Location: Earth
Posts: 56
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-17-2007
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,108
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:

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

Code:
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.

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

Code:
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:

Code:
/somewhere/myscript.sh firstrun1 firstrun2
/somewhere/myscript.sh secondrun1 secondrun2
bakunin
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0