在ksh中如何写复杂的if
How to write complex if in ksh
我不经常使用 ksh,也不知道怎么写:
if variable is empty or variable equals "no rows selected"
我试过了:
if [[ -z "${NUMCARSAT}" -o "$NUMCARSAT" | tr -s " " == "no rows selected" ]]
error = syntax error '-o' unexpected
if [ -z "${NUMCARSAT}" -o "$NUMCARSAT" | tr -s " " = "no rows selected" ]
error = test: ] missing
Usage: tr [ [-c|-C] | -[c|C]ds | -[c|C]s | -ds | -s ] [-A] String1 String2
tr { -[c|C]d | -[c|C]s | -d | -s } [-A] String1
谁能给我写下来的权利
谢谢
直截了当ksh
if-clause
if [[ -z "${NUMCARSAT}" ]] || [[ "$NUMCARSAT" != "no rows selected" ]]; then
要删除前导和尾随空格,您可以使用 xargs
和 here-doc(<<<
) 语法。即
if [[ -z "${NUMCARSAT}" ]] || [[ $(xargs <<< "$NUMCARSAT") != "no rows selected" ]];
看看这是否有效。
$ NUMCARSAT=" no rows selected "
$ xargs <<<"$NUMCARSAT"
no rows selected
即情况
$ if [[ $(xargs <<< "$NUMCARSAT") == "no rows selected" ]]; then echo "match"; fi
match
如果我们从变量的开头删除字符串 "no rows selected",
我们只需要测试一个空字符串。
empty_or_no_rows_selected() {
[[ -z "${1#no rows selected}" ]]
}
用法:
if empty_or_no_rows_selected $NUMCARSAT
then
: ....
在参数替换中查找 #
、##
、%
和 %%
的工作方式。
我不经常使用 ksh,也不知道怎么写:
if variable is empty or variable equals "no rows selected"
我试过了:
if [[ -z "${NUMCARSAT}" -o "$NUMCARSAT" | tr -s " " == "no rows selected" ]]
error = syntax error '-o' unexpected
if [ -z "${NUMCARSAT}" -o "$NUMCARSAT" | tr -s " " = "no rows selected" ]
error = test: ] missing
Usage: tr [ [-c|-C] | -[c|C]ds | -[c|C]s | -ds | -s ] [-A] String1 String2
tr { -[c|C]d | -[c|C]s | -d | -s } [-A] String1
谁能给我写下来的权利
谢谢
直截了当ksh
if-clause
if [[ -z "${NUMCARSAT}" ]] || [[ "$NUMCARSAT" != "no rows selected" ]]; then
要删除前导和尾随空格,您可以使用 xargs
和 here-doc(<<<
) 语法。即
if [[ -z "${NUMCARSAT}" ]] || [[ $(xargs <<< "$NUMCARSAT") != "no rows selected" ]];
看看这是否有效。
$ NUMCARSAT=" no rows selected "
$ xargs <<<"$NUMCARSAT"
no rows selected
即情况
$ if [[ $(xargs <<< "$NUMCARSAT") == "no rows selected" ]]; then echo "match"; fi
match
如果我们从变量的开头删除字符串 "no rows selected", 我们只需要测试一个空字符串。
empty_or_no_rows_selected() {
[[ -z "${1#no rows selected}" ]]
}
用法:
if empty_or_no_rows_selected $NUMCARSAT
then
: ....
在参数替换中查找 #
、##
、%
和 %%
的工作方式。