在新的 Xterm 窗口中执行 Bash 函数

问题描述 投票:0回答:3

有没有一种方法可以在新的 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)

目前上述内容不起作用,因为它抱怨测试未定义。任何帮助将不胜感激

bash xterm
3个回答
0
投票

不要将其放入函数中,只需:

#!/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" &


0
投票

我使用

typeset
通过
ssh
导出函数,但似乎你也可以将它用于
xterm

$ function test(){
    echo "Do some work"
}

$ export -f test

$ xterm -title "Work1" -e "$(typeset -f test); test" "$date_today" "$time_today" &

0
投票

在专用 Xterm 窗口中运行函数

Bayou的答案几乎是正确的,但暗示默认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 不是 :

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 窗口将开始图标化,执行所需的工作,抬起并在关闭之前等待任何键。

© www.soinside.com 2019 - 2024. All rights reserved.