Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Linux Shell Scripting Cookbook

You're reading from   Linux Shell Scripting Cookbook Do amazing things with the shell and automate tedious tasks

Arrow left icon
Product type Paperback
Published in May 2017
Publisher
ISBN-13 9781785881985
Length 552 pages
Edition 3rd Edition
Tools
Arrow right icon
Authors (3):
Arrow left icon
Clif Flynt Clif Flynt
Author Profile Icon Clif Flynt
Clif Flynt
Sarath Lakshman Sarath Lakshman
Author Profile Icon Sarath Lakshman
Sarath Lakshman
Shantanu Tushar Shantanu Tushar
Author Profile Icon Shantanu Tushar
Shantanu Tushar
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Shell Something Out FREE CHAPTER 2. Have a Good Command 3. File In, File Out 4. Texting and Driving 5. Tangled Web? Not At All! 6. Repository Management 7. The Backup Plan 8. The Old-Boy Network 9. Put On the Monitors Cap 10. Administration Calls 11. Tracing the Clues 12. Tuning a Linux System 13. Containers, Virtual Machines, and the Cloud

Math with the shell

The Bash shell performs basic arithmetic operations using the let, (( )), and [] commands. The expr and bc utilities are used to perform advanced operations.

How to do it...

  1. A numeric value is assigned to a variable the same way strings are assigned. The value will be treated as a number by the methods that access it:
        #!/bin/bash
        no1=4;
        no2=5;
  1. The let command is used to perform basic operations directly. Within a let command, we use variable names without the $ prefix. Consider this example:
        let result=no1+no2
        echo $result

                  Other uses of let command are as follows:

  • Use this for increment: 
                $ let no1++
  • For decrement, use this:
                $ let no1--
  • Use these for shorthands:
                let no+=6
                let no-=6

                These are equal to let no=no+6 and let no=no-6, respectively.

  • Alternate methods are as follows:

              The [] operator is used in the same way as the let command:

                    result=$[ no1 + no2 ]

              Using the $ prefix inside the [] operator is legal; consider this example:

                    result=$[ $no1 + 5 ]

      The (( )) operator can also be used. The prefix variable names                        with a $ within the (( )) operator:

                    result=$(( no1 + 50 ))

             The expr expression can be used for basic operations:

                    result=`expr 3 + 4`
                    result=$(expr $no1 + 5)

      The preceding methods do not support floating point numbers,
      and operate on integers only.

  1. The bc application, the precision calculator, is an advanced utility for mathematical operations. It has a wide range of options. We can perform floating point arithmetic and use advanced functions:
        echo "4 * 0.56" | bc
        2.24
        no=54;
        result=`echo "$no * 1.5" | bc`
        echo $result
        81.0

The bc application accepts prefixes to control the operation. These are separated from each other with a semicolon.

  • Decimal places scale with bc: In the following example, the scale=2 parameter sets the number of decimal places to 2. Hence, the output of bc will contain a number with two decimal places:
                echo "scale=2;22/7" | bc
                3.14
  • Base conversion with bc: We can convert from one base number system to another one. This code converts numbers from decimal to binary and binary to decimal:
                #!/bin/bash
                Desc: Number conversion
                no=100
                echo "obase=2;$no" | bc 
                1100100
                no=1100100
                echo "obase=10;ibase=2;$no" | bc
                100
  • The following examples demonstrate calculating squares and square roots:
                echo "sqrt(100)" | bc #Square root
                echo "10^10" | bc #Square
You have been reading a chapter from
Linux Shell Scripting Cookbook - Third Edition
Published in: May 2017
Publisher:
ISBN-13: 9781785881985
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime