检查鱼的流浪状态

时间:2019-03-08 07:02:06

标签: bash shell vagrant fish

我正在尝试编写一个鱼脚本,以检查当前的无业游民状态,并根据此状态执行某些操作。一个简单的例子是:

检查是否有无业游民在运行,如果是,则无业游民停止,如果没有,则进行无业游民。

我想出了类似的东西

# Go to vagrant folder
cd "/vagrant/folder"

# Set status
set status(vagrant status --machine-readable | grep state,running)

# Check status
if  [ status != "" ] 
vagrant halt

# Send notification
notify-send "Vagrant is halted."

else

vagrant up

# Send notification
notify-send "Vagrant is up."

end

我不知道该字符串比较是否可行,或者是否存在更整洁,更精确的方法来检查流浪者状态。

1 个答案:

答案 0 :(得分:0)

找到带有test和$ status的解决方案

# Get status
vagrant status --machine-readable | grep state,running

# Check status
if test $status -eq 0

    # Vagrant is running
    vagrant halt

    # Send notification
    notify-send "Vagrant is halted."

else

    # Vagrant is not running
    vagrant up

    # Send notification
    notify-send "Vagrant is up."

end