bc

Floating point results in Bash integer division

≯℡__Kan透↙ 提交于 2019-12-20 15:46:20
问题 I have a backup script on my server which does cron jobs of backups, and sends me a summary of files backed up, including the size of the new backup file. As part of the script, I'd like to divide the final size of the file by (1024^3) to get the file size in GB, from the file size in bytes. Since bash does not have floating point calculation, I am trying to use pipes to bc to get the result, however I'm getting stumped on basic examples. I tried to get the value of Pi to a scale, however,

BC command not working in CYGWIN

蹲街弑〆低调 提交于 2019-12-19 07:29:02
问题 I have downloaded CygWin(32-bit) and installed (on Windows 7 32 bit System ) today to run shell script but i found one problem that bc command isn't working. I am getting error -bash: bc: command not found . So please help me out! 回答1: You need to select the bc package for installation from the "Select Packages" window. By default setup installs only base category that is ~ 50 packages, while the full availability is ~ 4000. https://cygwin.com/packages/x86/bc/bc-1.06.95-2 回答2: On Windows

convert large number to spoken english

雨燕双飞 提交于 2019-12-12 06:46:31
问题 Converting 'small' numbers to English is not to troublesome. But if you handle BCMath Arbitrary Precision numbers then it can be. Using code from: http://marc.info/?l=php-general&m=99928281523866&w=2 The maximum number seems to be: two billion one hundred forty seven million four hundred eighty three thousand six hundred forty seven Anyone know a function to convert numbers bigger than that? 回答1: You have to write your own function, I suggest to use numbers as a string, let a substract like

Using bc in awk

与世无争的帅哥 提交于 2019-12-12 04:56:54
问题 I am trying to use bc in an awk script. In the code below, I am trying to convert hexadecimal number to binary and store it in a variable. #!/bin/awk -f { binary_vector = $(bc <<< "ibase=16;obase=2;FF") } Where do I go wrong? 回答1: Not saying it's a good idea but: $ awk 'BEGIN { cmd = "bc <<< \"ibase=16;obase=2;FF\"" rslt = ((cmd | getline line) > 0 ? line : -1) close(cmd) print rslt }' 11111111 Also see http://gnu.org/software/gawk/manual/gawk.html#Bitwise-Functions and http://gnu.org

Most elegant unix shell one-liner to sum list of numbers of arbitrary precision?

给你一囗甜甜゛ 提交于 2019-12-11 08:22:06
问题 As regards integer adding one-liners, several proposed shell scripting solutions exist; however, on closer look at each of the solutions chosen, there are inherent limitations: awk ones would choke at arbitrary precision and integer size (it behaves C-like, afterall) bc ones would rather be unhappy with arbitrarily long inputs: (sed 's/$/+\\/g';echo 0)|bc Understanding that there may be issues of portability on top of that across platforms (see [1] [2]) which is undesired, is there a generic

Writing a term to bc via pipes in C

北城余情 提交于 2019-12-11 04:59:30
问题 I ran into a problem with this C exercise. The task is to create two processes. The two are connected with two pipes that terminate in the stdin and stdout of the child. The child process is then replaced with bc. I am then supposed to write a term (e.g. 1 + 2) from the parent to the child process (bc). The pipes are doing what they're supposed to do, however, bc doesn't seem to like the input. When I write into the pipe, bc responds with multiple lines of the following: (standard_in) 1:

In bash how do I divide two variables and output the answer rounded upto 5 decimal digits? [duplicate]

妖精的绣舞 提交于 2019-12-10 22:17:00
问题 This question already has answers here : Round a divided number in Bash (10 answers) Closed 3 years ago . I took two variables as input and after dividing them, I want to get the output rounded up to 5 decimal digits. I have tried this way -> sum=12 n=7 output=$("scale=5;sum/n"|bc) echo $output My code isn't showing any output. What can I do?? TestCase: If sum=3345699 and n=1000000 then (sum/n)=3.345699 , it should be changed into 3.34570 . 回答1: The problem here is that you missed the echo

How to get a decimal number when dividing in bc?

删除回忆录丶 提交于 2019-12-06 16:38:52
问题 I need 'bc' to divide a number and give me not only the floor but also the remainder. For instance 'bc' gives me '2' if I do '5/2'. I'd really want something like '2.5' Maybe this isn't even possible? 回答1: scale = 20 It sets the number of decimal places. $ bc bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 355/113 3 scale=20 355/113 3.14159292035398230088 quit $ 来源: https:/

awk output into BC HexToDec calculation

点点圈 提交于 2019-12-06 15:47:44
I am trying to combine awk and bc in order to create a bash function for quickly converting hex values into a proper format. echo FFFFFF | VAR=$(awk '{print toupper($0)}') | "ibase=16; $((((($VAR - 5) / 100) - 1000)))" | bc I am trying this but I cant seem to get the output of awk into the input of the bc argument. Although you try to do it with awk and bc , I give you the most convenient option here : printf . This allows you to easily convert between decimal, octal and hexadecimal. [bash] % printf "%d" 0xFFFFFF # hex2dec 16777215 [bash] % printf "%#O" 0xFFFFFF # hex2oct 077777777 [bash] %

Bash decimal to base 62 conversion

点点圈 提交于 2019-12-05 02:41:28
I would like to reverse the operation performed by the following bash command: $ echo $((62#a39qrT)) 9207903953 i.e. convert decimal 9207903953 to base 62 , keeping bash standard of {0..9},{a..z},{A..Z} . I know I can do this by using bc , but I will have to manually convert each character then. For example, I do this currently: BASE62=($(echo {0..9} {a..z} {A..Z})) for i in $(echo "obase=62; 9207903953" | bc) do echo -n ${BASE62[$i]} #Doesn't work if bc's output contains leading zeroes done There must be a way to do this in a less 'hackier' way. Do you know of a way to do this more