编写根据输出分支的bash脚本

时间:2013-06-21 23:12:36

标签: bash

我需要做什么:

  1. 在命令行中运行“Art list --pool = prototype | grep tg”(输出命令示例:http://i.imgur.com/wyb57uB.png

  2. 从第三列为“AutoTestd @ atest”或“”的输出中查找第一行,并且在字符']'之后没有单词(例如,以示例中的tg106开头)

  3. 如果有,请运行“Art grab --pool = prototype tg106”(使用找到的行的前5个字符)

  4. 如果不存在,请等待5秒钟,然后返回1.

  5. 这就是我目前所拥有的:

    #!/bin/bash
    
    echo "Hello World!"
    grabbed=false
    while !grabbed do
       for /f "tokens=1,5,9" %%i in ('Art list --pool=prototype | grep tg') do
          if ("%%j" == "AutoTestd@atest" || "%%j" == "<free>") then
             echo "found"
             # grab a DUT
             Art grab --pool=prototype %%i
             grabbed=true;
          else
             echo "trying again..."
          fi
       done
       sleep 5
    done
    

    但是,我在尝试运行时遇到这些错误:

    〜@ bs340.sjc&gt; ./my_script

    Hello World!

    ./ my_script:第6行:意外令牌附近的语法错误“”令牌= 1,5,9“'

    ./ my_script:第6行:`for / f“tokens = 1,5,9”%% i in('Art list --pool = prototype | grep tg')do'

1 个答案:

答案 0 :(得分:2)

# Until we find a line containg word1 or word2, and ending with a ]
until line=$(command1 | grep -E '(word1|word2).*]$' | head -n 1); [[ $line ]]
do
    # Sleep and try again
    sleep 5
done

#Run the final command with the first 5 chars from that line
command2 "${line:0:5}"