Visit Our UNIX and Linux User Community


execute script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting execute script
# 1  
Old 10-17-2007
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. Smilie
tonet
# 2  
Old 10-17-2007
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

Previous Thread | Next Thread
Test Your Knowledge in Computers #286
Difficulty: Easy
AIX Version 1 was introduced in 1986 for the IBM RT PC workstation, was based on HP-UX.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute a script based on status of embedded script

Hi, I have 2 scripts, first.ksh and second.ksh and would like first.ksh to run based on response of 'Y/y' received for second.ksh but in my case it continues to execute 'rm /tmp/list' irrespective of response received, below are my scripts with further info, Hope I'm making sense :) Thanks! ... (1 Reply)
Discussion started by: mbak
1 Replies

2. Shell Programming and Scripting

Batch script to execute shell script in UNIX server

Hi team, My requirement is to transfer pdf files from windows machine to unix server and then from that unix server we should sftp to another server. I have completed the first part i.e From windows to using to unix server with the help of psftp.exe code: psftp user@host -pw password <... (1 Reply)
Discussion started by: bhupeshchavan
1 Replies

3. Linux

How to execute a simple select script using a shell script?

Hi team, I have two select statements and need to run them using SYSDBA user select * from temp_temp_seg_usage; select segment_name, tablespace_name, bytes/ (1024*1024) UsedMb from dba_segments where segment_name='TEMP_TEMP_SEG_USAGE'; Need to run this using a shell script say named... (1 Reply)
Discussion started by: pamsy78
1 Replies

4. Solaris

Need to execute the script with script name wihtout using ./scriptname in Solaris 10

Hi, I am using solaris 10.Is there any way to execute the script with the scriptname wihtoug using ./scriptname?Also does it varies from shell to shell.I have scripts in bash,ksh,sh shells. Example:script.sh is the script name.I need to execute the script like this script.sh instead... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

5. Programming

CGI Perl script to execute bash script- unable to create folder

Hi I have a bash script which takes parameters sh /tmp/gdg.sh -b BASE-NAME -n 1 -s /source/data -p /dest/data/archive -m ARC gdg.sh will scan the /source/data and will move the contents to /dest/data/archive after passing through some filters. Its working superb from bash I have... (0 Replies)
Discussion started by: rakeshkumar
0 Replies

6. Shell Programming and Scripting

Expect script to execute a script on a remote host

Hi, I am new to the expect scripting. I have this expect script as below : spawn ssh remote_server -l id set pass "12345" set opt "s" expect "Password:" {send "$pass\r" ; } expect "*ENTER*" {send "Enter\r"; exp_continue } expect "Please select option :" {send... (2 Replies)
Discussion started by: curt137
2 Replies

7. Shell Programming and Scripting

Dos batch script to execute unix shell script

Can anyone help me with a dos batch script to execute a shell script residing in an unix server. I am not able to use ssh. Thanks in advance (2 Replies)
Discussion started by: Shri123
2 Replies

8. Shell Programming and Scripting

execute script without displaying child script messages

Hi i have a script which is calling another script. But my child script is displaying some values. sample script. export LOG_File=/log $/Project/Scripts/emit.ksh mv inner_test.ksh /log/test.ksh echo 'Success' here emit.ksh is doing some processing .and echoing some values. what i... (2 Replies)
Discussion started by: kam786sim
2 Replies

9. Shell Programming and Scripting

Execute unix shell script to text file using the script

Hi all, I am beginner in UNIX...I want to use unix shell script to create text.file...I know how to use using by command...can anybody tell me for the script? Thanks i changed the threads title from "tex file" to "text file", because "tex" would probably be misunderstood as reference to... (4 Replies)
Discussion started by: mastercar
4 Replies

10. Shell Programming and Scripting

script execute or no execute

o hola.. Tengo un script que se ejecuta bajo una tarea del CronJOb del unix, tengo la version 11 de unix, mi script tiene un ciclo que lee unos archivos .txt luego cada uno de esos archivos debe pasar por un procedimiento almacenado el cual lo tengo almacenado en mi base de datos oracle 10g,... (4 Replies)
Discussion started by: Kespinoza97
4 Replies

Featured Tech Videos