0

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?

afp_2008
  • 1,940
  • 1
  • 19
  • 46
  • 3
    If they're bash scripts you should use `bash script_N.sh`. Or if they begin with `#!` just use `./script_N.sh` – Barmar Mar 05 '21 at 00:04
  • I tried `./script_N.sh` and still the same issue. – afp_2008 Mar 05 '21 at 00:07
  • 1
    Does `set -x` display the `script_1.sh` line? – Barmar Mar 05 '21 at 00:10
  • 1
    If not, it means that `sudo yum ...` hasn't finished. – Barmar Mar 05 '21 at 00:11
  • @Barmar yes it does print the lines of `script_1.sh`. – afp_2008 Mar 05 '21 at 00:14
  • 2
    Why does it print those lines? Does that script also begin with `set -x`? Do the other scripts have that? – Barmar Mar 05 '21 at 00:14
  • 4
    What does "stops at" mean? Does it hang forever, or does it [suddenly exit](https://stackoverflow.com/questions/18815151/why-does-my-script-suddenly-exit-after-a-command)? – that other guy Mar 05 '21 at 00:45
  • 1
    Please provide a [mcve], to much information is missing to continue helping IMHO. – Nic3500 Mar 05 '21 at 02:55
  • @Nic3500 please see the updated post. – afp_2008 Mar 05 '21 at 03:42
  • 1
    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`? – Jonathan Leffler Mar 05 '21 at 04:08
  • @JonathanLeffler `gcc --version` is only printed when I comment out `scl enable devtoolset-9 bash`. I ran `scl enable devtoolset-9 bash` and it does not output anything to the screen. – afp_2008 Mar 05 '21 at 04:15
  • 1
    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 isn't a command I'm familiar with. – Jonathan Leffler Mar 05 '21 at 04:17
  • @JonathanLeffler it is not waiting for any input. After execution, it brings the prompt again when I run it by itself. – afp_2008 Mar 05 '21 at 04:19
  • 1
    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. – Jonathan Leffler Mar 05 '21 at 04:22
  • @JonathanLeffler `scl enable foo bar bash` actually runs bash instance with foo and bar Software Collections enabled. https://linux.die.net/man/1/scl – afp_2008 Mar 05 '21 at 04:26
  • 1
    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? – Jonathan Leffler Mar 05 '21 at 04:28
  • I just tried `scl enable devtoolset-9 bash & echo "Enabing devtoolset-9"` and it works and ultimately prints out the `gcc --version`. – afp_2008 Mar 05 '21 at 04:29
  • 1
    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. – Jonathan Leffler Mar 05 '21 at 04:35
  • @JonathanLeffler Wonderful! Adding `-c echo "Hi"` made it work! – afp_2008 Mar 05 '21 at 04:39
  • 1
    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 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. `-c exit` would simply terminate the shell (instead of echoing `Hi`), for example. – Jonathan Leffler Mar 05 '21 at 04:42
  • Thank you very much. If you would like to post a solution, I will accept it immediately. – afp_2008 Mar 05 '21 at 04:43

1 Answers1

1

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 out scl enable devtoolset-9 bash. I ran scl 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 with foo and bar 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 the gcc --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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278