Bash script call another script

#! /bin/kshecho "prova". ./second.shecho "ho lanciato il secondo". ./third.shecho "ho lanciato il terzo"second.sh:

echo "sono nel seconcarry out script"dosomething1exit $?If second.sh detects an error and also exits through condition -9, initially.sh exits constantly.How I have the right to do stop exiting from the initially shell if the boy shell exits?

I can"t modify second.sh.

You watching: Bash script call another script


*

*

What you"re doing here is including second.sh and 3rd.sh as sub-scripts running in the same procedure, which is dubbed “sourcing” in shell programming. . ./second.sh is basically indistinguishable to including the text of second.sh at that allude. The departure command exits the process, it does not matter whether you contact it in the original script or in a sourced script.

If all you desire to do is run the commands in second.sh and also third.sh and also they don"t have to accessibility or modify variables and attributes from the original script, speak to these scripts as son processes.

#! /bin/kshecho "prova"./second.shecho "ho lanciato il secondo"./3rd.shecho "ho lanciato il terzo"If you need the other scripts to access variables and also functions from the original manuscript, yet not modify them, then call these scripts in subshells. Subshells are sepaprice processes, so exit exits only them.

See more: Could Not Be Saved Because The Source File, Fix: Firefox ' Could Not Be Read'

#! /bin/kshecho "prova"(. ./second.sh)echo "ho lanciato il secondo"(. ./third.sh)echo "ho lanciato il terzo"If you have to usage variables or attributes characterized in second.sh and third.sh in the parent manuscript, then you"ll must save sourcing them.

The return builtin exits only the sourced manuscript and also not the entirety procedure — that"s among the few distinctions in between consisting of an additional manuscript with the . command also and also consisting of its text in the parent script. If the sourced scripts just contact departure at the toplevel, as opposed to inside functions, then you can readjust leave into return. You can carry out that without editing the script by utilizing an alias.

See more: Surface Pro 4 Trackpad Not Working : Touchpad Fixed (All Models)

#! /bin/kshecho "prova"alias exit=rerotate. ./second.shecho "ho lanciato il secondo". ./3rd.shunalias exitecho "ho lanciato il terzo"If leave is additionally dubbed inside attributes, I do not think there"s a non-cumbersome method. A cumbersome method is to set an departure trap and put your code there.

#!/bin/kshdo_initially () echo "prova" trap "after_second" EXIT . ./second.sh after_secondafter_second () echo "ho lanciato il secondo" trap "after_third" EXIT . ./third.sh after_thirdafter_3rd () trap - EXIT echo "ho lanciato il terzo"do_first