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.
The bash WAIT command is used to halt the execution of a script until all background jobs or specified JobID/PIDs terminate successfully and return an expected exit code to trigger the next command that was “waited for.”
The bash wait command is a Shell command that waits for background running processes to complete and returns the exit status. Unlike the sleep command, which waits for a specified time, the wait command waits for all or specific background tasks to finish.
Yes, they are executed sequentially. However, if you run a program in the background, the next command in your script is executed immediately after the backgrounded command is started.
#$ does "nothing", as # is starting comment and everything behind it on the same line is ignored (with the notable exception of the "shebang"). $# prints the number of arguments passed to a shell script (like $* prints all arguments). Follow this answer to receive notifications. edited Jul 9 at 13:55.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With