Bash脚本,如果逻辑为if,则在逻辑上与ands和ors [英] Bash script with logical ands, and ors in an if elif else statement
问题描述
我已经搜索并找到了很多if elif函数的示例,但我发现似乎都没有包含逻辑AND或OR运算符。
I've searched and found a lot of examples of if elif functions, but none I have found seem to also include logical AND or OR operators.
I' m在bash脚本中遇到以下if语句时遇到问题:
I'm having trouble with the following if statement in a bash script:
#!/bin/bash
BASE="$(basename "${PWD}")"
REPO="$(cut -d/ -f4 <<<"${PWD}")"
if [ "$BASE" = "$REPO" ] -o [ "$BASE" = "" ]
then
echo "[$REPO]"
elif [ "$REPO" = "" ] -a [ "$BASE" != "" ]
then
echo "$BASE"
else
echo "[$REPO] $BASE"
fi
我正在接受ol' bash:[:参数过多
IF和ELIF都出错,我似乎无法使其正常工作。 bash脚本是否不喜欢它们的IF语句来弄复杂呢?
I'm getting the ol' bash: [: too many arguments
error for both the IF and the ELIF, and I can't seem to get it to work. Do bash scripts not like their IF statements to get this complex? Doesn't seem overly optimistic to expect this level of comparison, is it?
推荐答案
您正在混合<$ c的语法$ c> [ aka test
和Bash本身。在 [
和]
之间, -a
是布尔值AND ,但这只是该特定命令的功能。
You are mixing the syntax of [
aka test
and Bash itself. Between [
and ]
, -a
is a boolean AND, but it is only a feature of this particular command.
在一般情况下, command1&&仅当
将执行 command1
返回成功时,command2 command2
,类似地, command1 ||仅当
才会执行 command1
返回失败时,command2 command2
。
In the general case, command1 && command2
will execute command2
only if command1
returns success, and similarly, command1 || command2
will execute command2
only if command1
returns failure.
所以你可以说
if [ condition1 -a condition2 ]
或
if [ condition1 ] && [ condition2 ]
但是无论如何,您的代码看起来确实是您想要的 case
代替。按照您的脚本的逻辑,我遇到了一个问题,但是可以将其简化很多,因为实际上在任何情况下都不能使用 basename $ PWD
空字符串(在根目录中,基本名称为 /
)。另外,在 elif
分支中,您已经知道 $ BASE
不为空(现在忽略它的事实)永远不会为空),因为如果是的话, if
分支将已经被采用。
But anyway, your code looks like you actually want case
instead. I'm having a bit of an issue following the logic of your script, but it can be simplified quite a lot, because there are actually no situations where basename $PWD
can be the empty string (in the root directory, the basename is /
). Also, in the elif
branch, you already know that $BASE
is not empty (ignoring for now the fact that it never will be empty), because if it were, the if
branch would have been taken already.
假设您要检查我们是在根目录还是在下四级的目录中;或(b)在小于四级的非根目录中;否则在其他地方(c)(这意味着目录下移五个或更多级别),请尝试此操作。因为 case
将采用第一个匹配的分支(请记住, *
甚至会匹配包含斜杠的字符串)
Anyway, assuming you want to check whether we are in (a) the root directory or a directory exactly four levels down; or (b) in a non-root directory less than four levels down; or else (c) somewhere else (which means a directory five or more levels down), try this. Because case
will take the first matching branch (keeping in mind that *
will match even a string containing a slash in this context if necessary), I had to reorder these slightly.
case $PWD in
/*/*/*/*/* ) # more than 4 (c)
echo "[$REPO] $BASE" ;;
/ | /*/*/*/* ) # root or exactly 4 (a)
echo "[$REPO"] ;;
*) # else, less than 4, not root (b)
echo "$BASE" ;;
esac
这篇关于Bash脚本,如果逻辑为if,则在逻辑上与ands和ors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!