1

I want to run my VirtualBox after my I logged in. To do that, I have to check if it's already running or not. If not, then start the VM.

My problem is that I can not put the result of VirtualBox command into a variable in Bash.

I've created a function to get the exit parameter, but I get a "Syntax error: Invalid parameter '|'" error message.

How can I make it work?

This is my Bash script:

########## Starting om-server VirtualBox ##############

function Run_VM {
    "$@"
    local status=$?
    if [ $status -eq 0 ]; then
        echo "Starting om-server VM!";
        VBoxManage startvm "om-server"
        sleep 30
        ssh root@192.168.1.111
    else
        echo "om-server VM is running!"
    fi
    return $status
}


check_vm_status="VBoxManage showvminfo \"om-server\" | grep -c \"running (since\""

Run_VM $check_vm_status

########## Starting om-server VirtualBox ##############
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohammad_Hosseini
  • 2,481
  • 3
  • 30
  • 53

1 Answers1

1

In order to do what you want, you would have to use command substitution:

check_vm_status="$(VBoxManage showvminfo \"om-server\" | grep -c \"running (since\")"

The caveat is that the instructions will be executed during the variable expansion (i.e. during the variable allocation).

If you want to execute your instructions only during your function, you could use a eval:

function Run_VM {
        eval "$@"
        local status=$?
        if [ $status -eq 0 ]; then
                echo "Starting om-server VM!";
                VBoxManage startvm "om-server"
                sleep 30
                ssh root@192.168.1.111
        else
                echo "om-server VM is running!"
        fi
        return $status
}


check_vm_status="VBoxManage showvminfo \"om-server\" | grep -c \"running (since\""


Run_VM $check_vm_status

Note that eval brings a lot of issues.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aserre
  • 4,916
  • 5
  • 33
  • 56
  • 1
    I think the OP wants to run the command in the function, with the `"$@"`. – Some programmer dude Jul 24 '17 at 08:02
  • @Aserre I've changed my script to yours and I got this errors : bash: .bashrc: line 154: unexpected EOF while looking for matching `"' bash: .bashrc: line 159: syntax error: unexpected end of file – Mohammad_Hosseini Jul 24 '17 at 08:21
  • @Mohammad_Hosseini this means you forgot a `"` somewhere in the code – Aserre Jul 24 '17 at 08:22
  • I removed () from the check_vm_status, and now It ran but give this error : Syntax error: Invalid parameter '|' – Mohammad_Hosseini Jul 24 '17 at 08:22
  • @Aserre ok eval solved the problem but the strange thing is when it runs it gives 1 as output but in the if statement the part which is if [ $status -eq 0 ] is executing ! why ? – Mohammad_Hosseini Jul 24 '17 at 08:26
  • @Mohammad_Hosseini I'm not sure what you mean : if the `eval "$@"` displays `1`, then its return code is `0` (success), because the last function executed is `grep`. – Aserre Jul 24 '17 at 08:30
  • I don't know why when I changed 0 to 1 in if statement problem solved. it's a bit strange. but solved my problem. – Mohammad_Hosseini Jul 24 '17 at 08:31