Send a sequence of commands to a new terminal from a script

空扰寡人 提交于 2020-01-16 07:42:28

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/48236537/send-a-sequence-of-commands-to-a-new-terminal-from-a-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!