Issue
I am trying to create a shell script that runs several scripts under CentOS 7. Each script starts with #!/bin/bash
. Each script is tested and can be run as a standalone shell script with no problem.
Content of scripts_all.sh
#!/bin/bash
set -x
sudo yum install g++ make binutils cmake openssl-devel boost-devel zlib-devel
sh script_1.sh # executes all the contents of script_1.sh
sh script_2.sh # does not executed any of the command in script_2.sh.
sh script_3.sh
sh script_4.sh
script_1.sh
#!/bin/bash
sudo yum install centos-release-scl
sudo yum install devtoolset-9-gcc*
scl enable devtoolset-9 bash
which gcc
gcc --version
script_2.sh
#!/bin/bash
sudo yum install centos-release-scl-rh
sudo yum-config-manager --enable centos-release-scl-rh
sudo yum install devtoolset-9
scl enable devtoolset-9 bash
It appears that ./scripts_all.sh
will successfully execute set -x
, sudo yum
, sh script_1.sh
but stops at sh script_2.sh
. It is worth noting that I can run sh script_2.sh
with no issue independent of scripts_all.sh
. I don't know why the rest of the scripts won't be run.
./scripts_all.sh
prints the lines of sh script_1.sh
and their executions but it never prints the lines of sh script_2.sh
.
Could someone kindly help?
Solution
Copying comments into the semblance of an answer.
Change the sh script_1.sh
etc lines to bash -x script_1.sh
(or sh -x script_1.sh
since the scripts don't seem to use any Bash-specific syntax) and monitor what's going on. Do you see the version information from gcc --version
in script_1.sh
?
gcc --version
is only printed when I comment outscl enable devtoolset-9 bash
. I ranscl enable devtoolset-9 bash
and it does not output anything to the screen.
That suggests the scl
command is not completing. Maybe it is waiting for input from the terminal. Do you see the output from which gcc
when you include the scl
command? If not, then it is close to certain that scl
is trying to read from the terminal. I dunno what it's reading — it isn't a command I'm familiar with.
It is not waiting for any input. After execution, it brings the prompt again when I run it by itself.
If you're not seeing the which gcc
and gcc --version
output, then it is probable that the scl
command is not completing, IMO. What does the bash
at the end of the command options do? Does it run a bash
process? If so, where is its input coming from? Running with the -x
option (sh -x script_1.sh
) would show you what is being executed, and whether scl
is completing.
scl enable foo bar bash
actually runs a bash instance withfoo
andbar
Software Collections enabled. See https://linux.die.net/man/1/scl
OK; and what is that bash
instance doing? Is it not waiting for input before it executes anything? It's a little surprising that there isn't a prompt, but not completely astonishing. Have you tried typing exit
when scl
hangs?
I just tried
scl enable devtoolset-9 bash & echo "Enabling devtoolset-9"
and it works and ultimately prints out thegcc --version
.
Well, that &
runs the scl
command in background, leaving the echo
to run, and then which gcc
and gcc --version
. Replace the &
with a semicolon. Or replace the &
with -c 'echo Hi'
and a semicolon and see what happens.
Wonderful! Adding
-c echo "Hi"
made it work!
So that bash
command specified at the end of scl enable devtoolset-9 bash
was waiting for input from you, which is why it didn't terminate (and you don't see which gcc
running) and so on. You've got the same issue at the end of script_2.sh
— what about the other scripts? But you now know what's going on and can decide what to do about it. Using -c exit
would simply terminate the shell (instead of echoing Hi
), for example.
I'd need to study the scl
command more, but do you actually need to use it in these scripts?
Answered By - Jonathan Leffler