Simple test driving me mad!

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple test driving me mad!
# 1  
Question 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)!!!Smilie

Here is the code:


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))
elif [ $success -gt 0 ] && [ $tarVal -le $newTarVal ]; then
success=$((success + 1))


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!

# 2  
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.
# 3  
Thanks Perderabo - of course you're absolutely right, my version of Korn is not 93, so it doesn't support floats - grrr.Smilie

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!


# 4  
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

Last edited by TioTony; 10-25-2003 at 01:27 AM..
# 5  
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,

# 6  
Oops. don't bother. Gone mad!
# 7  
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! Smilie
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #13
Difficulty: Easy
EBCDIC, ASCII, SIXBIT, and ANSI are methods for encoding text characters on a computer.
True or False?

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wrong test interpretation for simple function.

Hello everyone, I have written simple script below to check if ip is added to interface #!/usr/local/bin/bash IFCONFIG="/sbin/ifconfig" SERVICE="/usr/sbin/service" IP="79.137.X.X" GREP=$(${IFCONFIG} | grep ${IP}) ip_quantity_check () { echo ${GREP} | wc -l } if ];... (2 Replies)
Discussion started by: bryn1u
2 Replies

2. Shell Programming and Scripting

Simple script to test mountGpoint for read-write

Guys, need your help urgently. ServerA ServerB ServerA has an email facility and can connect remotely via root using key to ServerB What I am trying to achieve here is ServerA would be able to remotely: 1. Do command 'touch /mnt/' on ServerB 2. If touch failed with... (4 Replies)
Discussion started by: jaapar
4 Replies

3. UNIX for Dummies Questions & Answers

Simple negated test condition

Without grep, I'd like to make a test condition so that any a word that does not have the successive letters car in it will be echoed. for example, bluecar will contain "car" so it will show up as a no var=bluecar $ echo $var|if ]; then echo "yes";fi yes this variable contains "car" so I... (5 Replies)
Discussion started by: newbie2010
5 Replies

4. Windows & DOS: Issues & Discussions

Getting mad at different CMD / Batch behaviour

I have these commands: MOVE "C:\Users\Pascal\Dropbox\Public\U_PC_Backup\Anki Flashcards\Cameo_App_Virtualization\0backup\Cards_only\Card_Backup\" "C:\Users\Pascal\Dropbox\Public\U_PC_Backup\Anki Flashcards\Cameo_App_Virtualization\0backup\Cards_only\Card_Backup_%date%\" xxcopy /E /Y... (2 Replies)
Discussion started by: pasc
2 Replies

5. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

6. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

7. UNIX for Dummies Questions & Answers

Using gzip and my speakers have gone mad!

Hello, I wanted to compress a directory which I did with: tar -czvf backup15062007.tar.gz backup15062007/ I knew that you could specify the compression level with gzip so I then did: gzip -cvf9 backup15062007.tar.gz backup15062007compress.tar.gz Now I know this probably won't... (5 Replies)
Discussion started by: patwa
5 Replies

8. HP-UX

HP-UX boxes - am I mad?

Greetings, and thanks for having such an interesting forum! I currently have 3 Intel boxes running 2000, and two HP boxes running HP-UX 10.20. I am very inexperienced with the Unix/Linux world. I have several problems, mainly in getting them all networked together. I am constrained to keep a... (3 Replies)
Discussion started by: Menace
3 Replies

Featured Tech Videos