Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
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, Second Edition

You're reading from   Linux Shell Scripting Cookbook, Second Edition Don't neglect the shell ‚Äì this book will empower you to use simple commands to perform complex tasks. Whether you're a casual or advanced Linux user, the cookbook approach makes it all so brilliantly accessible and, above all, useful.

Arrow left icon
Product type Paperback
Published in May 2013
Publisher Packt
ISBN-13 9781782162742
Length 384 pages
Edition 2nd Edition
Tools
Arrow right icon
Toc

Table of Contents (16) Chapters Close

Linux Shell Scripting Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
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. The Backup Plan 7. The Old-boy Network 8. Put on the Monitor's Cap 9. Administration Calls Index

Math with the shell


Arithmetic operations are an essential requirement for every programming language. In this recipe, we will explore various methods for performing arithmetic operations in shell.

Getting ready

The Bash shell environment can perform basic arithmetic operations using the commands let, (( )), and []. The two utilities expr and bc are also very helpful in performing advanced operations.

How to do it...

  1. A numeric value can be assigned as a regular variable assignment, which is stored as a string. However, we use methods to manipulate as numbers:

    #!/bin/bash
    no1=4;
    no2=5;
    
  2. The let command can be used to perform basic operations directly. While using let, we use variable names without the $ prefix, for example:

    let result=no1+no2
    echo $result
    
    • Increment operation:

      	$ let no1++
      
    • Decrement operation:

      	$ let no1--
      
    • Shorthands:

      	let no+=6
      	let no-=6
      

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

    • Alternate methods:

      The [] operator can be used in the same way as the let command as follows:

      	result=$[ no1 + no2 ]
      

      Using the $ prefix inside [] operators are legal, for example:

      	result=$[ $no1 + 5 ]
      

      (( )) can also be used. $ prefixed with a variable name is used when (( )) operator is used, as follows:

      	result=$(( no1 + 50 ))
      

      expr can also be used for basic operations:

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

      All of the preceding methods do not support floating point numbers, and operate on integers only.

  3. bc , the precision calculator is an advanced utility for mathematical operations. It has a wide range of options. We can perform floating point operations and use advanced functions as follows:

    echo "4 * 0.56" | bc
    2.24
    
    no=54; 
    result=`echo "$no * 1.5" | bc`
    echo $result
    81.0
    

    Additional parameters can be passed to bc with prefixes to the operation with semicolon as delimiters through stdin.

    • 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;3/8" | bc
      	0.37
      
    • Base conversion with bc: We can convert from one base number system to another one. Let us convert from decimal to binary, and binary to octal:

      	#!/bin/bash
      	Desc: Number conversion
      	
      	no=100
      	echo "obase=2;$no" | bc
      	1100100
      	no=1100100
      	echo "obase=10;ibase=2;$no" | bc
      	100
      
    • Calculating squares and square roots can be done as follows:

      	echo "sqrt(100)" | bc #Square root
      	echo "10^10" | bc #Square
      
You have been reading a chapter from
Linux Shell Scripting Cookbook, Second Edition - Second Edition
Published in: May 2013
Publisher: Packt
ISBN-13: 9781782162742
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