Operating System - HP-UX
1753648 Members
5991 Online
108798 Solutions
New Discussion юеВ

Re: Newbie sh-posix question about While loop

 
SOLVED
Go to solution
Jason Martens
Frequent Advisor

Newbie sh-posix question about While loop

I have the following bit of code, but it generates an error every time I run it:


while [[ $I -lt $CNT ]];
do
if [[ `grep ${VOLUMES[$I]} *postBackup | grep "has been successfully removed" | wc -l` -eq 1 ]]
let SucessfullyMerged = $SucessfullyMerged + 1
echo ${VOLUMES[$I]}
let I=$I+1;
done


It generates this error message:
./BackupStatus[16]: Syntax error at line 23 : `done' is not expected.

In my script, line 23 is the line immediately after the done. I'm kind of new to this, can someone tell me what I'm doing wrong?

Thanks,
Jason
Never swap out a tape drive at 3 AM!!!
6 REPLIES 6
MarkSyder
Honored Contributor
Solution

Re: Newbie sh-posix question about While loop

You have an "if" inside your do - done loop.

You need to end the if with a "fi" before the done.

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Joseph C. Denman
Honored Contributor

Re: Newbie sh-posix question about While loop

problems with the "if" area.

if
then
else
fi


Hope this helps.

...jcd...
If I had only read the instructions first??
A. Clay Stephenson
Acclaimed Contributor

Re: Newbie sh-posix question about While loop

Your if statement does not conform to this:
if [[ some_condition ]]
then
command1
command2
fi

Lose the semicolon in let I=$I + 1, if fact better syntax is is simply

((I += 1))

Learn to enclose ALL variables in {}.
Start using the typeset command to type variables so that instead of
I=0
do
typeset -i10 I=0
If it ain't broke, I can fix that.
Jason Martens
Frequent Advisor

Re: Newbie sh-posix question about While loop

Thanks all! That was it. Clay, can you explain this a bit more? Why is it better to use the typeset syntax? And why enclose variables in {}?

Learn to enclose ALL variables in {}.
Start using the typeset command to type variables so that instead of
I=0
do
typeset -i10 I=0
Never swap out a tape drive at 3 AM!!!
A. Clay Stephenson
Acclaimed Contributor

Re: Newbie sh-posix question about While loop

One word: discipline. If you "zig" each and everytime, you no longer have to decide to "zig" or "zag". In all cases, the {}'s will work; in SOME cases they are required in order for the shell to correctly parse a statement. Do it every time and you can't go wrong.

The typeset is the preferred method nowadays. It gives the shell intepreter hints about how a given variable should be treated and what are legal operations to perform on it. Typesetting also avoids ambiguities and the radix (base) of a given integer variable which in turn avoids problems. For example, consider:

I=08

Now that might be an error if your intent was to create an octal number (or if you grabbed 08 out of a cal command) BUT if
you first did:

typeset -i10 I
then
I=08
or
I=8
would be correctly interpreted in either case.

Since you indicated that you were a newbie, learn the disciplined approach early and you won't need to unlearn less than well-disciplined habits.

If it ain't broke, I can fix that.
Dan Martin_1
Frequent Advisor

Re: Newbie sh-posix question about While loop

Jason,

I would also clean up your grep statement. Either:

if grep "${VOLUMES[$I]}.*has been successfully removed" *postBackup

or

cnt=$(grep -c "${VOLUMES[$I]}.*has been successfully removed" *postBackup)
if ((cnt == 1))

. . . depending on whether you need to check for any occurence of the line (first example) or just one occurence of the line (second example).

Notice the $() - this is the POSIX shell's command substitution syntax and is more robust than the back ticks.

Also, you do not need to use the "$" when using let or (( )). ((i = i + 1)) or ((i += 1)) or let "i += 1"

Dan