Using Floating Numbers in String


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using Floating Numbers in String
# 1  
Old 06-29-2009
Using Floating Numbers in String

Dear Unix Gurus,

I have a list of files that I want to loop over....for example:
Code:
 
sl40_z11.70.txt
sl41_z11.40.txt
sl42_z11.10.txt
sl43_z10.80.txt

using the script

Code:
 
#!/bin/sh
#
echo -n "....enter first Z-coordinate position....."; read zpos
q="scale=3; $zpos"
p=0.3
#
loopNumber=$[($lastslice - $firstslice)+1]
echo "loopNumber is $loopNumber"
echo firstslice no. is $firstslice
 
for ((i=$firstslice; i<=$loopNumber; i++)); do 
n=$[i]
for ((k=0; k<=$loopNumber; k++)); do 
r=$[k] ; f=$r*$p ; zposition=$q-$f 
 
echo "sl$[n]_z$[zposition].txt"
 
done
done

where firstslice is 40 and lastslice is 43 and 0.3 is the difference in z-cordinate btw the files.

My problem is that the script works fine with the exception that bash doesn't recognize $zposition. It apparently can't handle iteration with floating numbers? Can someone help?

Cheers
# 2  
Old 06-29-2009
Try this and change according to ur variables.

echo "scale=4;$b+$c" | bc
# 3  
Old 06-29-2009
Quote:
Originally Posted by jayan_jay
Try this and change according to ur variables.

echo "scale=4;$b+$c" | bc
I'm not sure I understand you. Doing
Code:
echo "$zposition" |bc

returns the correct output. But doing
Code:
echo "sl$[n]_z$[zposition].txt"

does not. This is my problem.
# 4  
Old 06-29-2009
Try this
echo "sl${n}_z${zposition}.txt"
# 5  
Old 07-01-2009
Quote:
Originally Posted by jayan_jay
Try this
echo "sl${n}_z${zposition}.txt"
doesn't work. can I just ask..did you actually try out your suggestions bufore suggesting them to the forum?
# 6  
Old 07-01-2009
Humm, I have looked at this shell script a few times and scratched my head.

You set the variable 'q' to a string
Code:
q="scale=3; $zpos"

yet you expect to be able to subtract a number from it
Code:
zposition=$q-$f

How is this meant to work?

Last edited by fpmurphy; 07-01-2009 at 09:12 PM.. Reason: fixed typo
# 7  
Old 07-06-2009
Quote:
Originally Posted by fpmurphy
Humm, I have looked at this shell script a few times and scratched my head.

You set the variable 'q' to a string
Code:
q="scale=3; $zpos"

yet you expect to be able to subtract a number from it
Code:
zposition=$q-$f

How is this meant to work?
Hi,

I revisited it too and scratched my head as well! There's a bug in the script...if firstslice is bigger than lastslice it won't work. So I've tried a different tack using awk.

as a reminder, this is what I want to do; I have a list of files:

Code:
 
sl40_z11.70.txt
sl41_z11.40.txt
sl42_z11.10.txt
sl43_z10.80.txt

I want to loop through these files keeping their labelling intact. For example, I want to do in a single script

Code:
cp sl40_z11.70.txt sl40_z11.70.dat
cp sl41_z11.40.txt sl41_z11.40.dat
cp sl42_z11.10.txt sl42_z11.10.dat
cp sl43_z10.80.txt sl43_z10.80.dat

I have hundreds of these files and I need to carry out other non-trivial operations on them. Problem is bash does not recognize floating numbers so I can't loop over them.

my new script is :

Code:
 
#!/bin/sh
#
echo -n "....enter first slice number....."; read firstslice
echo -n "....enter last slice number....."; read lastslice
#
echo -n "....enter first Z-coordinate position....."; read zpos
#sliceDiff=0.3
#
loopNumber=$[($lastslice - $firstslice)+1]
echo "loopNumber is $loopNumber"
echo firstslice no. is $firstslice
for ((k=$firstslice;k<=$lastslice; k++)); do 
r=$[k]
echo "$firstslice $r $zpos" | awk '{print $3-(($2-$1)*0.3)}'
#echo "$firstslice $r $zpos" | awk '{print $i}' sl$2_z[$3-(($2-$1)*0.3)].txt > xxxx.dat
done

So running this script and entering at prompts "40" , "43", "11.70"
loops over the 4 files nicely. But I don't know how to incorporate the output into the last line of the script so that awk recognizes the filename.

Any help would be much appreciated.

Cheers
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparison of floating point numbers in bash

I have the following code snippet in bash if ]; then minm=`echo "$diff" | bc` fi It works well for most of the cases. However lets say diff is -0.17 and minm is -0.0017. In such a case the comparison seems to fail. Is the correct way to compare a mixture of positive and... (12 Replies)
Discussion started by: ngabrani
12 Replies

2. Shell Programming and Scripting

Floating Point Numbers in c shell!

I have started using bash but this script which I am working on it, is in c chell. So here is my simple problem: set x = 0.4124\0.234 echo $x 0.4124.0.234 Same operation in Bash gives me correct result in my terminal. So there is something with my c shell that is causing this behaviour.... (8 Replies)
Discussion started by: dixits
8 Replies

3. UNIX for Dummies Questions & Answers

Add floating point numbers from file

How do I use bash to add all the floating point numbers saved in a file like this? 490.47 244.61 263.07 131.59 246.81 115.20 (3 Replies)
Discussion started by: locoroco
3 Replies

4. Programming

Testing floating point numbers

Hi guys I have problem with my simple calculator, author of my book wrote One way I tried is to test if one the inpur number is grater than zero, and then substatct And my protptype function is #include <stdio.h> int main(void) { float a, b , result; ... (11 Replies)
Discussion started by: solaris_user
11 Replies

5. Shell Programming and Scripting

How to compare floating point numbers in shell script?

How can we compare 2 floating point numbers in SHELL script? (11 Replies)
Discussion started by: dearanik
11 Replies

6. Shell Programming and Scripting

Addition of floating numbers

Hi, i want to add two decimal values to $ set a= 12.4 $ set b=3.6 $ w=`expr $a - $b` expr: non-numeric argument or is there any other method to do this mathematics operation. i need to use this into my script. (4 Replies)
Discussion started by: dear_abhi2007
4 Replies

7. Shell Programming and Scripting

How to get the ceiling value for floating numbers?

Dear guys; How can I get the ceiling value of any floating number using shell or awk scripts or functions. Example:- old values "7.2" or "7.8" or "7.5" --->>> ceiling function ---->>> new value "8". BR (6 Replies)
Discussion started by: ahmad.diab
6 Replies

8. Shell Programming and Scripting

floating point numbers in if

# if > then > echo "1" > else > echo "2" > fi -bash: How can i compare floating point numbers inside statement? (15 Replies)
Discussion started by: proactiveaditya
15 Replies

9. Shell Programming and Scripting

How to Compare Floating point / real numbers

Hai, Can you please guide me, to compare the floating point numbers. Eg. If then echo "value1 is grater " fi This code is not working properly when i excuted with floating values or real numbers (13 Replies)
Discussion started by: padarthy
13 Replies

10. Shell Programming and Scripting

problem with floating point numbers in awk

hi all, i have the following problem using awk in a script i want to read the values from a column with real numbers and calculate the mean.the problem is that when i use a statement such as this num = $4 i cant find a way to convert the variable from string to floating point to perform... (7 Replies)
Discussion started by: kanagias
7 Replies
Login or Register to Ask a Question