Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell script: File watcher 1

Status
Not open for further replies.

arravalli12

Programmer
May 3, 2005
62
0
0
US
Hi,

I am trying to execute this script...but I am getting an error saying:
startinformsh_fw.ksh: line 38: syntax error: unexpected end of file

I am pasting the script here:

Code:
#!/bin/ksh

if [ "$#" != "3" ]
then
echo "Usage: $0 <file_name> <poll_interval_in_sec> <minimum size in bytes>"
exit 1
fi


FILE_NAME=$1
POLL_INTERVAL=$2
SIZE=$3
found=0

while {"$found" != "1"}
{
           x = 'find . -name "$FILE_NAME"'

           if ["$x" != ']
           then
           FILESIZE_PREVITER='ls -l "$FILE_NAME"|awk '{print $5}'
           sleep $POLL_INTERVAL

           FILESIZE_CURRITER='ls -l "$FILE_NAME"|awk '{print $5}'

               if ["$FILESIZE_PREVITER" eq "FILESIZE_CURRITER" && "FILESIZE_CURRITER" ge "$SIZE"]
               then
               echo 'Successful'|/usr/lib/sendmail -t shp15@test.com
               $found=1
               #echo "Hello"
               exit 0
               fi
           fi
           sleep $POLL_INTERVAL
           continue
}

Could anyone suggest where is the mistake?
Thanks
 
Try changing your [tt]while[/tt] loop to...
Code:
while [b][[/b] "$found" != "1" [b]][/b]
[b]do[/b]
    .
    . your commands go here
    .
[b]done[/b]
 
Shruti Parikh:
Well, I'm not as up to speed on my Korn shell scripting as I used to be, but I'm pretty sure that the [tt]while[/tt] loop uses square brackets, not curly brackets. Just like Bash.

There's also a set of curly brackets around the body of the [tt]while[/tt]. Instead, that should be [tt]do[/tt] and [tt]done[/tt].

To assign a value to a variable named [tt]x[/tt], I don't think you can have spaces around the equals sign in a shell script.

If you want to assign the output of the [tt]find[/tt] command to the variable [tt]x[/tt], you need to use the grave accent mark (`) rather than the single-quote ('). This applies to everywhere you want to use the output of a command rather than the literal string of the command.

Sometimes, but not always there are problems when comparing a variable to an empty string. It's safer to do something like [tt][ "x$x" != "x" ][/tt] (see below).

You can't assign a value to a variable with the dollar-sign. i.e.: [tt]found=1[/tt] instead of [tt]$found=1[/tt]

I don't think the [tt]continue[/tt] is actually necessary if it's the last line of the block anyway. I don't think it hurts anything either.
Code:
if [ "$#" != "3" ]
then
    echo "Usage: $0 <file_name> <poll_interval_in_sec> <minimum size in bytes>"
    exit 1
fi

FILE_NAME=$1
POLL_INTERVAL=$2
SIZE=$3
found=0

while [ "$found" != "1" ]
do
    x=`find . -name "$FILE_NAME"`
    if [ "x$x" != "x" ]
    then
        FILESIZE_PREVITER=`ls -l "$FILE_NAME"|awk '{print $5}'`
        sleep $POLL_INTERVAL

        FILESIZE_CURRITER=`ls -l "$FILE_NAME"|awk '{print $5}'`

        if ["$FILESIZE_PREVITER" eq "FILESIZE_CURRITER" && "FILESIZE_CURRITER" ge "$SIZE"]
        then
            echo 'Successful' | /usr/lib/sendmail -t shp15@pitt.edu
            found=1
            exit 0
        fi
    fi
    sleep $POLL_INTERVAL
    continue
done



-- Ghodmode
 
Instead of
Code:
while { "$found" != "1" }
{
#code
}

use
Code:
while [ $found ] 
do
#code
done

The "while [ $found ]" line works because $found evaluates to zero when it's not one, and zero is interpreted as "true". Any non-zero value is false.

Rod Knowlton

IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

 
RodKnowIton,
Can you please look at this code:

[bansah01@:/h/bansalh01/temp]more test.sh
#!/bin/ksh
a=1
while [ "$a" ]
do
echo "-$a- is the value"
read a
done

Execution :
------------

[bansah01@:/h/bansalh01/temp]test.sh
-1- is the value
2
-2- is the value
3
-3- is the value
4
-4- is the value
0
-0- is the value
0
-0- is the value

This is not exiting either for 0 or for non-zero. Can you tell me where I made the mistake?
 
Try...
Code:
#!/bin/ksh
a=1
while [b](( a ))[/b]
do
   echo "-$a- is the value"
   read a
done
It's better for boolean numeric tests.
 
Hi

Just abit explanation to what was wrong in your code. The bracket ( [tt][[/tt] ) command is similar to the [tt]test[/tt] command, and for both the default operation if none specified, is not empty string check :
man test said:
[-n] STRING
the length of STRING is nonzero
So giving just the "$a" string as parameter, which evaluates to "0", the result was 0, meaning ok, the string is not empty, and this is why not exited the [tt]while[/tt].

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top