0

I have a script scriptA.sh that if a variable assume a certain value, it has to execute another script scriptB.sh that execute something and then call a scriptA.sh, that will call a scriptB.sh and so on.

I draw the execution:

ScriptA - ScriptB         
   |
   |
   _
            |
            _
   |
   |
   _
            |
            _

I have tried in this way but process are never closed

ScriptA.sh
./scriptB.sh &
exit

ScriptB.sh
./scriptA.sh &
exit

And also this way but process are never closed

ScriptA.sh
./scriptB.sh && exit

ScriptB.sh
./scriptA.sh && exit

Any suggestion?

The actual scripts:

scriptA.sh:

#!/bin/bash
val=123
directory_path=`pwd`
script_name=$0
if [ $val -eq 123 ];then
  echo "call B and exit"
  ./scriptB.sh $directory_path $script_name && exit 0
fi

scriptB.sh:

#!/bin/bash
directory_path=$1
exec_command=$2
cd
cd $directory_path
echo "call A and exit"
$exec_command && exit 0
5
  • You don't have a proper end condition on the recursion? Commented Jan 30, 2017 at 12:22
  • 1
    can you add the actual bash-code to your question? Commented Jan 30, 2017 at 13:13
  • @Kusalananda no I haven't a proper end condition: ScriptA is in a while true loop and if a value is over the threshold then ScriptA call ScriptB and terminate. The ScriptB execute, then call ScriptA and terminate. Commented Jan 30, 2017 at 13:21
  • @MichaelD. scriptA.sh #!/bin/bash val=123 directory_path=`pwd` script_name=$0 if [ $val -eq 123 ];then echo "call B and exit" ./scriptB.sh $directory_path $script_name && exit 0 fi scriptB.sh #!/bin/bash directory_path=$1 exec_command=$2 cd cd $directory_path echo "call A and exit" $exec_command && exit 0 Commented Jan 30, 2017 at 13:25
  • nice endless loop. exit will never be called Commented Jan 30, 2017 at 13:36

1 Answer 1

0

Update: To keep your structure, you might create these two files:

scripta.sh

#!/bin/bash
./controller.sh a

scriptb.sh

#!/bin/bash
./controller.sh b

Here is a suggestion to make it easier:

controller.sh

#!/bin/bash
# run   ./controller.sh a   to run a/b
# run   ./controller.sh b   to run b/a
if [ "$1" == "a" ]; then
  ./ascript.sh      
  ./bscript.sh

elif [ "$1" == "b" ]; then
  ./bscript.sh
  ./ascript.sh
else
  echo "choose a or b ./controller.sh [a|b]"
  exit 255
fi
exit 0

ascript.sh

#!/bin/bash
echo "script a"
# add code here

bscript.sh

#!/bin/bash
echo "script b"
# add code here
2
  • Thank you for the answer but this isn't useful for me because I can't change the script structure with the insertion of a controller... Commented Jan 30, 2017 at 14:18
  • edited/updated my answer: "Do One Thing and Do It Well" and "Keep it Simple, Stupid." are some *nix rules which you might want to follow. Commented Jan 30, 2017 at 14:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.