Search code examples
bashshellvagrantfish

Check vagrant status with fish


I am trying to write a fish script that checks on the current vagrant status and does something based on it. A simple example would be:

Check if vagrant is running, if yes do vagrant halt, if no do vagrant up.

I have come up with something like:

# 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

I do not know if that string comparison is the way to go or if there is a neater, more precise way to check the vagrant status.


Solution

  • Found the solution with test and $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