【问题标题】:How to write complex if in ksh如何在ksh中编写复杂的if
【发布时间】:2017-04-08 11:47:52
【问题描述】:

我不经常用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

谁能给我写下来的权利

谢谢

【问题讨论】:

    标签: unix if-statement ksh


    【解决方案1】:

    如果我们从变量的开头删除字符串“no rows selected”, 我们只需要测试一个空字符串。

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

    用法:

    if empty_or_no_rows_selected $NUMCARSAT
    then
        : ....
    

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

    【讨论】:

      【解决方案2】:

      简单直接的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
      

      【讨论】:

      • 这是否意味着“非空”?
      • 修剪在哪里?
      • @mlwacosmos:修剪是什么意思,上面检查变量$NUMCARSAT是否为空或有你提到的字符串。要检查字符串是否不为空,请执行 [[ ! -z "${NUMCARSAT}" ]]
      • 我想删除字符串值前后的空格...做一个修剪
      猜你喜欢
      • 2014-02-20
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多