bash 确定变量是否为空,如果是则退出。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19838064/
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 determine if variable is empty and if so exit.
提问by user2653557
I am trying to perform this: i have a test file which md5sum of files located on sftp. variables should contain an md5sum (string), if the variable is empty it means there is no file on the sftp server. i am trying this code but it does not work..
我正在尝试执行此操作:我有一个测试文件,其中包含位于 sftp 上的文件的 md5sum。变量应包含一个 md5sum(字符串),如果变量为空,则表示 sftp 服务器上没有文件。我正在尝试此代码,但它不起作用..
if [ -z $I_IDOCMD5 ] || [ -z $I_LEGALMD5 ] || [ -z $I_ZIPMD5 ]
then
echo "ERROR: At least one file not present of checksum missing no files will be deleted" >>$IN_LOG
ERRORS=$ERRORS+2
else
if [[ $I_IDOCMD5 == $($DIGEST -a md5 $SAPFOLDER/inward/idoc/$I_IDOC) ]]
then
echo "rm IDOC/$I_IDOC" >/SAP/commands_sftp.in
else
echo "problem with checksum"
ERRORS=$ERRORS+2
fi
if [[ $I_LEGALMD5 == $($DIGEST -a md5 $SAPFOLDER/inward/legal/$I_LEGAL) ]]
then
echo "rm LEGAL/$I_LEGAL" >>/SAP/commands_sftp.in
else
echo "problem with checksum"
ERRORS=$ERRORS+2
fi
if [[ $I_ZIPMD5 == $($DIGEST -a md5 $SAPFOLDER/inward/zip/$I_ZIP) ]]
then
echo "rm ZIP/$I_ZIP" >>/SAP/commands_sftp.in
else
echo "problem with checksum"
ERRORS=$ERRORS+2
fi
回答by Pipo
The answer I prefer is following
我更喜欢的答案是
[[ -z "" ]] && { echo "Parameter 1 is empty" ; exit 1; }
Note, don't forget the ;
into the {}
after each instruction
注意,不要忘记;
进入{}
每条指令之后
回答by user2653557
One way to check if a variable is empty is:
检查变量是否为空的一种方法是:
if [ "$var" = "" ]; then
# $var is empty
fi
Another, shorter alternative is this:
另一个更短的选择是这样的:
[ "$var" ] || # var is empty
回答by Lirt
In bash you can use set -u
which causes bash to exit on failed parameter expansion.
在 bash 中,您可以使用set -u
which 导致 bash 在参数扩展失败时退出。
From bash man (section about set
builtin):
来自 bash man(关于set
内置的部分):
-u
Treat unset variables and parameters other than the special parameters "@" and "*" as an error when performing parameter expansion. If expansion is attempted on an unset variable or parameter, the shell prints an error message, and, if not interactive, exits with a non-zero status.
-u
执行参数扩展时,将未设置的变量和特殊参数“@”和“*”以外的参数视为错误。如果尝试对未设置的变量或参数进行扩展,shell 会打印一条错误消息,如果不是交互式的,则以非零状态退出。
For more information I recommend this article: http://redsymbol.net/articles/unofficial-bash-strict-mode/
有关更多信息,我推荐这篇文章:http: //redsymbol.net/articles/unofficial-bash-strict-mode/
回答by gluk47
You can use a short form:
您可以使用简短的形式:
FNAME="$I_IDOCMD5"
: ${FNAME:="$I_LEGALMD5"}
: ${FNAME:="$I_ZIPMD5"}
: ${FNAME:?"Usage: if [ -z "$I_IDOCMD5" ] || [ -z "$I_LEGALMD5" ] || [ -z "$I_ZIPMD5" ]
then
echo "one is missing"
else
echo "everything OK"
fi
echo "\"$I_IDOCMD5\""
echo "\"$I_LEGALMD5\""
echo "\"$I_ZIPMD5\""
filename"}
In this case the script will exit if neither of the I_...
variables is declared, printing an error message prepended with the shell script line that triggered the message.
在这种情况下,如果没有I_...
声明任何变量,脚本将退出,打印一条错误消息,并在触发该消息的 shell 脚本行之前打印。
See more on this in abs-guide(search for ?Example 10-7?).
在abs-guide 中查看更多相关信息(搜索?示例 10-7?)。
回答by thom
First test only this (just to narrow it down):
首先只测试这个(只是为了缩小范围):
##代码##回答by thom
"if the variable is empty it means there is no file on the sftp server"
“如果变量为空,则表示 sftp 服务器上没有文件”
If there is no file on the sftp server, isthe variable then reallyempty ?
如果在SFTP服务器上没有文件,是那么变量确实是空的?
No hidden spaces or anything like that ? or the number zero (which counts as non-empty) ?
没有隐藏空间或类似的东西?或数字零(算作非空)?