if 条件变量逻辑运算符
if condition with variable logical operator
让我问一下,因为我已经尝试了几天,我不知道我的想法是否可行。
我在文件 1 中有以下文本:
aaa common-criteria policy POLICY1
min-length 8
max-length 16
numeric-count 1
upper-case 3
lower-case 2
char-changes 4
!
和文件 2 中的以下文本:
aaa common-criteria policy POLICY2
min-length 1
max-length 127
char-changes 4
!
并且我想根据我的策略中的阈值检查两个文件中的所有值是否正确。
所以我创建了两个带有阈值的数组,以便将每个值与每个文件中的值进行比较
declare -a COM=("min-length" "max-length" "upper-case" "lower-case" "numeric-count")
declare -a OPE=("-ge" "-le" "-ge" "-ge" "-ge")
declare -a VAL=("8" "16" "1" "1" "1")
并且我创建了循环来检查文件中的每个值与 VAL(i)
中的值
for ((i=0; i<${#COM[@]}; i++)); do
THRESHOLD=$(grep ${COM[$i]} FILE | awk -F " " '{print }') --> to get value VAL(i) for each comand COM(i)
if [[ $THRESHOLD -ge "${VAL[$i]}" ]]; then echo "OK"; else echo "KO"; fi
done
下一步应该是在 if 条件下将“-ge”更改为 OPE(i),以允许以正确的方式分析每个命令,因为“max-length”值应小于阈值。
如果我将“-ge”更改为 ${OPE[$i]}
如果 [[ "$THRESHOLD" "${OPE[$i]}" "${VAL[$i]}" ]]
该过程失败“条件二元运算符在 `${OPE[$i]}'
附近出现预期的语法错误
你知道“if”条件是否有这种灵活的句法吗?
谢谢!
[[ ... ]]
在变量展开之前以一种特殊的方式解析,因此这是不可能的(这也是为什么你不需要总是双引号 [[ ... ]]
中的变量)。但是您可以使用遵循正常解析规则的旧 [ ... ]
。
op==
[ x "$op" x ] # ok
可以将 test
command 与作为变量传递的参数一起使用:
test
命令示例:
v1=1; op=" -eq "; v2=1
if test "$v1 $op $v2"; then echo true; else echo false; fi
true
v1=1; op=" -eq "; v2=0
if test "$v1 $op $v2"; then echo true; else echo false; fi
false
建议在 gawk
脚本中使用关联数组,这是 Linux 机器中的标准 awk
:
script.awk
function boolExprTest(expr) { # function to test bool exression provided as input string
split(expr, exprToken); # split input string into tokens
# return boolean expression result for each 2nd token
if (exprToken[2] == ">=") return exprToken[1] >= exprToken[3];
if (exprToken[2] == "<=") return exprToken[1] <= exprToken[3];
if (exprToken[2] == ">") return exprToken[1] > exprToken[3];
if (exprToken[2] == "<") return exprToken[1] < exprToken[3];
if (exprToken[2] == "==") return exprToken[1] == exprToken[3];
}
BEGINFILE { # at the beging of each input file read
# define boolean expression templates using associative arry boolExpr[]
boolExpr["min-length"] = " >= 8";
boolExpr["max-length"] = " <= 16";
boolExpr["upper-case"] = " >= 1";
boolExpr["lower-case"] = " >= 1";
boolExpr["numeric-count"] = " >= 1";
# define current file's results using associative arry currResults[]
currResults["min-length"] = "undefined";
currResults["max-length"] = "undefined";
currResults["upper-case"] = "undefined";
currResults["lower-case"] = "undefined";
currResults["numeric-count"] = "undefined";
# print file seperator
print "---------------+---------------+-------------";
}
# for each line in current file
in boolExpr { # if 1st field is a member in boolExpr[]
currResults[] = "fail"; # default result is fail in currResults[]
if (boolExprTest( boolExpr[])) { # used function boolExprTest () to test " boolExpr[]"
currResults[] = "pass"; # set pass result in currResults[]
}
}
ENDFILE { # at the end of each input file read
for (argument in currResults) { # iterate results
#fromat print the results
printf("%-15s|%-15s|%s\n", FILENAME, argument, currResults[argument]);
}
}
输出:
$ awk -f script.awk input.*.txt
---------------+---------------+-------------
input.1.txt |numeric-count |pass
input.1.txt |max-length |pass
input.1.txt |lower-case |pass
input.1.txt |min-length |pass
input.1.txt |upper-case |pass
---------------+---------------+-------------
input.2.txt |numeric-count |undefined
input.2.txt |max-length |fail
input.2.txt |lower-case |undefined
input.2.txt |min-length |fail
input.2.txt |upper-case |undefined
让我问一下,因为我已经尝试了几天,我不知道我的想法是否可行。
我在文件 1 中有以下文本:
aaa common-criteria policy POLICY1
min-length 8
max-length 16
numeric-count 1
upper-case 3
lower-case 2
char-changes 4
!
和文件 2 中的以下文本:
aaa common-criteria policy POLICY2
min-length 1
max-length 127
char-changes 4
!
并且我想根据我的策略中的阈值检查两个文件中的所有值是否正确。 所以我创建了两个带有阈值的数组,以便将每个值与每个文件中的值进行比较
declare -a COM=("min-length" "max-length" "upper-case" "lower-case" "numeric-count")
declare -a OPE=("-ge" "-le" "-ge" "-ge" "-ge")
declare -a VAL=("8" "16" "1" "1" "1")
并且我创建了循环来检查文件中的每个值与 VAL(i)
中的值for ((i=0; i<${#COM[@]}; i++)); do
THRESHOLD=$(grep ${COM[$i]} FILE | awk -F " " '{print }') --> to get value VAL(i) for each comand COM(i)
if [[ $THRESHOLD -ge "${VAL[$i]}" ]]; then echo "OK"; else echo "KO"; fi
done
下一步应该是在 if 条件下将“-ge”更改为 OPE(i),以允许以正确的方式分析每个命令,因为“max-length”值应小于阈值。 如果我将“-ge”更改为 ${OPE[$i]} 如果 [[ "$THRESHOLD" "${OPE[$i]}" "${VAL[$i]}" ]] 该过程失败“条件二元运算符在 `${OPE[$i]}'
附近出现预期的语法错误你知道“if”条件是否有这种灵活的句法吗? 谢谢!
[[ ... ]]
在变量展开之前以一种特殊的方式解析,因此这是不可能的(这也是为什么你不需要总是双引号 [[ ... ]]
中的变量)。但是您可以使用遵循正常解析规则的旧 [ ... ]
。
op==
[ x "$op" x ] # ok
可以将 test
command 与作为变量传递的参数一起使用:
test
命令示例:
v1=1; op=" -eq "; v2=1
if test "$v1 $op $v2"; then echo true; else echo false; fi
true
v1=1; op=" -eq "; v2=0
if test "$v1 $op $v2"; then echo true; else echo false; fi
false
建议在 gawk
脚本中使用关联数组,这是 Linux 机器中的标准 awk
:
script.awk
function boolExprTest(expr) { # function to test bool exression provided as input string
split(expr, exprToken); # split input string into tokens
# return boolean expression result for each 2nd token
if (exprToken[2] == ">=") return exprToken[1] >= exprToken[3];
if (exprToken[2] == "<=") return exprToken[1] <= exprToken[3];
if (exprToken[2] == ">") return exprToken[1] > exprToken[3];
if (exprToken[2] == "<") return exprToken[1] < exprToken[3];
if (exprToken[2] == "==") return exprToken[1] == exprToken[3];
}
BEGINFILE { # at the beging of each input file read
# define boolean expression templates using associative arry boolExpr[]
boolExpr["min-length"] = " >= 8";
boolExpr["max-length"] = " <= 16";
boolExpr["upper-case"] = " >= 1";
boolExpr["lower-case"] = " >= 1";
boolExpr["numeric-count"] = " >= 1";
# define current file's results using associative arry currResults[]
currResults["min-length"] = "undefined";
currResults["max-length"] = "undefined";
currResults["upper-case"] = "undefined";
currResults["lower-case"] = "undefined";
currResults["numeric-count"] = "undefined";
# print file seperator
print "---------------+---------------+-------------";
}
# for each line in current file
in boolExpr { # if 1st field is a member in boolExpr[]
currResults[] = "fail"; # default result is fail in currResults[]
if (boolExprTest( boolExpr[])) { # used function boolExprTest () to test " boolExpr[]"
currResults[] = "pass"; # set pass result in currResults[]
}
}
ENDFILE { # at the end of each input file read
for (argument in currResults) { # iterate results
#fromat print the results
printf("%-15s|%-15s|%s\n", FILENAME, argument, currResults[argument]);
}
}
输出:
$ awk -f script.awk input.*.txt
---------------+---------------+-------------
input.1.txt |numeric-count |pass
input.1.txt |max-length |pass
input.1.txt |lower-case |pass
input.1.txt |min-length |pass
input.1.txt |upper-case |pass
---------------+---------------+-------------
input.2.txt |numeric-count |undefined
input.2.txt |max-length |fail
input.2.txt |lower-case |undefined
input.2.txt |min-length |fail
input.2.txt |upper-case |undefined