bash 如何检查bash读取中没有输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8031073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:02:07  来源:igfitidea点击:

How to check for no input in bash read?

bash

提问by qliq

I am looking for a conditional to avoid users from leaving an input value blank. Any suggestions?

我正在寻找一个条件来避免用户将输入值留空。有什么建议?

Thanks

谢谢

采纳答案by Manny D

No inputs (or even spaces I believe) get entered as empty strings, so check input while the input var is empty:

没有输入(甚至我相信的空格)作为空字符串输入,因此在输入变量为空时检查输入:

input=
while [[ $input = "" ]]; do
   read input
done

回答by Mark L

unset input
while [ -z ${input} ]; do
     read input
done