error executing script in a while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting error executing script in a while loop
# 1  
Old 08-03-2010
error executing script in a while loop

Im unable to run scripts when i read each script thru a while loop. Is this way of execution thru while loop is wrong or is there any error in the script.

I get the following error msg and i use ksh.
Code:
./vftest.ksh[17]: ./add.ksh -customer 4875 -dim RD,TRND,TT,HS,MRKT,PRDC,ACV,CV,FCT: not found

contents of variable SCRIPT_LIST are
Code:
add.ksh -customer 4875 -dim RD,TRND,TT,HS,MRKT,PRDC,ACV,CV,FCT
div.ksh -customer 4875 -dim RD,TRND,TT,HS,MRKT,PRDC,ACV,CV,FCT
sub.ksh -customer 4875 -dim RD,TRND,TT,HS,MRKT,PRDC,ACV,CV,FCT

Code:
while read scrpt
                do
                        ./${scrpt} &
                        echo "Script running: ${scrpt}"
		sleep 10
                done < ${SCRIPT_LIST}

Moderator's Comments:
Mod Comment Use code tags, please.
# 2  
Old 08-03-2010
You get this error because you're passing the entire record as a single argument.

If the script names do not contain white spaces or other pathological characters, you could use eval, but it could be dangerous, because it will try to execute whatever string you pass to it:

Code:
eval "./$script &"

Another option is to assign the words (script name, options and arguments) to a different variables, something like:

Code:
while read _script _option1 _argument1 _option2 ...; do
  ./"$_script" "$_option1" "$_argument1" ... & 
done

# 3  
Old 08-04-2010
Thanks! It seems to work with eval command. Can you pls tell me how the script runs when the eval command is used?
# 4  
Old 08-04-2010
Because shell word splitting occurs before variable expansion, when you run the script like this:

Code:
./${scrpt} &

the shell tries to execute the following command:

Code:
 ./"add.ksh -customer 4875 -dim RD,TRND,TT,HS,MRKT,PRDC,ACV,CV,FCT" &

So the shell is looking for a command named add.ksh -customer 4875 ..., not for command/script named add.ksh.

The argument passed to eval is evaluated twice, in your case first the variable is expanded, than the ordinary processing is attempted: redirection, shell word splitting, variable expansion (if any variable is left) etc, so the shell sees the correct command and executes it.

As I said, it would be safer to use the second approach.
# 5  
Old 08-04-2010
Thanks much. I could use your 2nd option but the each scripts im running in the bg has different number of arguments to be passed. i.e other than these arguments
Code:
-customer 4875 -dim RD,TRND,TT,HS,MRKT,PRDC,ACV,CV,FCT

to the script there are other diff types of arguments/parameters need to be passed. Guess i could use $* in while loop to this approach.
I have this doubt: if im able to run the series of jobs in bg how can i confirm that all these series of jobs are running "only in bg" why because all these jobs are called thru the parent script
Code:
while read scrpt
                do
                        eval "./${scrpt} &"
                        echo "Script running: ${scrpt}"
		sleep 10
                done < ${SCRIPT_LIST}

Hence i guess there would be only one PID generated for this parent-script alone and not for the other scripts/jobs running inside the parent script.Pls advise.
# 6  
Old 08-04-2010
Quote:
Originally Posted by michaelrozar17
Thanks much. I could use your 2nd option but the each scripts im running in the bg has different number of arguments to be passed. i.e other than these arguments
Code:
-customer 4875 -dim RD,TRND,TT,HS,MRKT,PRDC,ACV,CV,FCT

to the script there are other diff types of arguments/parameters need to be passed. Guess i could use $* in while loop to this approach.
You could use something like this (untested):

Code:
while read; do
  set -- "$REPLY"
  "$@"
done<your_file_name

Quote:
I have this doubt: if im able to run the series of jobs in bg how can i confirm that all these series of jobs are running "only in bg" why because all these jobs are called thru the parent script
Code:
while read scrpt
                do
                        eval "./${scrpt} &"
                        echo "Script running: ${scrpt}"
        sleep 10
                done < ${SCRIPT_LIST}

Hence i guess there would be only one PID generated for this parent-script alone and not for the other scripts/jobs running inside the parent script.Pls advise.
I'm not sure I understand. If you need the pids of the background jobs:

Code:
$ cat s
printf 'echo %s\n' one two | 
  while read; do
    eval "$REPLY&"
        printf 'last pid --> %d\n' "$!"
  done
$ bash s
last pid --> 1596
one
last pid --> 3224
two

# 7  
Old 08-06-2010
In my 1st post i have this script
Code:
while read scrpt
                do
                        eval "./${scrpt} &" # running the other scripts in backgound
                        echo "Script running: ${scrpt}"
        sleep 10
                done < ${SCRIPT_LIST}

where when this script is run it runs the other scripts thru the while loop in background.These other scripts (or the list of scripts to be run) are present in variable SCRIPT_LIST.How can we find the pid's of these other scripts.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Error when executing script

Hi, I wrote this script to test if the output for DIR1 and DIR2 comes out as I want : #!/bin/bash DAY=$(date +%d) MONTH=$(date +%b) YEAR=$(date +%Y) DIR1=$($MONTH$YEAR"_Blast_BC01") DIR2=$($MONTH$YEAR"_Blast_BC15") echo $DIR1 echo $DIR2 This is the output I want for echo $DIR1 ... (12 Replies)
Discussion started by: anaigini45
12 Replies

2. UNIX for Dummies Questions & Answers

Error executing the script

I have the following script test.sh owned by dwdev account and group dwdev, the permissions on the script are as follows. -rw-r-x--- 1 dwdev dwdev 279 Sep 17 13:19 test.sh Groups: cat /etc/group | grep dwdev dwdev:x:704:dwdev dwgroup:x:725:dwdev writers:x:726:dwdev User: cat /etc/passwd |... (3 Replies)
Discussion started by: Ariean
3 Replies

3. Shell Programming and Scripting

"if" Loop not working when executing script using cron

I am facing this weird issue where the script is working fine from the command line but when I am executing it from cron though it is working fine but the "if" loop is processing else part though I know that the if part of the logic is true and ideally the loop should execute the if portion. ... (3 Replies)
Discussion started by: sk2code
3 Replies

4. Shell Programming and Scripting

Error executing script

Please delete de thread. Thanks. (10 Replies)
Discussion started by: Rodrih92
10 Replies

5. Shell Programming and Scripting

Error while executing a script

Hi Please assist. Im getting an error while execuing the script name d "cdsnd.basel.cd_new " as siiadm user. Thanks. siiadm> ls -l total 64 -rwxr-xr-x 1 siiadm sboadm 1004 Sep 17 2008 cdsnd.basel.cd -rwxr-xr-x 1 siiadm sapsys 998 Nov 16 09:14 cdsnd.basel.cd_new... (1 Reply)
Discussion started by: samsungsamsung
1 Replies

6. UNIX for Dummies Questions & Answers

Error Executing the script.

Hi , I m getting an error after executing the script. My script. Script is used to find out the date on 8 different machines(mentioned in SERVERNAMES file). I have added public key to avoid ssh password and ssh without password working fine. #!/bin/sh fn_VMFind() { Date=`ssh -t... (5 Replies)
Discussion started by: pinga123
5 Replies

7. Shell Programming and Scripting

Error while executing the below script

I am executing the below in telnet #!/usr/bin/ksh File1=simple.txt # The file to check LogFile=simple.log # The log file DelayMax=30 # Timeout delay Tolerance=2 # BEGIN ############################## while true do StampNow=$(date +%s)/60 # stamp in minutes ... (3 Replies)
Discussion started by: chinniforu2003
3 Replies

8. Shell Programming and Scripting

Error while executing a script

My script is as below : #!/bin/sh for line in `cat Results.txt` do FEILD1=`echo $line |awk -F"|" '{print $1}'` FEILD2=`echo $line |awk -F"|" '{print $2}'` FEILD3=`echo $line |awk -F"|" '{print $3}'` FEILD4=`echo $line |awk -F"|" '{print $4}'` echo "$FEILD1 $FIELD2 $FIELD3 $FIELD4" done ... (15 Replies)
Discussion started by: shwetainnani
15 Replies

9. Shell Programming and Scripting

error while executing the script

Hello I am executing the following script nawk 'NR == 1 || substr($0,63,5) ~ /H... / && \ _++ == 2 { fn && close(fn); fn = "part_" ++c; _ = 1 } { print > fn }' sample.dat When i execute as it is it is executing fine. but when i execute the whole script as a single line like below ... (2 Replies)
Discussion started by: dsdev_123
2 Replies

10. Shell Programming and Scripting

Script not executing second loop

I have a server that receives backup files from several servers. Each server has its own directory to scp their files into, some of the files are received as .tar files and need to be compressed before being dumped. When the scp of the tar file is complete a file named 'flag' is also sent to... (2 Replies)
Discussion started by: thumper
2 Replies
Login or Register to Ask a Question