#] #] ********************* #] "$d_SysMaint"'Linux/arithmetic notes.txt' www.BillHowell.ca 20Jul2018 initial # view in text editor, using constant-width font (eg courier), tabWidth = 3 #48************************************************48 #24************************24 # Table of Contents, generate with : # $ grep "^#]" "$d_SysMaint"'Linux/arithmetic notes.txt' | sed "s/^#\]/ /" # ********************* "$d_SysMaint"'Linux/arithmetic notes.txt' 05Mar2023 search 'Linux bash how do I add a number to a variable?' (bash no good!) 12Jul2022 numbers & strings $ if [ 100 -le 200 ]; then echo "pthOptrNam"; else echo "Bye Aqsa"; fi pthOptrNam $ if [ 100 -ge 200 ]; then echo "pthOptrNam"; else echo "Bye Aqsa"; fi Bye Aqsa $ if [ "100" -ge 200 ]; then echo "pthOptrNam"; else echo "Bye Aqsa"; fi Bye Aqsa $ if [ "100" -le 200 ]; then echo "pthOptrNam"; else echo "Bye Aqsa"; fi pthOptrNam 18Mar2022 arithmentic in video script 23Oct2019 search "Linux bash and how do I use variables in let arithmetic?" 23Oct2019 reLineNum() 13Apr2019 see "/media/bill/SWAPPER/bin/pdftk copy, insert [ISBN, copyright] CRWN, EU, IEEE, USgov papers.sh" 20July2018 bash arithmetic 05Aug2018 bash "/media/bill/SWAPPER/bin/dir_size sum, net.sh" #24************************24 # Setup, ToDos, 12Oct2023 Now in strings.h https://stackoverflow.com/questions/18093871/how-can-i-do-division-with-variables-in-a-linux-shell calc(){ awk "BEGIN { print "$*" }"; } answered Nov 21, 2014 at 15:31 zielonys num=$(( $num + $metab )) double parenthesis are better? $ (( myvar = 6 + 6 )); echo $myvar $ echo $(( 7 + 7 )) $ echo $(( myvar = 7 + $(( vartwo = 4 + 4 )) )) $ let "varone = 1" "vartwo = varone++"; echo $varone, $vartwo $ let "varone = 1" "vartwo = ++varone"; echo $varone, $vartwo https://linuxhint.com/write-bash-if-else-statements-one-line/ $ if [ 100 -le 200 ]; then echo "pthOptrNam"; else echo "Bye Aqsa"; fi pthOptrNam $ if [ 100 -ge 200 ]; then echo "pthOptrNam"; else echo "Bye Aqsa"; fi Bye Aqsa $ if [ "100" -ge 200 ]; then echo "pthOptrNam"; else echo "Bye Aqsa"; fi Bye Aqsa $ if [ "100" -le 200 ]; then echo "pthOptrNam"; else echo "Bye Aqsa"; fi pthOptrNam #24************************24 #08********08 #] ??Oct2023 #08********08 #] ??Oct2023 #08********08 #] ??Oct2023 #08********08 #] ??Oct2023 #08********08 #] 12Oct2023 Linux division strings.h tPthP_get_tDeltaDays() { pinn1="$1" pinn2="$2" t1=$( pinn_timeModTo_timeUnix "$pinn1" ) t2=$( pinn_timeModTo_timeUnix "$pinn2" ) tDelta=$(( $t2 - $t1 )) # echo "Unix msecs: t1 = $t1; t2 = $t2; tDelta (ms) = $tDelta" calc $tDelta/86400 } +-----+ https://stackoverflow.com/questions/18093871/how-can-i-do-division-with-variables-in-a-linux-shell How can I do division with variables in a Linux shell? Asked 10 years, 2 months ago Modified 2 years, 11 months ago Viewed 416k times When I run commands in my shell as below, it returns an expr: non-integer argument error. Can someone please explain this to me? $ x=20 $ y=5 $ expr x / y expr: non-integer argument edited Aug 7, 2013 at 3:10 Todd A. Jacobs asked Aug 7, 2013 at 2:56 Judking +--+ Those variables are shell variables. To expand them as parameters to another program (ie expr), you need to use the $ prefix: expr $x / $y The reason it complained is because it thought you were trying to operate on alphabetic characters (ie non-integer) If you are using the Bash shell, you can achieve the same result using expression syntax: echo $((x / y)) Or: z=$((x / y)) echo $z edited Aug 7, 2013 at 3:04 answered Aug 7, 2013 at 2:59 paddy +--+ I believe it was already mentioned in other threads: calc(){ awk "BEGIN { print "$*" }"; } then you can simply type : calc 7.5/3.2 2.34375 In your case it will be: x=20; y=3; calc $x/$y or if you prefer, add this as a separate script and make it available in $PATH so you will always have it in your local shell: #!/bin/bash calc(){ awk "BEGIN { print $* }"; } edited Jun 16, 2017 at 20:36 ktbiz 58644 silver badges1313 bronze badges answered Nov 21, 2014 at 15:31 zielonys https://stackoverflow.com/questions/18093871/how-can-i-do-division-with-variables-in-a-linux-shell calc(){ awk "BEGIN { print "$*" }"; } answered Nov 21, 2014 at 15:31 zielonys #08********08 #] 05Mar2023 search 'Linux bash how do I add a number to a variable?' (bash no good!) +-----+ https://stackoverflow.com/questions/6348902/how-can-i-add-numbers-in-a-bash-script How can I add numbers in a Bash script? Asked 11 years, 8 months ago Modified 16 days ago Viewed 1.2m times +--+ Use the $(( )) arithmetic expansion. num=$(( $num + $metab )) See Chapter 13. Arithmetic Expansion for more information. edited Jan 28, 2020 at 17:54 Peter Mortensen answered Jun 14, 2011 at 19:28 Steve Prentice 3 Strange. It does not work here when I try to compute 191.003 + 190. – Sigur Apr 7, 2017 at 23:02 2 @Sigur I believe this is because bash doesn't natively support floating point maths .i.e this method, nice as it is, only works with integers. Try using one of the dc or bc solutions instead. – Hashim Aziz May 8, 2021 at 0:56 1 Is mandatory use $ between the (())? Seems not – Manel Jordan Dec 20, 2021 at 21:33 >> why are the symbols not in quotes? 08********08 #] 12Jul2022 numbers & strings https://linuxhint.com/write-bash-if-else-statements-one-line/ $ if [ 100 -le 200 ]; then echo "pthOptrNam"; else echo "Bye Aqsa"; fi pthOptrNam $ if [ 100 -ge 200 ]; then echo "pthOptrNam"; else echo "Bye Aqsa"; fi Bye Aqsa $ if [ "100" -ge 200 ]; then echo "pthOptrNam"; else echo "Bye Aqsa"; fi Bye Aqsa $ if [ "100" -le 200 ]; then echo "pthOptrNam"; else echo "Bye Aqsa"; fi pthOptrNam 08********08 #] 18Mar2022 arithmentic in video script recrd_start() { t_vidSeconds="$1" xPosn="$2" yPosn="$3" width="$4" hight="$5" p_out="$6" # see "$d_bin"'video production/0_video capture - dimensions.ods' # [posn, lenth] in cm, # Benq monitor, fullscreen : # x_width y_height # pixels 1920 1080 # (cm) 60.0 34.5 # pixels/cm 32.0 31.3 xPix_per_cm="32.0" yPix_per_cm="31.3" # https://linuxhandbook.com/bash-arithmetic-operators/ # https://unix.stackexchange.com/questions/89712/how-to-convert-floating-point-number-to-integer # xPosn=$( echo " 5.0 * 30 " | bc -l ) xPosn=$( echo " $xPosn * $xPix_per_cm " | bc -l | sed 's|\..*||' ) yPosn=$( echo " $yPosn * $yPix_per_cm " | bc -l | sed 's|\..*||' ) width=$( echo " $width * $xPix_per_cm " | bc -l | sed 's|\..*||' ) hight=$( echo " $hight * $yPix_per_cm " | bc -l | sed 's|\..*||' ) echo "[x,y]Posn (pixels) = $xPosn, $yPosn" echo "[x,y]Leng (pixels) = $width, $hight" echo '' # echo "d_work : $d_work" echo "recrd_start : $date_ddmmmyyyy_hm" echo "duration (s): $t_vidSeconds" echo "pout : $p_out" echo "Type Ctrl-C to terminate recording" recordmydesktop -x $xPosn -y $yPosn --width $width --height $hight --v_quality 63 --s_quality 8 --device pulse --buffer-size 264620032 -o "$p_out" & # capture the process ID so it can later be killed # linux command "pidof" - process id of the specified program PID_recMyDsk="$(pidof recordmydesktop)" echo "PID_recMyDsk= $PID_recMyDsk" # wait for video to run & end, then kill record to start convert/save sleep "$t_vidSeconds" # normally SIGTERM 15, SIGINT = 2 to interrupt kill -15 $PID_recMyDsk } *********** #] 23Oct2019 search "Linux bash and how do I use variables in let arithmetic?" https://www.computerhope.com/unix/bash/let.htm >> great webPage Another way to perform arithmetic evaluation of an expression is to enclose it in double parentheses: (( myvar = 6 + 6 )); echo $myvar To return the value of an arithmetic expression directly, so that its result can be used as part of a larger statement, prefix the double parentheses with a dollar sign ("$"), like this: echo $(( 7 + 7 )) Also, note that double parentheses expressions can be nested: echo $(( myvar = 7 + $(( vartwo = 4 + 4 )) )) Let : $ let "varone = 1" "vartwo = varone++"; echo $varone, $vartwo 2, 1 $ let "varone = 1" "vartwo = ++varone"; echo $varone, $vartwo 2, 2 operators are listed row-by-row in order of decreasing precedence. Operators listed on the same row are of equal precedence : Operator Operation var++, var-- Post-increment (++), Post-decrement (--). Interpret the value of integer variable var and then add or subtract one (1) to it. ++var, --var Pre-increment (++), Pre-decrement (--). Add or subtract one (1) to the value of integer variable var, and then interpret the value. -expr, +expr Unary minus, Unary plus. Unary minus returns the value of the expression expr it precedes, as if it had been multiplied by negative one (-1). Unary plus returns the expression expr unchanged, as if it had been multiplied by one (1). !, ~ Logical negation, Bitwise negation. Logical negation returns false if its operand is true, and true if the operand is false. Bitwise negation flips the bits in the binary representation of the numeric operand. ** Exponentiation. *, /, % Multiplication, division, remainder (modulo). +, - Addition, subtraction. <<, >> Bitwise shift left, bitwise shift right. <=,>=,<,> Comparison: less than or equal to, greater than or equal to, less than, greater than. ==, != Equality, inequality. Equality returns true if its operands are equal, false otherwise. Inequality returns true if its operands are not equal, false otherwise. & Bitwise AND. The corresponding binary digits of both operands are multiplied to produce a result. For any given digit, the resulting digit is 1 only if the corresponding digit in both operands is also 1. ^ Bitwise XOR (eXclusive OR). A binary digit of the result is 1 if and only if the corresponding digits of the operands differ. For instance, if the first binary digit of the first operand is 1, and the second operand's first digit is 0, the result's first digit is 1. | Bitwise OR. If either of the corresponding digits in the operands is 1, that digit in the result is also 1. && Logical AND. Returns true if both of the operands are true. || Logical OR. Returns true if either of the operands is true. expr1 ? expr2 : expr3 Conditional (ternary) operator. If expr1 is true, return expr2. If expr1 is false, return expr3. =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= Assignment. Assign the value of the expression that follows the operator, to the variable that precedes it. If an operator prefixes the equals sign, that operation is performed before assignment. For instance, let "var += 5" is equivalent to let "var = var + 5". The assignment operation itself evaluates to the value assigned. 08********08 #] 10Aug2022 search "Linux bash and how do I add a series of numbers?" This looks VERY bad! bash just isn't made for this use QNial +-----+ https://stackoverflow.com/questions/61396970/how-do-i-sum-a-sequence-of-numbers-in-linux-bash-script How do I sum a sequence of numbers in Linux bash script? [duplicate] Asked 3 years, 3 months ago Modified 3 years, 3 months ago Viewed 918 times >> uses awk for initial, delta, final +-----+ https://stackoverflow.com/questions/3634012/shell-script-numbers-sum shell script + numbers sum Asked 12 years, 11 months ago Modified 12 years, 11 months ago Viewed 62k times +--+ You can eliminate the last dollar sign and freely space the operands and operators (including the variable and the assignment operator) for readability if you move the double parentheses all the way to the outside. num1=1232 num2=24 num3=444 (( sum = num1 + num2 + num3 )) (( count++ )) (( sum += quantity )) You can't use the increment style operators (*= /= %= += -= <<= >>= &= ^= |= ++ --) unless you use let or the outside (()) form (or you're incrementing variables or making assignments on the right hand side). answered Sep 3, 2010 at 15:02 Dennis Williamson +--+ you can use $(()) syntax, but if you have decimal numbers, use awk, or bc/dc to do your maths, "portably". answered Sep 3, 2010 at 10:02 ghostdog74 *********** #] 23Oct2019 reLineNum() # relineNum - re-line-number a list (eg Table of Contents, List of equations) reLineNum() { IFS='' while read -u 7 line; do let "lineNum=$( echo $line | sed 's/^\([0-9]\+\):.*/\1/' )" let "lineNum_new= $lineNum + $2 " lineBas=$( echo "$line" | sed 's/.*\(:.*\)/\1/' ) lineNew="$lineNum_new$lineBas" echo "$lineNew" >>"$3" done 7< "$1" } ************** #] 13Apr2019 see "/media/bill/SWAPPER/bin/pdftk copy, insert [ISBN, copyright] CRWN, EU, IEEE, USgov papers.sh" let "flag=$crwn + $euro + $ieee + $spcl + $usgv" if [ $flag -ge 1 ]; then echo "$pdf_uncp : $crwn, $euro, $ieee, $usgv" >>"$p_precpy_log" cat "$p_precpy_tmp" | sed -e "s/$sed_CRWN//;s/$sed_Euro//;s/$sed_IEEE//;s/$sed_USgv//" >>"$pdf_eras" fi } 23Oct2019 - maybe the following didn't work? erase_precopyrights() { while read -u 7 f_pre; do crwn=$( grep -cF "$footnote_CRWN" "$f_pre" ) euro=$( grep -cF "$footnote_Euro" "$f_pre" ) ieee=$( grep -cF "$footnote_IEEE" "$f_pre" ) spcl=$( grep -cF "$footnote_spcl" "$f_pre" ) usgv=$( grep -cF "$footnote_USgv" "$f_pre" ) done 7< "$p_precpy_tmp" **************************** #] 20July2018 bash arithmetic https://www.lifewire.com/arithmetic-in-bash-2200566 by Juergen Haas Updated March 18, 2018 Although Bash is a scripting language, it has pretty much all the capabilities of a general purpose programming language. This includes arithmetic functions. There are a number of syntax options you can use to evoke arithmetic evaluation of an expression. Perhaps the most readable one is the let command. For example let "m = 4 * 1024" will compute 4 times 1024 and assign the result to the variable "m". You can print out the result by adding an echo statement: let "m = 4 * 1024" echo $m You can test this from the command line by entering the following code: let "m = 4 * 1024"; echo $m You can also create a file containing the Bash commands, in which case you should add a line at the top of the file that specifies the program that is supposed to execute the code. For example: #!/bin/bash let "m = 4 * 1024" echo $m assuming the Bash executable is located in /bin/bash. You also need to set the permissions of your script file so that it is executable. Assuming the script file name is script1.sh, you can set the permissions to make the file executable with the command: chmod 777 script1.sh After that you can execute it with the command: ./script1.sh The available arithmetic operations are similar to those in standard programming languages like Java and C. Besides multiplication, as illustrated above, you use addition: let "m = a + 7" or subtraction: let "m = a - 7" or division: let "m = a / 2" or modulo (the remainder after an integer division): let "m = a % 100" When an operation is applied to the same variable to which the result is assigned you can use the standard arithmetic shorthand assignment operators, also referred to as compound assignment operators. For example, for addition, we have: let "m += 15" which is equivalent to "m = m + 15". For subtraction we have: let "m -= 3" which is equivalent to "m = m - 3". For division we have: let "m /= 5" which is equivalent to "m = m / 5". And for modulo, we have: **************************************** #] 05Aug2018 bash "/media/bill/SWAPPER/bin/dir_size sum, net.sh" #!/bin/sh # /media/bill/SWAPPER/bin/dir_size sum, net.sh # www.BillHowell.ca 05Aug2018 initial # to run $ bash "/media/bill/SWAPPER/bin/dir_size sum, net.sh" d_data="/media/bill/SWAPPER/bin/" d_temp="/media/bill/ramdisk/" f_du_data="$d_data""dir_size sum, net test.txt" f_sizes="$d_temp""dir_size sizes.txt" cat "$f_du_data" | grep --only-matching "^[0-9]*" >"$f_sizes" exec 9<"$f_sizes" read -u 9 line let "total = line" let "residual = line" echo "total= $total" calculations() { while read -u 9 line; do let "residual = residual - line" echo "residual= $residual" done let "percent = residual / total * 100" } calculations echo "" echo "total(Mb)= $total, residual(Mb)= $residual, residual(%)= $percent" $ bash "/media/bill/SWAPPER/bin/dir_size sum, net.sh" total= 27714 residual= 25881 residual= 25872 residual= 24491 residual= 18671 residual= 18646 residual= 18550 residual= 18549 residual= 1363 residual= 839 total(Mb)= 27714, residual(Mb)= 839, residual(%)= 0 ************************************ # enddoc