Problem in calling a script inside a script

Hi team,

I have a script in different folder. Now i want to call that script and execute that script from that path alone.

My code is

#!/bin/bash
wname=yahoo
PATH='/opt/IBM'
wac=`/usr/bin/ls $PATH | /usr/bin/grep "$wname"`
STOP=`/usr/bin/find $PATH/$wac -type f -name "stop.sh"`
/usr/bin/sh $STOP & (here i am executing the script in background)
/usr/bin/sleep 5 
kill  (after 5 sec it need to kill the sleep process)

$! holds the PID of the last background process that was executed

1 Like

Best is to use another variable name instead of PATH.PATH is a system variable and may cause issues when using otherway.
And you need not to give "&" for background process.You may call the main script in background as well

Off topic: The $PATH environment variable is reserved for the Shell. Choose another name for your environment variable ... $SEARCH ?

If you overwrite $PATH in the manner of your script, the Shell will not be able to find any command (ls / find / grep ... whatever) from that point on.

1 Like

Not sure what's a file and what's a directory in your script and whether the string "yahoo" is the full name or part of the name of something.

What is the output from this enquiry:

file `ls /opt/IBM | grep "yahoo"`

Does the path to stop.sh ever change?

Are you sure there will always only a single stop.sh be found?

thanks aashish sharma,

I have rename PATH todifferent variable name, now its working fine.

But how to call the mail script in background ?

---------- Post updated at 08:24 AM ---------- Previous update was at 08:20 AM ----------

o/p of that command is
/opt/IBM/yahoo/stop.sh

Unless you have an unusual version of ls , that output is impossible.
Anyway we now know that "yahoo" is a directory and that there is probably only one script and that it probably does not vary its location. Therefore we don't need to search.

Can the whole script be condensed to this? :

#!/bin/bash
/usr/bin/sh /opt/IBM/yahoo/stop.sh & ; PID=$?
/usr/bin/sleep 5
# Check whether we still have any background jobs
if [ -n "`jobs`" ]
then
     kill $PID
fi
1 Like

script name will be the same but my location will differ based upon my input... that's why i made a search and find that exact file in the script.... i need to add some more stuffs also.... thanks for your solution ...