Im trying to write a /bin/bash script that refreshes VMware Fusion using the built in snapshot capabilities.
I am having an issue getting a variable to pass into the find argument of an "if-then" statement.
Im thinking the problem might have something to do with the working directory of the script, not being able to find the file within its location.
#!/bin/bash
export PATH="$PATH:/Library/Application Support/VMware Fusion"
# Path to the VMware Bundle file
vmpath="/Users/xxxx/Documents/Virtual Machines.localized/Ubuntu.vmwarevm/"
echo "$vmpath"
# Path to VMware Image file
vmxpath="/Users/xxxx/Documents/Virtual Machines.localized/Ubuntu.vmwarevm/Ubuntu.vmx"
echo "$vmxpath"
# If this variable is found it means that VMware is currently hosting an Image
vmopen=`ls "/Users/xxxx/Documents/Virtual Machines.localized/Ubuntu.vmwarevm/" | grep -m2 "00000" | grep ".lck"`
echo "$vmopen"
# This argument determines if VMware is running and then defines the parameters for
# "Refreshing" the VMware by "Rolling Back" to a previously created snapshot called "Snapshot"
cd "/Users/xxxx/Documents/Virtual Machines.localized/Ubuntu.vmwarevm/"
if [ -f "$vmopen" ]; then
echo "then"
vmrun -T ws stop "$vmxpath" hard
osascript -e 'quit application "VMware Fusion"'
vmrun -T ws revertToSnapshot "$vmxpath" SnapShot
else
echo "else"
vmrun -T ws revertToSnapshot "$vmxpath" SnapShot
fi
exit 0
Like I said, I think the problem has something to do with this part:
if [ -f "$vmopen" ]; then
just passing that part reveals the item I need, but not the path to the item.
However if I add the path in front of the variable, then I just get the path.
I've tried changing the directory to the source of the file, but honestly I've never used a "cd" command in a script, so im not even sure if you can do that.
If any body had any ideas, it would be much appreciated.
If your system has this option, be sure $vmopen is not empty (compare to "", or with -z in 'if ..'
if [[ $vmopen == "" ]]; then echo IT_IS EMPTY; else echo IT_IS_NOT_EMPTY; fi # or
if [[ -z $vmopen ]]; then echo IT_IS EMPTY; else echo IT_IS_NOT_EMPTY; fi
Is that is your question?
Also, you are right, to check a file for existance you need or to be in that directory, or to check with path. You already have it (I do not know why you do not use already defined variables)
when VMware Fusion is running a OS, there is a file called "Ubuntu-000001.vmdk.lck"
the problem is, is that this file changes names, otherwise I would just reference the file.
When vm is running the contents of the Ubuntu.vmwarevm/ is:
-rw-rw-rw-@ 1 xxxxxx staff 7405568 Nov 5 22:34 Ubuntu-000001.vmdk
drwxrwxrwx 3 xxxxxx staff 102 Nov 5 22:32 Ubuntu-000001.vmdk.lck
-rw-rw-rw-@ 1 xxxxxx staff 536870912 Nov 5 20:30 Ubuntu-Snapshot2.vmem
-rw-rw-rw-@ 1 xxxxxx staff 68563738 Nov 5 20:30 Ubuntu-Snapshot2.vmsn
-rw-rw-rw-@ 1 xxxxxx staff 8684 Nov 5 22:32 Ubuntu.nvram
-rwxrwxrwx@ 1 xxxxxx staff 3941924864 Nov 5 20:30 Ubuntu.vmdk
drwxrwxrwx 3 xxxxxx staff 102 Nov 5 22:32 Ubuntu.vmdk.lck
-rw-rw-rw-@ 1 xxxxxx staff 536870912 Nov 5 21:33 Ubuntu.vmem
drwxrwxrwx 3 xxxxxx staff 102 Nov 5 22:32 Ubuntu.vmem.lck
-rwxrwxrwx@ 1 xxxxxx staff 800 Nov 5 20:30 Ubuntu.vmsd
-rw-rw-rw-@ 1 xxxxxx staff 66693236 Nov 5 22:32 Ubuntu.vmss
-rwxrwxrwx@ 1 xxxxxx staff 2533 Nov 5 22:32 Ubuntu.vmx
drwxrwxrwx 3 xxxxxx staff 102 Nov 5 22:32 Ubuntu.vmx.lck
-rw-rw-rw-@ 1 xxxxxx staff 16634 Nov 5 21:24 Ubuntu.vmxf
-rw-r--r-- 1 xxxxxx staff 5221 Nov 5 22:32 quicklook-cache.png
-rw-r--r-- 1 xxxxxx staff 77570 Nov 5 21:51 vmware-0.log
-rw-r--r-- 1 xxxxxx staff 76195 Nov 5 21:24 vmware-1.log
-rw-r--r-- 1 xxxxxx staff 77060 Nov 5 21:16 vmware-2.log
-rw-r--r-- 1 xxxxxx staff 74671 Nov 5 22:32 vmware.log
When it isnt running its:
-rw-rw-rw-@ 1 xxxxxx staff 7405568 Nov 5 22:34 Ubuntu-000001.vmdk
-rw-rw-rw-@ 1 xxxxxx staff 536870912 Nov 5 20:30 Ubuntu-Snapshot2.vmem
-rw-rw-rw-@ 1 xxxxxx staff 68563738 Nov 5 20:30 Ubuntu-Snapshot2.vmsn
-rw-rw-rw-@ 1 xxxxxx staff 8684 Nov 5 22:34 Ubuntu.nvram
-rwxrwxrwx@ 1 xxxxxx staff 3941924864 Nov 5 20:30 Ubuntu.vmdk
-rw-rw-rw-@ 1 xxxxxx staff 536870912 Nov 5 21:33 Ubuntu.vmem
-rwxrwxrwx@ 1 xxxxxx staff 800 Nov 5 20:30 Ubuntu.vmsd
-rw-rw-rw-@ 1 xxxxxx staff 66693236 Nov 5 22:34 Ubuntu.vmss
-rwxrwxrwx@ 1 xxxxxx staff 2544 Nov 5 22:34 Ubuntu.vmx
-rw-rw-rw-@ 1 xxxxxx staff 16634 Nov 5 21:24 Ubuntu.vmxf
-rw-r--r-- 1 xxxxxx staff 0 Nov 5 22:34 quicklook-cache.png
-rw-r--r-- 1 xxxxxx staff 77570 Nov 5 21:51 vmware-0.log
-rw-r--r-- 1 xxxxxx staff 76195 Nov 5 21:24 vmware-1.log
-rw-r--r-- 1 xxxxxx staff 77060 Nov 5 21:16 vmware-2.log
-rw-r--r-- 1 xxxxxx staff 77214 Nov 5 22:34 vmware.log
rubin is correct about multiple return. The check wil failed if more than one file grepped.
Use, for example, "sed '1q'" - so, quiting after getting the first line.
Put it after last grep.
Actually, what about (use already defined, and no reason in greps )