在shell脚本中运行多个shell脚本时出现问题 [英] Issue running multiple shell scripts inside a shell script

查看:276
本文介绍了在shell脚本中运行多个shell脚本时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个在CentOS 7下运行多个脚本的shell脚本.每个脚本均以#!/bin/bash 开头.每个脚本都经过测试,可以毫无问题地作为独立的shell脚本运行.

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.

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

看来 ./scripts_all.sh 将成功执行 set -x sudo yum sh script_1.sh ,但在 sh script_2.sh 处停止.值得注意的是,我可以独立于 scripts_all.sh 来运行 sh script_2.sh .我不知道为什么其余的脚本无法运行.

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 打印 sh script_1.sh 的行及其执行,但从不打印 sh script_2.sh .

./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.

有人可以帮忙吗?

推荐答案

将注释复制到相似的答案中.

sh script_1.sh 等行更改为 bash -x script_1.sh (或自脚本以来为 sh -x script_1.sh 似乎不使用任何特定于Bash的语法)并监​​视正在发生的事情.您是否在 script_1.sh 中的 gcc --version 中看到了版本信息?

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?

仅当我注释掉 scl enable devtoolset-9 bash 时,才会打印

gcc --version .我运行了 scl enable devtoolset-9 bash ,它没有在屏幕上输出任何内容.

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.

这表明 scl 命令未完成.也许它正在等待来自终端的输入.当包含 scl 命令时,您是否看到了哪个gcc 的输出?如果不是,那么几乎可以肯定 scl 正在尝试从终端读取.我不知道它在读什么-这不是我熟悉的命令.

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.

如果没有看到 gcc gcc --version 的输出,则 scl 命令很可能没有完成,IMO.命令选项末尾的 bash 有什么作用?它是否运行 bash 进程?如果是这样,其输入来自哪里?使用 -x 选项( sh -x script_1.sh )运行将向您显示正在执行什么,以及 scl 是否完成./p>

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 实际上运行启用了 foo bar 软件集合的bash实例.参见 https://linux.die.net/man/1/scl

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

好;那个 bash 实例在做什么?它在执行任何操作之前不等待输入吗?没有提示,但并不完全令人惊讶,这有点令人惊讶.您是否曾尝试在 scl 挂起时键入 exit ?

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?

我刚刚尝试了 scl enable devtoolset-9 bash&回显"Enabling devtoolset-9" ,它可以工作并最终打印出 gcc --version .

I just tried scl enable devtoolset-9 bash & echo "Enabling devtoolset-9" and it works and ultimately prints out the gcc --version.

好吧,那个& 在后台运行 scl 命令,让 echo 继续运行,然后哪个gcc gcc --version .用分号替换& .或用 -c'echo Hi'和分号替换& ,看看会发生什么.

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.

太棒了!添加 -c回声"Hi" 使其正常工作!

因此,在 scl末尾指定的 bash 命令启用devtoolset-9 bash 一直在等待您的输入,这就是为什么它没有终止的原因(而您没有看到哪个gcc 正在运行)等等.您在 script_2.sh 的末尾遇到了相同的问题-其他脚本又如何呢?但是您现在知道发生了什么,并可以决定要怎么做.例如,使用 -c exit 会简单地终止外壳(而不是回显 Hi ).

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.

我需要进一步研究 scl 命令,但是您实际上需要在这些脚本中使用它吗?

I'd need to study the scl command more, but do you actually need to use it in these scripts?

这篇关于在shell脚本中运行多个shell脚本时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆