Bash 在不暂停脚本的情况下输入?

Bash input without pausing the script?

在 Bash 中,获得(用户)输入的唯一方法似乎是使用 read 方法,该方法会暂停脚本的其余部分。有什么方法可以在不暂停脚本的情况下接收命令行输入(以回车键结尾)。据我所知,可能有一种方法可以用 $1 ..?

如果您的流程结构为循环,

read -t0 可用于探测输入

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

否则您可以 运行 在后台执行一个命令,同时在前台提示输入。等...