If I enter bash -x option, 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.
我如何能逐行执行?我可以看到它是否正确,或者我中止并修复bug。同样的效果是每一行都有一个读音。
5 个解决方案
#1
88
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 shell in this case.
要停止脚本,您必须在本例中从另一个shell中删除它。
ALTERNATIVE1
ALTERNATIVE1
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,以使输出更具可读性。
#3
1
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.
假设你的命令在文件中。“嘘”,一个接一个地跑。
$ 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
#4
0
Have a look at bash-stepping-xtrace.
看看bash- steppx -xtrace。
It allows stepping xtrace.
它允许xtrace。
#5
0
xargs: can filter lines
xargs:过滤器能行
cat .bashrc | xargs -0 -l -d \\n bash
- -0 Treat as raw input (no escaping)
- -0作为原始输入(不转义)
- -l Separate each line (Not by default for performances)
- -l分开每行(不默认为性能)
- -d \\n The line separator
- -d \\n线分离器。
#1
88
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 shell in this case.
要停止脚本,您必须在本例中从另一个shell中删除它。
ALTERNATIVE1
ALTERNATIVE1
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,以使输出更具可读性。
#2
#3
1
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.
假设你的命令在文件中。“嘘”,一个接一个地跑。
$ 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
#4
0
Have a look at bash-stepping-xtrace.
看看bash- steppx -xtrace。
It allows stepping xtrace.
它允许xtrace。
#5
0
xargs: can filter lines
xargs:过滤器能行
cat .bashrc | xargs -0 -l -d \\n bash
- -0 Treat as raw input (no escaping)
- -0作为原始输入(不转义)
- -l Separate each line (Not by default for performances)
- -l分开每行(不默认为性能)
- -d \\n The line separator
- -d \\n线分离器。