Problem in Running Script

Hi friends,

i have written one small script named as "size.sh".....
while running its giving

syntax error at line no 2: 'str=$' unexpected.

How to run this file ??? please help me.

file content is :

-----------------------------
#!/bin/sh
str=$( df -k | grep /usr/local)
strlen=${#str}
Sindex=$( expr $strlen - 25)
Eindex=$( expr $Sindex + 2)
Size=`echo $str | cut -c$Sindex-$Eindex`

if ["$Size" -gt 60]
then
echo "Storage size is above 60. Plz clear the cache"
fi

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

Regards,
Samuel G.

which flavour/version of unix you are using?
I am not getting the error which you mentioned , I am using AIX 5.0

Anyway modify the statment str=$( df -k | grep /usr/local) with
str=`df -k | grep /usr/local`
You may not get error

try it!!!!
Shihab

Hi shihba,

thank u

str=`df -k | grep /usr/local` is working fine.

but the next line
strlen=${#str} is giving error that "bad substitution".
please help me in this...

Regards,
Sam.

Hi Sam,
It seems you are using old version of Unix which is not supporting this - I am not sure
your can rewrite the statment strlen=${#str} with strlen=`expr length $str`. This may work.

 By the way which unix flavour you are using and the version?

Shihab

Using backticks is not recommended. Your original form was ok save for proper quotation:

str="$(df -k | grep /usr/local)"

which could better be written:

str="$( df -k /usr/local | sed '1d' )"

The second error is because of typo: "${# ...}" only works with arrays, giving back the number of elements in an array.

The whole construction with strlen is not too elegant, this one is easier and does the same:

size="$( df -k /usr/local | sed '1d;s/<spc><spc>*/<spc>/g' | cut -f3 -d'<spc>' )"

(replace "<spc>" with literal space characters)
baknin

Replace your working shell to bash or ksh.
bash or ksh is more powerful.

in sh aa=`echo $a`
in bash or ksh you can use aa=$(echo $a) also.
bash support some operations in {}
e.g:
${file#/}
${file%/
}
...
${file:0:5}

cheers.

Change line 1 from #!/bin/sh to #!/usr/bin/ksh

Thanks a lot friends...

its working fine now....

i need one more help friends....

HOW TO RUN THIS SCRIPT FROM CRON....i want this script once in a week.

please help.

Regards,
Sam.

try searching the FAQs before posting.

Actually i am very new to UNIX....
I could not get correct way to run a script using CRON.
plz help me.

Regards,
Sam.

What did you try ?

I tried using this commands...

export EDITOR=vi

crontab -e

5 * * * * sh cache.sh

then exit from vi editor with :wq.

will it work or i have to give full path of cache.sh ????
wil it work for every 5 min ????

Please, issue "man cron" first before asking these questions.

it will be issued every 5th minute of an hour. 0:05, 1:05, etc.

It won't work because there is no path to the script.

It might still no work if you provide the path because cron doesn't pass any environment to the scripts called by it and i can't see any environment set in your code. Maybe it will stop saying "can't find df" or something such.

bakunin

Thanks Bakunin.....

I have my Script file in tmp folder. I have added the line in the cron file as

5 * * * * /tmp/cache.sh

its not working....what i have to do if i run this script in every 5 min?

Regards,
Sam.

I have told you: "man cron" and read the output.

to run it every 5th minute:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /tmp/cache.sh

You could have read that there instead of asking here.

but again, this still may not work because your script doesn't have any environment settings when it is called from cron. When you call it in your shell, it inherits all the environment from it (issue "env" to see it), when you let cron call it it gets nothing and is left to what it sets by itself.

Ok, 'nuff said. I won't answer any more questions before you don't even attempt to get the answers yourself. I'm glad to help, but I'm a lousy man-page-replacement

bakunin