Results 1 to 2 of 2

Thread: Using Grep and regex to pull a value out of a string

  1. #1

    Using Grep and regex to pull a value out of a string

    Hi all

    I'm new to shell script and I want to write a script that checks to see how many of my virtual machines are running and if it is below the correct amount to send me an email

    the command i need to run is simply

    $ vmrun list

    this returns me

    Total Running VMs: 4

    so basically i want to write a script that says

    myvar = `vmrun list | grep [only get me the number, e.g. 4] | wc -l`

    if [ $myvar < 4 ] then
    email me a message at 'mymail@mail.com' saying 'not all vms are running'

    or even

    email me a message at 'mymail@mail.com' with the output from the command vmrun list

    If anyone could help me with the actual shell script then I'd be very happy

  2. #2
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811
    Awk will be better for this ie:
    Code:
    #!/bin/sh
    NUMBER=`vmrun list | awk '{print $NF}'`
    if [ $NUMBER le 3 ] ; then
      echo "Less than the desired 4 running" | mail -s "Stats from wmrun" mymail@mail.com
    else
      echo "equal or more than the desired 4 running" | mail -s "Stats from wmrun" mymail@mail.com
    fi
    Don't worry Ma'am. We're university students, - We know what We're doing.
    'Ruiat coelum, fiat voluntas tua.'
    Datalogi - en livsstil; Intet liv, ingen stil.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •