Send a sequence of commands to a new terminal from

2019-08-23 03:39发布

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条回答
倾城 Initia
2楼-- · 2019-08-23 03:51

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
查看更多
登录 后发表回答