Simple test driving me mad!

Hi all,

I have been writing a script to automate some work for myself and have come accross a problem. I cannot understand why it doesn't work, but then I am new to both Unix and Korn shell hacking! Here is the problem:

I want to interogate a file for a number and store that number in $tarVal. tarVal is then compared to another number and the appropriate action taken...

What should happen is this. When $success is zero and $tarVal is greater than 0.3 $n should be incremented by 3 and it should then leave the block - agreed? However, for some reason it doesn't! When tarVal has come in at 0.44 (for example) according to the program 0.44 is less than 0.3, so $success has 1 added to it and the newTarVal is set to tarVal (0.44)!!!:confused:

Here is the code:

<snip>

tarVal=$(grep 20000 ${truncfile}-${n}-${j}-output.txt | awk {'print $10'})

if [ $success -eq 0 ] && [ $tarVal -gt 0.3 ]; then
n=$((n + 3))
elif [ $success -eq 0 ] && [ $tarVal -le 0.3 ]; then
success=$((success + 1))
newTarVal=$tarVal
elif [ $success -gt 0 ] && [ $tarVal -le $newTarVal ]; then
success=$((success + 1))
newTarVal=$tarVal
else
success=$success
newTarVal=$newTarVal
fi

<snip>

Can ANYBODY enlighten me? I don't know whether I have got syntax wrong (although the script runs without error) or whether the problem lies in the use of the logical AND (&&) in some way? Please, please help as this has been foxing me for hours and hours now and I've tried everything I can think of.

Thanks in advance for any help offered!

alarmcall

Most people are stuck with an older version of ksh supplied by the manufacturer. Unless you downloaded the most recent version, you will have a older version too. The old ksh supports integer arithmetic only. You are attempting to use floating point.

Type "alias float". If you have a builtin alias called float similiar to the integer builtin alias, then you have floating point in your ksh.

Thanks Perderabo - of course you're absolutely right, my version of Korn is not 93, so it doesn't support floats - grrr.:mad:

I'm going to try a workaround, since after reading through the installation instructions for Korn93 I've decided I've not got anywhere near enough knowledge to do so - I don't even understand some of the terminology!!

If you know of a newbie friendly set of instructions for doing the install on an UNNETWORKED ULTRA 60, please let me know!

Cheers,

alarmcall

Look at the 'bc' command. There are several examples on various websites of how it works plus the man pages. It's not the best, but it may be an alternative for you. Here's a basic example:

fraction=`echo "5 / 3" | bc -l`

$fraction will then equal 1.66666666666

Hey TT! Thanks for the pointer on the use of bc - Unix to me keeps on getting deeper and deeper! I didn't realise there were so many tools and utilities.

I tried using bc, but I just couldn't work out how to use it in a if-elif-fi test (and I'm really up against the clock on this one), so I whimped out in the end and converted my decimals to integers and worked with those instead.

You would think that Sun would have included ksh93 with Solaris 9 wouldn't you?...

Thanks again,

alarmcall.

Oops. don't bother. Gone mad!

Hi mbb,

Many ways to skin a cat...what I actually did was take the decimalized number, say 0.345, strip the "0." off the front using sed and hey presto there's my integer!

Cheers! :wink: