Book Image

Linux Shell Scripting Cookbook, Second Edition - Second Edition

Book Image

Linux Shell Scripting Cookbook, Second Edition - Second Edition

Overview of this book

The shell remains one of the most powerful tools on a computer system — yet a large number of users are unaware of how much one can accomplish with it. Using a combination of simple commands, we will see how to solve complex problems in day to day computer usage.Linux Shell Scripting Cookbook, Second Edition will take you through useful real-world recipes designed to make your daily life easy when working with the shell. The book shows the reader how to effectively use the shell to accomplish complex tasks with ease.The book discusses basics of using the shell, general commands and proceeds to show the reader how to use them to perform complex tasks with ease.Starting with the basics of the shell, we will learn simple commands with their usages allowing us to perform operations on files of different kind. The book then proceeds to explain text processing, web interaction and concludes with backups, monitoring and other sysadmin tasks.Linux Shell Scripting Cookbook, Second Edition serves as an excellent guide to solving day to day problems using the shell and few powerful commands together to create solutions.
Table of Contents (16 chapters)
Linux Shell Scripting Cookbook
About the Authors
About the Reviewers

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:

  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
    result=`echo "$no * 1.5" | bc`
    echo $result

    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
    • 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:

      	Desc: Number conversion
      	echo "obase=2;$no" | bc
      	echo "obase=10;ibase=2;$no" | bc
    • Calculating squares and square roots can be done as follows:

      	echo "sqrt(100)" | bc #Square root
      	echo "10^10" | bc #Square