bash 如何逐行执行bash脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9080431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How execute bash script line by line?
提问by Rodrigo
If I enter bash -xoption, it will show all the line. But the script will execute normaly.
如果我输入bash -x选项,它将显示所有行。但是脚本会正常执行。
How can I execute line by line? Than I can see if it do the correct thing, or I abort and fix the bug. The same effect is put a read
in every line.
如何逐行执行?我可以看看它是否做正确的事情,或者我中止并修复错误。相同的效果read
在每一行中放置一个。
回答by organic-mashup
You don't need to put a read in everyline, just add a trap like the following into your bash script, it has the effect you want, eg.
你不需要在每一行都读,只需在你的 bash 脚本中添加一个像下面这样的陷阱,它就有你想要的效果,例如。
#!/usr/bin/env bash
set -x
trap read debug
< YOUR CODE HERE >
Works, just tested it with bash v4.2.8 and v3.2.25.
有效,刚刚使用 bash v4.2.8 和 v3.2.25 对其进行了测试。
IMPROVED VERSION
改良版
If your script is reading content from files, the above listed will not work. A workaround could look like the following example.
如果您的脚本正在从文件中读取内容,则上面列出的内容将不起作用。解决方法可能类似于以下示例。
#!/usr/bin/env bash
echo "Press CTRL+C to proceed."
trap "pkill -f 'sleep 1h'" INT
trap "set +x ; sleep 1h ; set -x" DEBUG
< YOUR CODE HERE >
To stop the script you would have to kill it from another shellin this case.
在这种情况下,要停止脚本,您必须从另一个 shell中杀死它。
ALTERNATIVE1
备选方案1
If you simply want to wait a few seconds before proceeding to the next command in your script the following example could work for you.
如果您只是想在继续执行脚本中的下一个命令之前等待几秒钟,以下示例可能适合您。
#!/usr/bin/env bash
trap "set +x; sleep 5; set -x" DEBUG
< YOUR CODE HERE >
I'm adding set +x and set -x within the trap command to make the output more readable.
我在 trap 命令中添加了 set +x 和 set -x 以使输出更具可读性。
回答by Rodrigo
Maybe the BASH Debuggeris something for you.
也许BASH 调试器适合您。
回答by Brad Parks
If your bash script is really a bunch of one off commands that you want to run one by one, you could do something like this, which runs each command one by one when you increment a variable LN
, corresponding to the line number you want to run. This allows you to just run the last command again super easy, and then you just increment the variable to go to the next command.
如果你的 bash 脚本真的是一堆你想一个一个运行的一次性命令,你可以做这样的事情,当你增加一个变量时一个一个地运行每个命令LN
,对应于你想要运行的行号. 这使您可以非常轻松地再次运行最后一个命令,然后您只需增加变量即可转到下一个命令。
Assuming your commands are in a file "it.sh", run the following, one by one.
假设您的命令在“it.sh”文件中,请一一运行以下命令。
$ cat it.sh
echo "hi there"
date
ls -la /etc/passwd
$ $(LN=1 && cat it.sh | head -n$LN | tail -n1)
"hi there"
$ $(LN=2 && cat it.sh | head -n$LN | tail -n1)
Wed Feb 28 10:58:52 AST 2018
$ $(LN=3 && cat it.sh | head -n$LN | tail -n1)
-rw-r--r-- 1 root wheel 6774 Oct 2 21:29 /etc/passwd
回答by Tinmarino
xargs:can filter lines
xargs:可以过滤行
cat .bashrc | xargs -0 -l -d \n bash
- -0 Treat as raw input (no escaping)
- -l Separate each line (Not by default for performances)
- -d \\n The line separator
- -0 视为原始输入(无转义)
- -l 分隔每一行(不是默认的表演)
- -d \\n 行分隔符