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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if [ -f "$variable" ]; then issues, help!
# 1  
Old 11-05-2008
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.

Code:
#!/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:
Code:
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!!!
# 2  
Old 11-05-2008
Can you post the output of :

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

Also a plain output of :

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

# 3  
Old 11-05-2008
Sure you could 'cd ..' any time in script.
The 'grep -m2 ...' - I do not know this option
Code:
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 ..'
Code:
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)
Code:
if [ -f "$vmpath$vmopen" ]; then ....; fi;


Last edited by alex_5161; 11-05-2008 at 11:35 PM..
# 4  
Old 11-05-2008
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
# 5  
Old 11-05-2008
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.

Code:
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"

Code:
/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.
# 6  
Old 11-05-2008
so, check the $vmopen before use it:
Code:
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:
Code:
> 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)

Last edited by alex_5161; 11-06-2008 at 12:02 AM..
# 7  
Old 11-05-2008
In your if test you're looking for a lock file ( .lck ), but from the output this seems to be a directory,

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. UNIX for Dummies Questions & Answers

"Universal" sar option to check for paging issues?

Hi, Can anyone please advise a universal command option for using sar to check for paging/memory issues. For Linux, I used sar -B and check on the majflt/s column and a high number is supposed to indicate paging issues, is that correct? Unfortunately, on a Solaris server, sar -B does not... (1 Reply)
Discussion started by: newbie_01
1 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Solaris

BSM auditing issues, need to audit "permission denied"

Let me preface with I am semi-new to Solaris. I work with it in the labs at work and that's about my extent (although I run Linux at home). Well, a week ago security comes around with updated requirements, some of which are the need to audit all failures. For the life of me I cannot get a... (0 Replies)
Discussion started by: mph275
0 Replies

7. Shell Programming and Scripting

if [ "variable" = "numerical-range" ]; then

been a while so i'm a bit rusty and need a little help. writing a script that needs to compare $EXECHOST(a number) against a numerical range and then set a value. below isn't working but should give you folks an idea of my goal: if ; then echo "This is a 32B machine, exiting..." if ;... (4 Replies)
Discussion started by: crimso
4 Replies

8. AIX

aix 4.2 "compress" : any issues with files bigger then 1Gig ?

To speed up our backups, I found a way to compress all the backups files without running out of space. But before starting to use this in our procedures, I want to know if the command 'compress' has any issues in AIX 4.2 with files bigger then 1Gig. Our backup files have sizes ranging between 600Mg... (1 Reply)
Discussion started by: Browser_ice
1 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question