有没有一种方法可以在新的 XTERM 窗口中执行 bash 函数?以下是我正在尝试做的事情
function test(){
echo "Do some work"
}
然后在我的 bash 脚本中,我正在执行以下操作:
export -f test
xterm -title "Work1" -e "test" "$date_today" "$time_today" &
# The above I am trying to open xterm, run the function, and pass 2 parameters (date_today and time today)
目前上述内容不起作用,因为它抱怨测试未定义。任何帮助将不胜感激
不要将其放入函数中,只需:
#!/bin/bash
echo "Do some work"
并将文件命名为
test.sh
。不要仅仅称其为 test
。记得chmod +x file.sh
。然后调用它:
xterm -title "Work1" -e "<path_to_file>/test.sh" "$date_today" "$time_today" &
我使用
typeset
通过ssh
导出函数,但似乎你也可以将它用于xterm
。
$ function test(){
echo "Do some work"
}
$ export -f test
$ xterm -title "Work1" -e "$(typeset -f test); test" "$date_today" "$time_today" &
Bayou的答案几乎是正确的,但暗示bash是默认shell并且必须计算双引号之间的所有内容。
然后我使用这个长时间运行功能来显示安静发生了什么。
test() {
for i in {10..0..-1}; do
echo $i
read -sn 1 -t .3 _ && break
done
}
注意:过程中按任意键将立即停止。
然后
xterm -title "Sub work in progress..." -e "$(declare -f test);test" &
或者如果 default shell 不是 bash:
xterm -title "Sub work in progress..." -e bash -c "$(declare -f test);test" &
注意:由于函数是在双引号之间重新声明的,因此不需要
export
ed!
您可以使用
-hold
xterm 选项来等待用户关闭窗口,甚至启动它们图标化,然后在作业完成时将其提升:
xterm -iconic -title "Sub work in progress..." -e \
bash -c "$(declare -f test);test;printf '\e[1t';read -sn 1 _" &
因此 Xterm 窗口将开始图标化,执行所需的工作,抬起并在关闭之前等待任何键。