Playing with shell!

Playing with shell

Here I have summarized few shell script which I routinely use. All the informations are floating in the net. It needs just a little bit of careful google search and then test by yourself.
  1. Start with a simple one: Addition, subtraction, multiplication and division 
  2. Let say a=10, b=5, c=a+b or a-b or a/b or a*b. How to do that using Bash (Born Again Shell) shell scripting? However, bash script will give result in integer number.   
    a=10
    b=5
    c=$(($a+$b))
    echo $c

    It will print 15.

    c=$(($a-$b))
    echo $c
    It will print 5.

    c=$(($a/$b))
    echo $c
    It will print 2.
     
    c=$(($a*$b))
    echo $c

    It will print 50.

    Let say you would like to do little complex calculation like
     c=(a+b)/a*10 - b/(a-100)
     

    c=$(($(($a+$b))/$(($a*10))-$(($b/$(($a-100))))))

  1. What if one needs to get the same operation for real numbers? One needs to use then awk script.
  2. a=10.5
    b=2.5
    c=$(echo $a $b | awk '{ print $1 + $2 }')
    echo $c
    It will print 13.
     
     c=$(echo $a $b | awk '{ print $1 - $2 }')
    echo $c
    It will print 8.
     
    c=$(echo $a $b | awk '{ print $1 * $2 }')
    echo $c
    It will print 26.25.
     
    c=$(echo $a $b | awk '{ print $1/$2 }')
    echo $c
    It will print 4.2.

Comments

Popular posts from this blog

VMD High Resolution Image and Movie

Inset Plot in GRACE (Xmgrace)