How to run script b based on script a result - bash

I've got two working scripts: a.sh, b.sh.
I would like to create one script which will do the following:
1. Run a.sh
2. Running b.sh is dependant on the output of a.sh so wait for a String from a.sh standatd output saying 'a.sh launched' only then run b.sh If this is too trickyto to implement then perhaps simply wait for say 2 minutes before running the second script.
What would be the best way acheiving this?

Continuously reads output of a.sh and when it encounters "a.sh launched" it launches b.sh.
./a.sh | while read line; do
echo $line # if you want to see the output of a.sh
[ "$line" == "a.sh launched" ] && ./b.sh &
done
If you want to match
a.sh lounched at `date`
use advanced bash comparsion
[[ "$line" =~ "a.sh lounched".* ]]

Related

BASH Run multiple scripts from a another script

I have a doubt about running multiple scripts from a third one:
first.sh
#!/bin/bash
echo "script 1"
#... and also download a csv file from gdrive
second.sh
#!/bin/bash
echo "script 2"
third.awk
#!/usr/bin/awk -f
BEGIN {
print "script3"
}
I would like a 4th script that run them in order, I've tried the following but only runs the first script.
#!/bin/bash
array=( first.sh second.sh )
for i in "${array[#]}"
do
chmod +x $i
echo $i
. $i
done
But only runs the first script and nothing else.
Thank you very much for the support!
Santiago
You can't source an awk script into a shell script. Run the script instead of sourcing it.
. (aka source) executes commands from the file in the current shell, it disregards the shebang line.
What you need instead is ./, i.e. path to the script, unless . is part of your $PATH (which is usually not recommended ).
#!/bin/bash
array=( first.sh second.sh )
for i in "${array[#]}"
do
chmod +x "$i"
echo "$i"
./"$i" # <---
done
Why is the second script not running? I guess the first script contains an exit, which when sourced exits the shell, i.e. it doesn't continue running the outer wrapper.

How to use Parallel and sequential execution of bash scripts in unix

I have 3 shell scripts a.sh, b.sh and c.sh. the scripts a.sh and b.sh are to be run parallely and script c.sh should only run if a.sh and b.sh are run successfully ( with exit code 0).
Below is my code. The parallel execution is working fine but sequential execution of c.sh is out of order. It is executing after completion of a.sh and b.sh even if both the scripts are not returning exit codes 0. Below is the code used.
#!/bin/sh
A.sh &
B.sh &
wait &&
C.sh
How this can be changed to meet my requirement?
#!/bin/bash
./a.sh & a=$!
./b.sh & b=$!
if wait "$a" && wait "$b"; then
./c.sh
fi
Hey i did some testing, in my a.sh i had an exit 255, and in my b.sh i had an exit 0, only if both had an exitcode of 0 it executed c.sh.
you can try this :
.....
wait &&
if [ -z "the scripts a.sh or any commande u need " ]
then
#do something
C.sh
fi

Using while read, do Loop in bash script, to parse command line output

So I am trying to create a script that will wait for a certain string in the output from the command that's starting another script.
I am running into a problem where my script will not move past this line of code
$(source path/to/script/LOOPER >> /tmp/looplogger.txt)
I have tried almost every variation I can think of for this line
ie. (./LOOPER& >> /tmp/looplogger.txt)
bash /path/to/script/LOOPER 2>1& /tmp/looplogger.txt etc.
For Some Reason I cannot get it to run in a subshell and have the rest of the script go about its day.
I am trying to run a script from another script and access it's output then parse line by line until a certain string is found
Then once that string is found my script would kill said script (which I am aware if it is sourced then then the parent script would terminate as well).
The script that is starting looper then trying to kill it-
#!/bin/bash
# deleting contents of .txt
echo "" > /tmp/looplogger.txt
#Code cannot get past this command
$(source "/usr/bin/gcti/LOOPER" >> /tmp/ifstester.txt)
while [[ $(tail -1 /tmp/looplogger.txt) != "Kill me" ]]; do
sleep 1
echo ' in loop ' >> /tmp/looplogger.txt
done >> /tmp/looplogger.txt
echo 'Out of loop' >> looplogger.txt
#This kill command works as intended
kill -9 $(ps -ef | grep LOOPER | grep -v grep | awk '{print $2}')
echo "Looper was killed" > /tmp/looplogger.txt
I have tried using while IFS= read -r as well. for the above script. But I find it's syntax alittle confusing.
Looper Script -
./LOOPER
#!/bin/bash
# Script to test with scripts that kill & start processes
let i=0
# Infinite While Loop
while :
do
i=$((i+1))
until [ $i -gt 10 ]
do
echo "I am looping :)"
sleep 1
((i=i+1))
done
echo "Kill me"
sleep 1
done
Sorry for my very wordy question.

Why it runs different for source and sh calling in shell

My shell a.sh script like this:
#!/bin/sh
# $ret maybe from database or pipe,whatever it likes:
ret="cnt
1"
echo -e $ret
and calling in different ways produces different results:
$ sh a.sh
cnt 1
$ source a.sh
cnt
1
$
How can I get the same output under sh and source?
How can I get the same output under sh and source?
you need to quote echo. – fedorqui
thanks #fedorqui. that means echo -e "$ret" – tonylee0329
Exactly, quoting echo's argument is the way to adjust the difference between the two shells' echos.

send signal between scripts (bash)

I've a little problem, probably it's a stupid question, but I started learning bash about a week ago...
I have 2 script, a.sh and b.sh. I need to make both running constantly. b.sh should waits for a signal from a.sh
(I'm trying to explain:
a.sh and b.sh run --> a.sh sends a signal to b.sh -> b.sh traps signal, does something --> a.sh does something else and then sends another signal --> b.sh traps signal, does something --> etc.)
This is what I've tried:
a.sh:
#!/bin/bash
./b.sh &;
bpid=$!;
# do something.....
while true
do
#do something....
if [ condition ]
then
kill -SIGUSR1 $bpid;
fi
done
b.sh:
#!/bin/bash
while true
do
trap "echo I'm here;" SIGUSR1;
done
When I run a.sh I get no output from b.sh, even if I redirect the standard output to a file...
However, when I run b.sh in background from my bash shell, it seems to answer to my SIGUSR1 (sent with the same command, directly from shell) (I'm getting the right output)
What I'm missing?
EDIT:
this is a simple example that I'm trying to run:
a.sh:
#!/bin/bash
./b.sh &
lastpid=$!;
if [ "$1" == "something" ]
then
kill -SIGUSR1 $lastpid;
fi
b.sh:
#!/bin/bash
trap "echo testlog 1>temp" SIGUSR1;
while true
do
wait
done
I can't get the file "temp" when running a.sh.
However if I execute ./b.sh & and then kill -SIGUSR1 PIDOFB manually, everything working fine...
One of the possible solutions would be the next one (perhaps, it's dirty one, but it works):
a.sh:
#!/bin/bash
BPIDFILE=b.pid
echo "a.sh: started"
echo "a.sh: starting b.sh.."
./b.sh &
sleep 1
BPID=`cat $BPIDFILE`
echo "a.sh: ok; b.sh pid: $BPID"
if [ "$1" == "something" ]; then
kill -SIGUSR1 $BPID
fi
# cleaning up..
rm $BPIDFILE
echo "a.sh: quitting"
b.sh:
#!/bin/bash
BPIDFILE=b.pid
trap 'echo "got SIGUSR1" > b.log; echo "b.sh: quitting"; exit 0' SIGUSR1
echo "b.sh: started"
echo "b.sh: writing my PID to $BPIDFILE"
echo $$ > $BPIDFILE
while true; do
sleep 3
done
The idea is to simply write down a PID value from within a b (background) script and read it from the a (main) script.

Resources