[英]How to assign a variable inside if condition in shell script
I would like to know how to assign a variable inside if block in shell script.. 我想知道如何在shell脚本中的if块内分配变量。
Below is my code.. 下面是我的代码。
if [[ -z "$MMBOX_PATH" || -z "$BACKUP_PATH" || -z "$REMOTE_SERVER" || -z "$LOG_PATH" ]]
then
echo -e "Must Provide All Required Paths [$FLAG is Empty].."
exit 1
fi
The above code will run whenever it found empty variable, but I also wants to know which variable is empty (Eg, In above code suppose if LOG_PATH variable is empty then it should display in echo output in place of $FLAG ) 上面的代码将在找到空变量时运行,但我也想知道哪个变量为空(例如,在上面的代码中,假设LOG_PATH变量为空,则应在echo输出中显示它代替$ FLAG)
I tried following codes.. 我尝试了以下代码。
if [[ `FLAG='MMBOX_PATH'` && -z "$MMBOX_PATH" || `FLAG='BACKUP_PATH'` && -z "$BACKUP_PATH" || `FLAG='REMOTE_SERVER'` && -z "$REMOTE_SERVER" || `FLAG='LOG_PATH'` && -z "$LOG_PATH" ]]
then
echo -e "Must Provide All Required Paths [$FLAG is Empty].."
exit 1
fi
But above code returns false hence it is not printing the content inside echo. 但是上面的代码返回false,因此不会在echo中打印内容。
I also tried to keep FLAG variable before condition execution, but every time it returns 'Nothing' 我还尝试在条件执行之前保留FLAG变量,但是每次它返回“ Nothing”时
if FLAG='MMBOX_PATH' && [[ -z "$MMBOX_PATH" ]]
then
echo -e "Must Provide All Required Paths [$FLAG is Empty].."
exit 1
fi
In above case I'm getting FLAG='MMBOX_PATH' in output but if I add one more condition to that if nothing is printing (Means if I check same thing for BACKUP_PATH,REMOTE_SERVER..) 在上述情况下,我在输出中得到FLAG ='MMBOX_PATH',但是如果没有打印任何内容,那么我会再添加一个条件(意味着如果我对BACKUP_PATH,REMOTE_SERVER进行了同样的检查。。)
if FLAG='MMBOX_PATH' && [[ -z "$MMBOX_PATH" ]] && FLAG='LOG_PATH' && [[ -z "$LOG_PATH" ]]
then
echo -e "Must Provide All Required Paths [$FLAG is Empty].."
exit 1
fi
In this case nothing is printing even though MMBOX_PATH present and LOG_PATH empty. 在这种情况下,即使存在MMBOX_PATH并且LOG_PATH为空,也不会打印任何内容。
Note: Using if condition each and every variable it is possible to know which variable is empty,but I don't want to extend my lines with if-else conditions I just want to know in that if block itself how to assign a variable and prints once condition is true. 注意:使用if条件每个变量都可以知道哪个变量为空,但是我不想用if-else条件扩展行,我只是想知道if块本身如何分配变量,条件为真时打印。
Can anybody help me how to get empty variable..? 有人可以帮助我如何获取空变量吗? (/bin/bash)
(/ bin / bash)
If all you are doing is checking existence with the if you could use a function. 如果您正在做的就是使用来检查是否可以使用函数。
check() {
for i in "$@";do
if [[ -z "${!i}" ]]
then
echo -e "Must Provide All Required Paths [\$$i is Empty].."
exit 1
fi
done
}
check MMBOX_PATH BACKUP_PATH REMOTE_SERVER LOG_PATH
Shell already provides a syntax for verifying that a variable has a value and exits if it does not: Shell已经提供了一种语法,用于验证变量是否具有值,如果没有,则退出:
: ${MMBOX_PATH:?Must provide MMBOX_PATH}
: ${BACKUP_PATH:?Must provide BACKUP_PATH}
: ${REMOTE_SERVER:?Must provide REMOVE_SERVER}
: ${LOG_PATH:?Must provide LOG_PATH}
There's no need to define a check
function that does the same thing. 无需定义执行相同功能的
check
功能。
The initial colon is the do-nothing command; 最初的冒号是什么都不做的命令。 the shell evaluates its arguments, and
:
exits with status 0 immediately. shell评估其参数,然后
:
立刻以状态0退出。 The parameter expansion is what verifies that the named parameter has a value. 参数扩展是验证命名参数是否具有值的内容。 If it does not, the given error message is printed.
如果不是,则打印给定的错误消息。 If the shell is not interactive, it also exits with status 1.
如果外壳不是交互式的,则它也以状态1退出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.