【问题标题】:How to run a command based on output of another command in shell script? [closed]如何根据 shell 脚本中另一个命令的输出运行命令? [关闭]
【发布时间】:2020-06-12 09:51:41
【问题描述】:

我有两个脚本。一个是 Python 脚本,如果一切都成功,那么它会打印 status=done,然后还有另一个名为“node-red”的服务。

现在如果它读取status=done,那么它会运行node-red。如果没有status-done,那么它会尝试再次运行 Python 脚本,直到收到status=done

我该怎么做?

#!/bin/sh
python3 /home/pi/cryptoauthtools/python/examples/mikro_file.py
pid=$!
echo "Waiting for python script to execute"
if [ "${pid}" = "status=done" ]; then
   echo Job 1 exited with status $?
   node-red
fi

【问题讨论】:

  • 你标记问题 bash,但不要写 sh-script!
  • 您对$! 的使用没有意义。请参阅标题为Special Parameters 的段落。 $! 在后台扩展为最后一个命令的 PID,但您不会在后台启动任何内容。
  • IMO,这只是礼貌。通常,如果我们遇到问题,我们会发布到目前为止的内容,我们至少能做的就是让它没有语法错误。如果我们不能做到这一点,我们会问一个不同类型的问题:“为什么我会在这里遇到语法错误?”。如果我只是将一些看起来像是程序的单词拼凑在一起,这有点像在问:“我不知道怎么写,所以请为我写吧”。

标签: python bash shell sh


【解决方案1】:

https://www.mylinuxplace.com/bash-special-variables/

$!最后一个后台命令的进程号。

您需要捕获命令的输出,而不是最后一个后台任务的 PID。

#!/bin/sh
echo "Waiting for python script to execute"
output=$(python3 /home/pi/cryptoauthtools/python/examples/mikro_file.py)
exit_code=$?

if [ "$output" = "status=done" ]; then
   echo "Job 1 exited with status $exit_code"
   node-red

还有 $?是最后一个命令的退出代码,包括任何“回声”等。所以你需要捕获$?在要捕获执行返回码的命令之后直接进入另一个变量

另外,回显行会在 python 脚本运行后执行,因此最好在执行脚本之前通知用户该命令。

【讨论】:

  • == 不受 sh 支持
  • 它说“意外的操作员”
  • 如果不成功如何重启python脚本?
【解决方案2】:

我可以让它像下面这样工作:

#!/bin/sh 
test="$(python3 test.py)"
echo "Waiting for python script to execute"
if [ "${test}" = "status=done" ]; then
    echo Job 1 exited with status $?
    echo "node-red"
else
  test="$(python3 test.py)"
  while [ "$test" != "status=done" ]
  do
    test="$(python3 test.py)"
  done
    echo "Waiting for python script to execute"
    if [ "${test}" = "status=done" ]; then
      echo Job 1 exited with status $? 
      echo "node-red"
    fi
fi

为了模拟不同的输出,我的 python 文件是这样的:

import random 

status = ['done', 'wip', 'clarify']

print(f'status={random.choice(status)}')

这是我得到的输出:

/tmp$ ./script.sh
Waiting for python script to execute
Job 1 exited with status 0
node-red

/tmp$ ./script.sh
Waiting for python script to execute
Job 1 exited with status 0
node-red

/tmp$ ./script.sh  
Waiting for python script to execute 
Waiting for python script to execute 
Waiting for python script to execute 
Job 1 exited with status 0
node-red

/tmp$ ./script.sh  
Waiting for python script to execute 
Job 1 exited with status 0
node-red

编辑:改进的循环

【讨论】:

    【解决方案3】:

    如果没有“status-done”,那么它会尝试再次运行 python 脚本,直到收到“status=done”

    echo "Waiting for python script to success!"
    while
          output=$(python3 /home/pi/cryptoauthtools/python/examples/mikro_file.py)
          [ "$output" != 'status=done' ]
    do
          echo "The python script did not output 'status=done', repeating..."
          sleep 1
    done
    echo "Yay! python succeeded. Let's run node-red"
    node-red
    

    虽然python脚本的输出不是status=done,但重复一遍。然后运行node-red

    【讨论】:

      猜你喜欢
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 2015-11-02
      • 2022-10-21
      • 2021-09-08
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多