Bash input without pausing the script?

吃可爱长大的小学妹 提交于 2021-02-09 05:14:32

问题


In Bash the only way to get a (user) input seems to be to use the read method, which pauses the rest of the script. Is there any way to receive a command line input (ending with the enter key) without pausing the script. From what I've seen there may be a way to do it with $1 ..?


回答1:


read -t0 can be used to probe for input if your process is structured as a loop

 #!/bin/bash

 a='\|/-'
 spin()
 {
  sleep 0.3
  a="${a:1}${a:0:1}"
  echo -n $'\e'7$'\r'"${a:1:1}"$'\e'8
 }

 echo 'try these /|\- , dbpq , |)>)|(<( , =>-<'

 echo -n "  enter a pattern to spin:"
 while true
 do
   spin
   if read -t0 
   then
     read a
     echo -n "  using $a enter a new pattern:" 
   fi
 done

else you could run one command in the background while promptiong for input in the foreground. etc...



来源:https://stackoverflow.com/questions/27772692/bash-input-without-pausing-the-script

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