My kingdom for a small simple script

So, let me start off by saying that I’m neither a scripter or a programmer. It’s not for a lack of wanting to learn. I’ve started a few times but got side track by one thin… oh! Look! Squirrel!

Wait.. where was I?

Oh ya. Scripting. In some ways, it’s very surprising that I’ve never spent more time on scripting/programming. I understand it and can, given a bit of time, figure out what any script and most programs (based on code) are designed to do and why.

I’ve been working on my prep to do the VCAP-DCA 5 (I did the beta with no prep and not surprisingly, didn’t pass). So as I began working on the labs from the Troubleshooting course, I noticed that in one of the labs there were a series of questions that asked:

  • List all the virtual machines
  • Do any of the virtual machines have snapshots?
  • What is the current state of the virtual machines?

And I thought, well.. I could type out the command multiple times or I could do something short and sweet for each that I might be able to repeat but alter slightly. Now, I’m fairly proficient in *nix commands (e.g., Linux, Unix, Solaris, etc. are all favourite OS/platforms of mine) and can navigate fairly well through anything. Using grep and pipes and the like is fairly comfortably. So I thought.. piping the results from the list into the snapshot request might work but probably not because it really needs to be a variable. I double-checked to be sure. So I tried vmware-cmd -l | vmware-cmd hassnapshot.

I got back the help options for vmware-cmd. Definitely not the right way.

So I pinged my colleague Brian Watrous for some advice since he’s really, really good at this stuff and I figured it’d give me a place to start, at the least. So without knowing where I was doing this really on, he suggested a for loop and we started with this:

for each vm in `vmware-cmd –l`
do
echo “Hmmm… I wonder if $vm has a snapshot.”
vmware-cmd $vm hassnapshot
done

I thought, “PERFECT!”. It was simple, to the point and perfect for what I was looking for. Although the echo line is really optional, it gave a good indication to me where it was in the list. So I started with typing in the first line since the system should realize that there would be additional lines following (this is identified by the > as a prompt).

The system (vSphere Management Appliance) wasn’t none-to-happy about this. Instead of cheerily responding with a > it complained:

-bash: syntax error near unexpected token `vm'

I thought, hrmmm.. Maybe it’s a spacing issue and tried replacing the “for each” with “foreach” (since both are conventions used in other scripting languages).

Still didn’t work. The same error glared angrily at me, taunting me. I re-read it and then it dawned on me. It was something before the variable of vm.

Ah-ha. I retried the first line without the “each”. And voila! it worked.

So I learned a simple little script that I could use over and over again in this kind of scenario. Now, keep in mind I wasn’t looking for some longer script that would be used day-in and day-out but more of something simple for my particular situation and to practise with. And it’s always good to practise. I could have investigated doing this in perl, java or some other language but it would have been far more work than necessary.

So for anyone else who wondered.. now you know.

 

Your email address will not be published. Required fields are marked *