我有一个脚本 test
#! /bin/bash
sleep 60
echo hello
Run Code Online (Sandbox Code Playgroud)
我通过将其设置为可执行文件来运行它./test
,并且在它运行 sleep 命令时,我将其更改为
#! /bin/bash
sleep 60
echo hello!!
Run Code Online (Sandbox Code Playgroud)
输出是hello!!
。我想知道为什么不hello
呢?
hello
无论以后如何更改,我怎样才能让它输出?
这是适用于您的情况的解决方案。
将您的脚本分解为函数,每次source
从单独的文件中调用函数时。然后您可以随时编辑文件,您的运行脚本将在下次获取更改时获取更改。
foo() {
source foo.sh
}
foo
Run Code Online (Sandbox Code Playgroud)
测试
我创建了 2 个脚本callee.sh
,called.sh
如下所示。
#The contents of callee.sh script is as below.
callee.sh
foo() {
source called.sh
}
foo
#The contents of called.sh script is as below.
called.sh
#! /bin/bash
sleep 60
echo hello
Run Code Online (Sandbox Code Playgroud)
现在,我执行callee.sh
它又调用我的called.sh
脚本。基本上,called.sh
是我们将动态更改的脚本。
现在,当callee.sh
被执行时,我打开另一个外壳并将内容更改为,
#! /bin/bash
sleep 60
echo hello
echo "I added more contents here"
Run Code Online (Sandbox Code Playgroud)
现在,在第一壳从我称之为脚本callee.sh
后sleep
结束,输出我得到的是,
hello
Run Code Online (Sandbox Code Playgroud)
如您所见,I added more contents here
我的输出中没有得到所需的最终结果。
参考
归档时间: |
|
查看次数: |
5689 次 |
最近记录: |