在不暂停脚本的情况下进行Bash输入? [英] Bash input without pausing the script?

查看:54
本文介绍了在不暂停脚本的情况下进行Bash输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Bash中,获取(用户)输入的唯一方法似乎是使用read方法,该方法将暂停脚本的其余部分.有什么方法可以在不暂停脚本的情况下接收命令行输入(以Enter键结尾).从我所看到的,也许有一种方法可以用$ 1 ..?

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 ..?

推荐答案

read -t0 可用于探测您的过程是否为循环结构

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...

这篇关于在不暂停脚本的情况下进行Bash输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆