开发者

Bash Script Calls another bash script and waits for it to complete before proceeding

开发者 https://www.devze.com 2022-12-20 01:47 出处:网络
I have 1 bash script that runs another bash script, however the first bashscript isn\'t waiting for the second one to complete before proceeding, how can I force it to wait?

I have 1 bash script that runs another bash script, however the first bashscript isn't waiting for the second one to complete before proceeding, how can I force it to wait?

For example:

#!/bin/bash
# first.sh

#call to secondary script
sh second.sh
echo "second.sh has completed"

echo "continuing with the rest of first.sh..."

The way it开发者_如何转开发 is now, it will run second.sh, and continue on, without waiting for second.sh to complete.


AS I use scheme like this in few scripts - just calling second scripts in the same shell-copy using source.

In script-1:

source script2.sh

or:

. script2.sh

So - no one command in script-1 will not be proceeded till script2.sh will end all it's tasks.

Little example.

First script:

$ cat script-1.sh
#!/bin/bash
echo "I'm sccript $0."
echo "Runnig script-2..."

source script-2.sh

echo "script-2.sh finished!"

Second script:

$ cat script-2.sh
#bin/bash
echo "I'm script-2. Running wait operation..."
sleep 2
echo "I'm ended my task."

How it works:

$ ./script-1.sh
I'm sccript ./script-1.sh.
Runnig script-2...
I'm script-2. Running wait operation...
I'm ended my task.
script-2.sh finished!


Normally it does; something else is happening. Are you sure that the other script isn't running something in the background instead? You can try using wait regardless.


You can simply add the command wait after you execute the second script, it will wait for all process that you launch from your principal script

You can even recuperate the PID of your second script using the command echo $! directly after you call the second script, and then pass this PID as an argument to the wait command


try using bash second.sh and check your second.sh and make sure you don't have programs that run in the background


Another way to do it $(second.sh)

0

精彩评论

暂无评论...
验证码 换一张
取 消