if [ -f "$variable" ]; then issues, help!

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.

Thanks!!!

Can you post the output of :

ls /Users/xxxx/Documents/Virtual Machines.localized/Ubuntu.vmwarevm/ | grep -m2 "00000" | grep ".lck"

Also a plain output of :

ls /Users/xxxx/Documents/Virtual Machines.localized/Ubuntu.vmwarevm/

Sure you could 'cd ..' any time in script.
The 'grep -m2 ...' - I do not know this option

c> grep -m2 boo
/usr/xpg4/bin/grep: illegal option -- m
Usage:  grep [-E|-F] [-c|-l|-q] [-bhinsvwx] [file ...]
grep [-E|-F] [-c|-l|-q] [-bhinsvwx] -e pattern... [-f pattern_file]...[file...]
grep [-E|-F] [-c|-l|-q] [-bhinsvwx] [-e pattern]... -f pattern_file [file...]

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)

if [ -f "$vmpath$vmopen" ]; then ....; fi;

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

Thats interesting, I guess the -m2 doesn't do anything.

I had -m1, at one point, but that may have been from a previous script.

I dont think I need it after I've added the second "grep"

Thanks, now as far as your last point.

if [ -f "$vmpath$vmopen" ]; then ....; fi; 

thats the exact code that I was using, however when the file doesnt exist, like when VMware Fusion is off. I get this passed to the find"

/Users/xxxxxx/Documents/Virtual Machines.localized/Ubuntu.vmwarevm/ 

since the $vmopen did not exist, a non value was returned, and that non value gets appended to $vmpath.

So thats where Im stuck.

so, check the $vmopen before use it:

if [[ ! -z $vmopen && -f "$vmpath$vmopen" ]]; then ...; fi; 

Also, the "Ubuntu-000001.vmdk.lck" (that you attempt to grep from 'ls ..') is a directory, not a file.
So:

> mkdir Ubuntu-000001.vmdk.lck
> if [[ -f "Ubuntu-000001.vmdk.lck" ]]; then echo da; else echo net; fi
net

Existance could be checked by (in ksh or in bash) by -e option or by combining -f .. || -d .. (so, is file or directory)

In your if test you're looking for a lock file ( .lck ), but from the output this seems to be a directory,

drwxrwxrwx 3 xxxxxx staff 102 Nov 5 22:32 Ubuntu-000001.vmdk.lck

Also make sure that in your if statement, you're testing for single lock file, not a few of them.

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 )

vmopen=`ls "$vmpath/*0000*.lck" | sed '1q'`

Thanks for the help. It actually ran fine after I changed the "-f" to a "-d"

The second grep in the "ls" finds the right .lck item (directory, not file)