bash 如果空字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12468396/
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
bash if empty string
提问by nkvnkv
Some may have found out that in Ubuntu when you install or update via terminal
当您通过终端安装或更新时,有些人可能已经发现在 Ubuntu 中
you get a question similar to
你得到一个类似的问题
"do you with to install or remove package [Y/n]?"
“您是否要安装或删除软件包 [是/否]?”
when you press enter its like its the same as when you type "Y" or "y"
当您按 Enter 时,它就像您键入“Y”或“y”时一样
So i was wondering how to make that in bash This is what i have so far?
所以我想知道如何在 bash 中做到这一点 这是我到目前为止所拥有的?
echo "question?[Y/n]"
read choose
if [ $choose ~= "Y" ] [ $choose ~= "y" ] [ $choose ~= "" ]
then
#code
fi
回答by Jonathan Leffler
Classically (meaning it will work in POSIX-ish shells other than bash), you'd write:
经典地(意味着它可以在 POSIX-ish shell 中工作,而不是bash),你会写:
echo "Question? [Y/n]"
read choose
if [ "$choose" = "Y" ] || [ "$choose" = "y" ] || [ -z "$choose" ]
then
# code
fi
The quotes ensure that the test operator see an argument even if $chooseis an empty string.
引号确保测试操作符即使$choose是空字符串也能看到参数。
The [[operator seems to allow you to get away without quoting strings, but is not part of a POSIX shell. POSIX recognizes its existence by noting:
该[[运算符似乎允许您在不引用字符串的情况下离开,但它不是 POSIX shell 的一部分。POSIX 通过注意以下几点来识别它的存在:
The following words may be recognized as reserved words on some implementations (when none of the characters are quoted), causing unspecified results:
[[ ]] function select
以下单词在某些实现中可能被识别为保留字(当没有引用任何字符时),导致未指定的结果:
[[ ]] function select
If you want the shell to leave the cursor on the same line as the prompt, you can use:
如果您希望 shell 将光标留在与提示相同的行上,您可以使用:
printf "Question? [Y/n] "
POSIX shells recognize the \cescape; bashdoes not:
POSIX shell 识别\c转义;bash才不是:
echo "Question? [Y/n] \c"
(There may be a way to make bashhandle that, but printfis probably more portable.)
(可能有一种方法可以解决bash这个问题,但printf可能更便携。)
回答by Mark Reed
echo -n "Question? [Y/n]"
read choose
shopt -s nocasematch
if [[ "${choose:=Y}" =~ Y ]]; then
# they said yes
else
# they said something else
fi
This approach has the possible advantage that it leaves $chooseset to Y if you examine it later. If you prefer to be able to tell later whether they entered Yor just pressed enter, you can use ${choose:-Y}instead.
这种方法有一个可能的优点,$choose如果您稍后检查它,它会保留设置为 Y。如果您希望以后能够知道他们是输入了Y还是只是按了 Enter,则可以${choose:-Y}改用。
If you'd rather not set nocasematch, you can always check explicitly for both Yand y. Or, if you like the nocasematchsolution but care about preserving the state of the flag, you can check and restore it:
如果您不想设置nocasematch,则始终可以明确检查Y和y。或者,如果您喜欢该nocasematch解决方案但关心保留标志的状态,您可以检查并恢复它:
shopt -q nocasematch
let not_set=$?
shopt -s nocasematch
...
...
if (( not_set )); then
shopt -u nocasematch
fi
回答by Ansgar Wiechers
See test(1).
见test(1)。
[ "$choose" = "Y" -o "$choose" = "y" -o "$choose" = "" ]
You mustput the variable between double quotes, otherwise the expressions would produce an error with an empty variable, because their respective left sides would be empty.
你必须把变量放在双引号之间,否则表达式会产生一个带有空变量的错误,因为它们各自的左边是空的。
回答by Piotr Wadas
"read" reads input up to enter. So if you want just "enter" to be accepted as answer, try with "" answer. You can use [[ ]] instead of "test" command to check strings ( man test )
“读取”读取输入以进入。因此,如果您只想“输入”被接受为答案,请尝试使用“”答案。您可以使用 [[ ]] 代替“test”命令来检查字符串( man test )
dtpwmbp:~ pwadas$ [[ -z "" ]] && echo yes
yes
dtpwmbp:~ pwadas$ [[ -z "ABCD" ]] && echo yes
dtpwmbp:~ pwadas$
Here, a test for zero-length of the string.
在这里,测试字符串的零长度。

