How avoid using "BC" library?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How avoid using "BC" library?
# 1  
Old 08-11-2015
How avoid using "BC" library?

Hi all,

I am doing something like below:

Code:
MaxTime=$(bc <<< "scale = 3; ($MaxTime / 10)")

where MaxTime=127. I am getting answer as 12.700

Now I don't want to use bc. I want to do same thing without bc.

I am novice to shell scripting could anyone please tell me how to achieve same thing without using bc?

thanks
--girija

Last edited by Don Cragun; 01-13-2016 at 04:06 AM.. Reason: Change B and U tags to CODE and ICODE tags.
# 2  
Old 08-11-2015
Hello girijajoshi,

Welcome to forums. Please use code tags for commands/codes/inputs you are using in your posts as per forum rules. You can try following on same.
Code:
 awk -vMAXTime=127 'BEGIN{printf "%.3f\n",MAXTime/10}'

Output will be as follows.
Code:
 12.700

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 08-11-2015
In your example, the original value assigned to MaxTime is an integer. Will that always be the case, or can the original value be a floating point value?

What shell are you using? Can you use a 1993 or later version of the Korn shell? (It can perform floating point operations using shell arithmetic expansions.)

Do you just need to perform division by 10, or do you need to handle arbitrary arithmetic operations?
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 08-11-2015
Hi Ravinder,

Thanks for the reply.

Code:
awk -vMAXTime=127 'BEGIN{printf "%.3f\n",MAXTime/10}'

the above code gives me the op. could you please tell me how to store the return value in the variable. So that I can use that value for further calculations.

thanks
--girija

Last edited by Don Cragun; 08-11-2015 at 03:29 AM.. Reason: Add CODE tags again.
# 5  
Old 08-11-2015
Do it just like you did it in your first post in this thread:
Code:
MAXTime=$(awk -vMAXTime=127 'BEGIN{printf "%.3f\n",MAXTime/10}')

If you answer my earlier questions, there may be MUCH more efficient ways to do this.
# 6  
Old 08-11-2015
Hello girijajoshi,

Please use code tags for commands/codes which you are using in your posts as per forum rules, here is the link for same too.
https://www.unix.com/misc.php?do=cfrules

You can store it in a variable as follows.
Code:
 VAR=`awk -vMAXTime=127 'BEGIN{printf "%.3f\n",MAXTime/10}'`

If you want to see variable's value do a echo $VAR.
Hope this helps.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 08-11-2015
Hi all,

Thanks for the response. Ravinder's thing helped me a lot.

thanks
--girija
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

How to avoid rsync "The authenticity of host" prompt?

Hi, I wish to bypass the rsync prompt thus making the command non-interactive. Below is where it asks for prompt: rsync --delay-updates -F --compress --archive --rsh=/usr/share/bin/ssh /web/admin/Transfer/ user1@destserver.com:/tmp/test I tried the below two solution but... (2 Replies)
Discussion started by: mohtashims
2 Replies

3. Shell Programming and Scripting

How to avoid the "temp files" in my script?

Hi ! :) I noticed that I create often of temporary files to keep character strings or other and I wish to avoid that if possible ? e.g : #!/bin/bash CONFIG_FILE="conf.cfg" TEMP_HOSTNAME="temp_file1.txt" for IP in `egrep -o '({1,3}\.){3}{1,3}' $CONFIG_FILE` do ssh "$IP"... (2 Replies)
Discussion started by: Arnaudh78
2 Replies

4. Shell Programming and Scripting

How to avoid "Too many arguments" error, when passing a long String literal as input to a command?

Hi, I am using awk here. Inside an awk script, I have a variable which contains a very long XML data in string format (500kb). I want to pass this data (as argument) to curl command using system function. But getting Too many arguments error due to length of string data(payloadBlock). I... (4 Replies)
Discussion started by: cool.aquarian
4 Replies

5. Shell Programming and Scripting

How to avoid "override protection 644 (yes/no)?" -ksh 88

Hi All, I'm using Ksh 88 version. I'm trying to remove the files using the below script .The code is working fine but i'm getting override protection 644 (yes/no)? message for every file .. Pelase suggest #!/usr/bin/ksh set -x File_Path="/etc/home/logs" Dest_Path="/etc/home/temp"... (1 Reply)
Discussion started by: smile689
1 Replies

6. Solaris

How to avoid "cannot execute" issue with a runnable jar file?

I am trying to understand why the runnable jar file runs on one Unix server, but not the other with same environment settings. I copied exact same test jar from here --> http://www.javaworld.com/javaworld/javatips/javatip127/MakeJarRunnable.zip on to both Unix servers. Then changed... (5 Replies)
Discussion started by: kchinnam
5 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

Joining 3 AWK scripts to avoid use "temp" files

Hi everyone, Looking for a suggestion to improve the below script in which I´ve been working. The thing is I have 3 separated AWK scripts that I need to apply over the inputfile, and for scripts (2) and (3) I have to use a "temp" file as their inputfile (inputfile_temp and inputfile_temp1... (2 Replies)
Discussion started by: cgkmal
2 Replies

9. Shell Programming and Scripting

Avoid "++ requires lvalue" Error in Loop Calculation

Hi All, Please help me to perform sum of values in a loop, I am getting following error: "total=0++432907765772: ++ requires lvalue" where actual statement is as : total=$total+$amt where amt can have +ve or -ve values Thanks Sandeepb (3 Replies)
Discussion started by: sandeepb
3 Replies
Login or Register to Ask a Question