如何在ksh中编写复杂的

时间:2016-11-24 09:02:31

标签: unix if-statement 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

有人可以给我权利,如果写下来

谢谢

2 个答案:

答案 0 :(得分:0)

只是一个直截了当的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

答案 1 :(得分:0)

如果我们删除字符串&#34;没有选择行&#34;从变量的开头, 我们只需要测试一个空字符串。

empty_or_no_rows_selected() {
  [[ -z "${1#no rows selected}" ]]
}

用法:

if empty_or_no_rows_selected $NUMCARSAT
then
    : ....

在参数替换中查找###%%%的工作情况。