从脚本向新终端发送一系列命令 [英] Send a sequence of commands to a new terminal from a script
本文介绍了从脚本向新终端发送一系列命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个脚本,我想在最后添加一个关闭计时器,我希望倒计时在新的终端窗口中运行,以便我可以取消它,因为脚本通常会在后台运行.>
问题来了,
一个只包含以下内容的简单脚本
secs=$((60))而 [ $secs -gt 0 ];做echo -ne "$secs\033[0K\r"睡觉 1: $((秒--))完毕现在关机
工作正常,但如果我尝试将其发送到这样的新终端
gnome-terminal -e "bash -c'秒=$((60))而 [ $secs -gt 0 ];做echo -ne \"$secs\033[0K\r\"睡觉 1: $((秒--))完毕立即关机'"
它失败了,只是关闭了.如果我删除关闭行,我会收到此错误:
选项-e"已弃用,可能会在 gnome-terminal 的更高版本中删除.使用--"终止选项并在其后执行命令行.
有谁知道我该如何解决这个问题?
谢谢
解决方案
最简单的方法是导出一个函数:
倒计时(){秒=60而 (( 秒 > 0 ));做printf '%s\033[0K\r' "$secs"睡觉 1((秒--))完毕现在关机}导出 -f 倒计时gnome-terminal -- bash -c 倒计时
I have a script to which I wanna add a shutdown timer at the end, I'd like the countdown to run in a new terminal windows so I can cancel it since the script will usually be run in the background.
Here's the problem,
a simple script containing only the following
secs=$((60))
while [ $secs -gt 0 ]; do
echo -ne "$secs\033[0K\r"
sleep 1
: $((secs--))
done
shutdown now
works fine, but if I try to send it to a new terminal like this
gnome-terminal -e "bash -c
'secs=$((60))
while [ $secs -gt 0 ]; do
echo -ne \"$secs\033[0K\r\"
sleep 1
: $((secs--))
done
shutdown now'"
it fails and just shuts down. If I remove the shutdown line I get this error :
Option "-e" is deprecated and might be removed in a later version of gnome-terminal.
Use "-- " to terminate the options and put the command line to execute after it.
Does anyone know how I could fix this?
thanks
解决方案
The easy way to do this is to export a function:
countdown() {
secs=60
while (( secs > 0 )); do
printf '%s\033[0K\r' "$secs"
sleep 1
((secs--))
done
shutdown now
}
export -f countdown
gnome-terminal -- bash -c countdown
这篇关于从脚本向新终端发送一系列命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文