如何通过线执行bash脚本行? [英] How execute bash script line by line?

查看:134
本文介绍了如何通过线执行bash脚本行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我输入的bash -x 选项,它会显示所有的线。但是,该脚本将normaly执行。

If I enter bash -x option, it will show all the line. But the script will execute normaly.

我如何才能通过行执行行?比我能看它是否做了正确的事情,或者我放弃和修复bug。同样的效果是把中的每一行。

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.

推荐答案

您不需要把一个读的everyline,只需添加一个陷阱一样以下到您的bash脚本,它拥有你想要的效果,如:

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.

#!/usr/bin/env bash
set -x
trap read debug

< YOUR CODE HERE >

作品,只使用bash v4.2.8和v3.2.25测试它。

Works, just tested it with bash v4.2.8 and v3.2.25.

改良版

如果你的脚本读取从文件中的内容,上面列出的将无法工作。一种解决方法可能看起来像下面的例子。

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 >

要阻止你将不得不在脚本的在这种情况下,另一个shell 杀死它。

To stop the script you would have to kill it from another shell in this case.

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 >

我添加一套+ x和-x设置的陷阱命令中,以使输出更具可读性。

I'm adding set +x and set -x within the trap command to make the output more readable.

这篇关于如何通过线执行bash脚本行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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