A dip in the cup of Bash scripting

Scripting is one of the most powerful tools of any Linux box. Tasks which require repetitive actions and seem to take forever to complete can be automated in a jiffy. The possibilities are endless. Since built in Linux commands can be used in shell scripts this opens up countless ways to write scripts. The three scripts that follow are basic examples to get you started. In the future I will be writing a tutorial on bash scripting. For now, im throwing a bit of source code in your face.

Script 1 Simple Arithmetic Operations

#Simple shell script to illustrate basic arithmetic operations
#2005 (c) synapse – https://synapse.wordpress.com
#Feel free to modify or distribute this code in any medium so as long as these top 3 lines remain unchanged

#!/bin/bash

first_num=0
second_num=0

echo -n “Enter the first number :”
read first_num
echo -n “Enter the second number :”
read second_num

echo “first number + second number = $((first_num + second_num))”
echo “first number – second number = $((first_num – second_num))”
echo “first number * second number = $((first_num * second_num))”
echo “first number / second number = $((first_num / second_num))”
echo “first number % second number = $((first_num % second_num))”
echo “first number raised to the”
echo “power of the second number = $((first_num ** second_num))”

Script 2 Even Odd Numbers

#Simple shell script to calculate whether a number is odd or even
#2005 (c) synapse – https://synapse.wordpress.com
#Feel free to modify or distribute this code in any medium so as long as these top 3 lines remain unchanged

#!/bin/bash

number=0

echo -n “Enter a number :”
read number

echo “Number is $number”
if [ $((number % 2)) -eq 0 ]; then
echo “Number is even”
else
echo “Number is odd”
fi

Script 3 Convert Seconds Into Hours And Minutes

#Simple shell script to convert arbitary seconds into hours and minutes
#2005 (c) synapse – https://synapse.wordpress.com
#Feel free to modify or distribute this code in any medium so as long as these top 3 lines remain unchanged

#!/bin/bash

seconds=0

echo -n “Enter number of seconds :”
read seconds

hours=$((seconds / 3600))
seconds=$((seconds % 3600))
minutes=$((seconds / 60))
seconds=$((seconds % 60))

echo “$hours hour(s) $minutes minute(s) $seconds second(s)”

Script 4 Using The Select Case In Bash Scripts

#Simple shell script to illustrate the usage of the select case statement
#2005 (c) synapse – https://synapse.wordpress.com
#Feel free to modify or distribute this code in any medium so as long as these top 3 lines remain unchanged

#!/bin/bash

echo -n “Enter a number between 1 and 3 :”
read character
case $character in
1 ) echo “You entered one.”;;
2 ) echo “You entered two.”;;
3 ) echo “You entered three.”;;
* ) echo “You did not enter a number between 1 and 3”
esac

2005 © synapse. https://synapse.wordpress.com. All rights reserved. This document may be reproduced in any medium provided this copyright notice is included.

Advertisements

About synapse
Programming, motorcycles and photography. Want to do more, but only have time for so much!

7 Responses to A dip in the cup of Bash scripting

  1. Pingback: at t dsl

  2. Pingback: definition of meningitis

  3. Pingback: miniature pool table

  4. Pingback: address book print free

  5. Pingback: Casin Roulette online

  6. Pingback: rocket german

  7. Jadu Saikia says:

    Nice blog

    Bash one liner:

    $ secs=456
    $ printf “”%dh:%dm:%ds”\n” $(($secs/3600)) $(($secs%3600/60)) $(($secs%60))

    0h:7m:36s

    The awk one liner for this:

    $ echo – | awk -v “S=456” ‘{printf “%dh:%dm:%ds”,S/(60*60),S%(60*60)/60,S%60}’

    0h:7m:36s

    // Jadu, http://unstableme.blogspot.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: