while loop issues


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting while loop issues
# 1  
Old 12-16-2007
while loop issues

Hi guys,

Been sifting through these forums for awhile, but never had an account or needed to post. Shoutouts to a great forum with heaps of useful info.

Now i consider myself a noob when it comes to linux and to bash scripting. I recently started to learn to use Vmware ESX server which uses redhat. Now I wanted to learn how to bash script so i set myself a goal, to write a bash script that would backup some files on the ESX server and then ftp them.

I got a working script going, not very pretty but it worked. I recently made some changes to it and now it isnt doing exactly what i was hoping. Basically I put alot of IF commands in to check if things were on the right track and if they werent they set a variable to 1. The whole script runs under a while loop that checks to see if that variable is 1. If so it stops.

For some reason, now even when i set variable to 1, the loop keeps running till it gets to the end of all the commands. then it stops. So my point is have i done something wrong, or am i misunderstanding how while loops work. Shouldn't it stop the momment its condition is met?

Hoping someone can point me in the right direction.

Code:
#!/bin/bash

function getvmstate {
vmware-cmd "$1" getstate|grep on|wc -l
}

function basename()
{
local name="${1##*/}"
echo "${name%$2}"
}

function dirname()
{
local dir="${1%${1##*/}}"
[ "${dir:=./}" != "/" ] && dir="${dir%?}"
echo "$dir"
}

function namename()
{
local name=${1##*/}
local name0="${name%.*}"
echo "${name0:-$name}"
}

function ext()
{
local name=${1##*/}
local name0="${name%.*}"
local ext=${name0:+${name#$name0}}
echo "${ext:-.}"
}

function vmclonerun {
perl /usr/bin/vmclone $1 $2
}

COUNT=0
#storage1="storage1"
storage1="4721f5a3-2dd43ff0-636d-000e0ce9b566"
storage2="4721f5bc-5cb4f268-06bb-000e0ce9b566"
VM="/vmfs/volumes/4721f5bc-5cb4f268-06bb-000e0ce9b566/winxpsp2-test-01/winxpsp2-test-02.vmx"
VMCDIR=`dirname $VM`
VMCNAME=`namename $VM`
VMCBKPNAME="$VMCNAME-bkp"
VMCBKPDIR="$VMCDIR-bkp"
FTPPATH="myftpserver"
FTPUSER="myusername"
FTPPASS="mypassword"
OLDNAME="$VMCBKPNAME-old"
DELNAME="$OLDNAME-del"
skip=0
preoff=0

# for vm in `vmware-cmd -l`
# do
skip="0"
while [ $skip != 1 ]
do

if [ `id -u` != "0" ]
then
echo "Sorry, you are not root. Script exiting."
exit 1
fi

if [ -s $VM ]
then
echo "VMX Exists"
if [ -r $VM ]
then
echo "VMX Readable"
else
echo "VMX NOT Readable"
echo "Skipping VM"
skip=1
fi
else
echo "VMX corrupted or not there"
echo "Skipping VM"
skip=1
fi

check=`dirname "$VM"|grep $storage1|wc -l`
#echo "Check: $check"
#echo "VM: $vm"
if [ $check -eq 0 ]
then

if [ `getvmstate $VM` -eq 1 ]
then
vmware-cmd $VM stop trysoft
else
echo "VM OFF!"
echo "Proceeding with clone..."
preoff="1"
fi

while [ `getvmstate $VM` -eq 1 ]
do
# echo "Count $COUNT"
echo "Waiting for VM to shutdown...."
sleep 10
let COUNT=COUNT+1

if [ $COUNT -eq 12 ]
then
echo "VM Shutdown taking too long. Skipping VM."
skip="1"
fi

done

if [ `getvmstate $VM` -eq 0 ]
then

if [ -d "$VMCBKPDIR" ]
then
echo "Deleting old clone..."
rm -rf $VMCBKPDIR
fi

if [ -d "$VMCBKPDIR" ]
then
echo "Clone stil exists. Problem deleting. Skipping VM."
skip="1"
else
cd /
cd $VMCDIR
cd ../
echo "Cloning..."
vmclonerun $VMCNAME $VMCBKPNAME
cd /
echo "Clone complete."

if [ $preoff = 1 ]
then
echo "VM Left Off"
else
echo "Starting VM"
vmware-cmd $VM start
fi

echo "FTP Transfer started"


/usr/bin/ftp -n << !
open $FTPSERVER
user $FTPUSER $FTPPASS
prompt off
binary
lcd /
lcd $VMCBKPDIR
cd $FTPPATH
rename $OLDNAME $DELNAME
rename $VMCBKPNAME $OLDNAME
mkdir $VMCBKPNAME
cd $VMCBKPNAME
mput *
quit
!

echo "FTP Transfer complete"


echo "Removing local clone"
rm -rf $VMCBKPDIR
skip="1"
fi



else
echo "Unknown Error - VM state is not off"
skip="1"
fi

else
echo "In Storage 1 DISREGARDED"
skip="1"
fi

done
#done

# 2  
Old 12-17-2007
Quote:
Originally Posted by ryath
For some reason, now even when i set variable to 1, the loop keeps running till it gets to the end of all the commands. then it stops. So my point is have i done something wrong, or am i misunderstanding how while loops work. Shouldn't it stop the momment its condition is met?
The while loop only does the evaluation at the top of the loop, so if you go:
Code:
while thing
do
  blah
  stuff
  make thing not true
  some other stuff
done

It will do everything in the while loop once (inlcuding 'some other stuff') then get to the top and find 'thing' to be not true anymore.
# 3  
Old 12-17-2007
Ah ok cheers for the response. So it was just my poor understanding of a while loop.

Ok, well one question then during the whole while loop can i run a command to prematurely end the loop?

or do i have to re-structure the script?
# 4  
Old 12-18-2007
Quote:
Originally Posted by ryath
Ok, well one question then during the whole while loop can i run a command to prematurely end the loop?
'break' will do that. I don't use it myself as I dislike breaking out of loops but that's just me Smilie
# 5  
Old 12-18-2007
Quote:
Originally Posted by Smiling Dragon
'break' will do that. I don't use it myself as I dislike breaking out of loops but that's just me Smilie
I'll stand up for using "break", very useful when encountering error conditions and you need to abort what you are doing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop or while loop from a text file

Hi all, i developed a script to measure the uptime of a process in a Solaris 10/11 environments. All is well, but i came across a situation where there are multiple processes of the same name. Basically i have the following result file: beVWARS 13357 19592122 beVWARS 14329 19591910... (4 Replies)
Discussion started by: nms
4 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

4. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

5. HP-UX

HPUX KSH for loop issues.

Wrote a KSH script on a RHEL server and moved it over to the HPUX server and it is not liking a loop that I put together. Here is the simple form of it that does not work in KSH: for x in {0..$VARIABLE_NAME} do echo $x done This works: for x in {0..10} do echo $x ... (3 Replies)
Discussion started by: jake0391
3 Replies

6. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

7. Shell Programming and Scripting

Null Handling in Until loop. . .loop won't stop

Hi Im running this script, which is supposed to find the max value build some tables and then stop running once all the tables are built. Thing is , it keeps assigning a null value to $h and then $g is null so it keep building tables i.e. testupdateNUL. How can I stop this? Here is what I have: ... (4 Replies)
Discussion started by: brandono66
4 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

issues with a condition in a while loop

Hi, I am facing issues with test condition. I had a compound condition to write for both if and while, In one of the texts i referred, with a korn shell we can write compound statements like: ], however this doesn't worked for me. For example: if ] doesn't works, but if || worked. ... (1 Reply)
Discussion started by: amritps
1 Replies

10. Shell Programming and Scripting

how to get the similar function in while loop or for loop

Dear all How to write the shell script for the following statement: (C programming) for (i=0;i<30;i++) { if i=1 continue *skip this number (To do function here....) ... } similar statement in while loop.... I wrote the script in sh... (3 Replies)
Discussion started by: trynew
3 Replies
Login or Register to Ask a Question