[Doubt] How can I store numbers less than 1? Shell variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Doubt] How can I store numbers less than 1? Shell variables
# 1  
Old 11-21-2009
[Doubt] How can I store numbers less than 1? Shell variables

Hi everyone, I am having some problems with my scripts so I hope you could help me.
I am trying to store the result of a division in a variable in tcshell but I have the problem that if:

For example, dividing 2/100 the result is 0.02 but if I store that I have "0".

How can I have 0.02 stored?

Thank you, and sorry for my English

---------- Post updated at 11:52 AM ---------- Previous update was at 09:08 AM ----------

Ok, I have to use bc.
But, I do not want to put the scale, I want it shows the number until it finds a decimal number.

Example: 2/100 = 0.2000000
I only want it shows 0.2

And the same with all the divisions, any idea? thank you
# 2  
Old 11-21-2009

Use awk.
# 3  
Old 11-21-2009
Could you give me an example of how to use awk?
# 4  
Old 11-21-2009
Quote:
Originally Posted by Bloody
Could you give me an example of how to use awk?
You cannot keep a float value in a shell variable. For calculations yeilding float values, you can use tools such as awk / bc. Howver, the float results can be stored only as a string in shell variable.
# 5  
Old 11-21-2009
Quote:
Originally Posted by dennis.jacob
You cannot keep a float value in a shell variable. For calculations yeilding float values, you can use tools such as awk / bc. Howver, the float results can be stored only as a string in shell variable.
In ksh (ksh93) you can.
# 6  
Old 11-21-2009
Quote:
Originally Posted by dennis.jacob
You cannot keep a float value in a shell variable. For calculations yeilding float values, you can use tools such as awk / bc. Howver, the float results can be stored only as a string in shell variable.
Well, I want to show the value in the shell
# 7  
Old 11-21-2009
Quote:
Originally Posted by Bloody
Could you give me an example of how to use awk?
Code:
$  echo 2 100 | awk '{print $1 / $2}'
0.02

$  X=$(echo 2 100 | awk '{print $1 / $2}')
$  echo $X
0.02

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help Needed: UNIX shell variables to store Oracle table records

Hello Folks, I'm working on a requirement to automate the process of generating report(csv file) using metadata info stored in an Oracle table and E-mail it to respective people. Meta data table: Report_ID,Report_SUB_ID,Report_DB,Report_SQL,Report_to_email_Id 1,1,DEV,'select * From... (2 Replies)
Discussion started by: venkat_reddy
2 Replies

2. Linux

How to store count of multiple queries in variables in a shell script?

how to store the count of queries in variables inside a filein shell script my output : filename ------- variable1=result from 1st query variable2=result from 2nd query . . . . (3 Replies)
Discussion started by: sanvel
3 Replies

3. Shell Programming and Scripting

Shell script to read a file and store in variables

I have a input file like this. Sample.txt 30 | TXDatacenter | TXBackupDC 10 | UKDatacenter | UKBackupDC 0 | NLDatacenter | NLBackupDC ...... ...... ...... I need to get these values in different variables like this. Load1=30 PriCenter1=TXDatacenter... (5 Replies)
Discussion started by: Visha
5 Replies

4. Shell Programming and Scripting

How to store results of multiple sql queries in shell variables in ksh?

Hi, I have a script where I make a sqlplus connection. In the script I have multiple sql queries within that sqlplus connection. I want the result of the queries to be stored in shell variables declared earlier. I dont want to use procedures. Is there anyway else. Thanks in advance.. Cheers (6 Replies)
Discussion started by: gonchusirsa
6 Replies

5. Shell Programming and Scripting

Using Fractional Numbers in Shell Variables

Hello everyone, Before I state my problem I would like to inform you that this is not an assignment. I'm just trying to learn some shell programming in my free time. I'm trying to write a parking fee calculator. But I don't seem to be able to use fractional numbers. Could you please advice... (6 Replies)
Discussion started by: iwant2learn
6 Replies

6. Shell Programming and Scripting

A way to store 2 random numbers from a for loop?

I have a for loop that cycles twice and generates 1 random number for each pass through. I would like to be able to store the two numbers to use later for arithmetics. Is there a way to do that? Right now I can only seem to use the last random number for anything. Thanks. (4 Replies)
Discussion started by: AxlVanDamme
4 Replies

7. Shell Programming and Scripting

How to store contents of a command in array of variables in shell script?

I want to store contents of command dir in array of variables For eg: dir contents are command d2 demovi~ file inven java new untitled folder d1 demovi er1 filename inven~ myfiles ubuntu desktop xmms ----------------------------------- I... (3 Replies)
Discussion started by: netresearch
3 Replies

8. Shell Programming and Scripting

Doubt about variables scope

I call my script with two parameters myscript.sh aaa bbb What is the way to access $1 and $2 values inside a function? I call the function like this myfuntion $1 $1 but inside of the function, $1 and $2 are empty. Any suggestions? thank you in advanced. (1 Reply)
Discussion started by: aristegui
1 Replies

9. Shell Programming and Scripting

Doubt??? [scope of variables]

Heres an example..... <~/abc>$ cat textfile line 1 line 2 line 3 line 4 line 5 <~/abc>$ cat try.sh #/bin/ksh for runs in 1 2 3 do A=$runs echo "Inside A : $A" done echo "Outside A : $A" <- works fine (1 Reply)
Discussion started by: qzv2jm
1 Replies

10. Shell Programming and Scripting

Extract numbers from a string and store in variables

Hi All, Is it possible in Unix shell script to extract numbers from a string containing ".", such as; 5.2.314 And store in variables so; var1 = 5 var2 = 2 var3 = 314 Thanks in advance for any help anyone can provide dave (6 Replies)
Discussion started by: davewg
6 Replies
Login or Register to Ask a Question