LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-07-2007, 09:56 AM   #1
juchestyle
LQ Newbie
 
Registered: Jan 2007
Location: Orlando Florida
Posts: 3

Rep: Reputation: 0
Writing a bash script to find and register vmware vms


Hey guys,

I am new to scripting but I think this is an easy question.

In the script below I think I have the second line wrong. Is there a better way to write this? I am trying to discover all vmx files, then register each one. I have it getting the state right now until I can work out the "for vm in $findvm" part.

Your help is greatly appreciated!


#!/bin/bash

findvm="find /vmfs/volumes -name *.vmx"
for vm in $findvm
do vmware-cmd "$vmx" getstate
done
exit



Respectfully,
 
Old 01-07-2007, 10:13 AM   #2
juchestyle
LQ Newbie
 
Registered: Jan 2007
Location: Orlando Florida
Posts: 3

Original Poster
Rep: Reputation: 0
just in case I wasn't clear.

I want to collect a bunch of names of servers (server1.vmx, server2.vmx, etc.)

Then I want to use that next line to take that list of servers and get the power state.

I am getting an error when I run the current script and I believe I am not using the right syntax in that second line to get the program to take that list and get the power state for each found server.vmx.

Your help is greatly appreciated!

Respectfully,
 
Old 01-07-2007, 10:17 AM   #3
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
You're right, second line:

for i in $findvm;

else do and so on is regarded as other words to iterate $i over.
 
Old 01-07-2007, 10:28 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Code:
 #!/bin/bash
find /vmfs/volumes -name '*.vmx' -exec vmware-cmd "{}" getstate \;
I think that would be a better way of doing it, but isn't the problem with the original script the first line which should be
Code:
findvm=$(find /vmfs/volumes -name *.vmx)
As I understand it using quotes would make the for loop iterate over the words "find", then "/vmfs/volumes", and so on.

And you probably meant
Code:
do vmware-cmd "$vm" getstate

Last edited by ntubski; 01-07-2007 at 10:32 AM.
 
Old 01-07-2007, 10:38 AM   #5
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Oops, took " for ` when glancing through...
 
Old 01-07-2007, 11:36 AM   #6
juchestyle
LQ Newbie
 
Registered: Jan 2007
Location: Orlando Florida
Posts: 3

Original Poster
Rep: Reputation: 0
Raskin and ntubski, you help is invaluable, thank you so much!

So here is what I have now:


#!/bin/bash
find /vmfs/volumes -name '*.vmx' -exec vmware-cmd "{}" getstate \;
done
exit


This seems to be working much better. The output looks like this:


getstate() = off
getstate() = off
getstate() = on
getstate() = off


How do I get it to list the servername and then the state, for example:

server1.vmx = off

Second question, is that done need to be there or is there better syntax for it? When I ran the program it ended like this:

./tryimprove.vbs: line 3: syntax error near unexpected token `done'
./tryimprove.vbs: line 3: `done'
[root@ESX MattsMadDatastore]# getstate() = off
-bash: syntax error near unexpected token `='
[root@ESX MattsMadDatastore]# getstate() = on
-bash: syntax error near unexpected token `='
[root@ESX MattsMadDatastore]# getstate() = off
-bash: syntax error near unexpected token `='
[root@ESX MattsMadDatastore]# getstate() = off


Respectfully,
Matthew
 
Old 01-07-2007, 12:03 PM   #7
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
``done'' was needed as a pair for ``do'' when you used ``for''. Surely, now only
Code:
find /vmfs/volumes -name '*.vmx' -exec vmware-cmd "{}" getstate \;
is needed.
I would make one more script to do something like
vmware-cmd "$1" getstate | sed -e 's/^getstate[(][)]/'"$1"'/'
and put it instead vmware-cmd in your script.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash (help writing script) lebabyg Linux - General 7 07-04-2006 05:22 PM
writing a bash script poiuytrewq Linux - Newbie 2 10-07-2004 10:26 PM
Help writing a pinging bash script dehuszar Linux - Software 3 06-18-2004 02:03 PM
Writing to a file - Bash script Skute Programming 2 03-15-2004 04:41 AM
writing bash script ankitgdit Programming 4 08-19-2003 06:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration