LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-14-2013, 12:19 PM   #1
linson_85
Member
 
Registered: Oct 2008
Location: Latitude= 9.9722, Longitude= 76.378
Distribution: Open VZ,fedora & Centos
Posts: 47

Rep: Reputation: 0
Repeat if loop in bash script


Hello,

I'm trying to setup a bash script for creating KVM guests. What I'm trying to setup is, i need to verify whether VM name exists on the host already first. If with no such name, then create the VM. To make it clear I have following VMs created on my host

Output of virt list -all

Id Name State
----------------------------------------------------
3 ffDeb64 running
5 Deb-6-32 running
6 Clust-VM01 running
7 Clust-VM02 running

----------------------------------------------------

So I'm trying to setup a script that will ask to enter the desired name first. If such name exists, then it should prompt again to enter the name. If such name doesn't exists, the script will create the VM. The intention is to use unique VM name during creation


Script details is as follows

]# cat createvm.sh
#!/bin/bash
echo -n " Enter the name : "; read VMname #line1
if [[ "$(cat virt-list.sh |awk '{print $2}'|grep $VMname)" == "$VMname" ]] #line2
then #line3
echo "The name $VMname exists" #line4
else #line5
echo "Proceed with further ..." #line6
fi #line7




Content of file virt-list.sh is as follows

]# cat virt-list.sh
Id Name State
----------------------------------------------------
3 ffDeb64 running
5 Deb-6-32 running
6 Clust-VM01 running
7 Clust-VM02 running


So, I want the script go to line1 when it execute the line4

Which means if VM name exists, I want the script to prompt to enter the VM name again ( which suppose to be unique name )

Whats the best and easier way to achieve this ?
Any help ?

Last edited by linson_85; 11-14-2013 at 12:38 PM.
 
Old 11-14-2013, 12:47 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Have a look at this:
Code:
#!/bin/bash

while true
do
  read -r -p "Enter the name : " VMname

  if ! $(virsh list --all | grep -q "\b$VMname\b")
  then
    break
  else
    echo "Oops, name exists."
  fi
done

echo "we can continue with the script"

Last edited by druuna; 11-14-2013 at 12:50 PM. Reason: Grep needs word boundries
 
1 members found this post helpful.
Old 11-14-2013, 09:19 PM   #3
linson_85
Member
 
Registered: Oct 2008
Location: Latitude= 9.9722, Longitude= 76.378
Distribution: Open VZ,fedora & Centos
Posts: 47

Original Poster
Rep: Reputation: 0
Thanks druuna.

Thats what I needed. Much appreciated.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 03:14 AM
[SOLVED] BASH script to record webcam & save in defined directory, stop recording then repeat? elliotjreed Linux - General 1 07-10-2013 10:06 AM
[SOLVED] Bash Script for loop sunveer Programming 8 09-10-2012 12:42 PM
AAXINE How to Loop or Repeat timomeinen Linux - Software 2 02-04-2005 06:52 AM


All times are GMT -5. The time now is 08:22 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration