Bash Scripting

Example 1: Example to demostrate use of read command used to accept input from keyboard, variable and $().

Write a program to display welcome message and Current System’s date. You should give your name as an input and script should return your name and date in following format.
Welcome <your_name>. Current date is: <date/time>


#!/bin/bash
read name
echo Welcome $name. Current date is: $(date)


Note:
“read name” : statement reads input from user (keyboard) and store it in variable called name
“echo Welcome $name. Current date is: $(date)” : to access value of name variable we need to prefix $ sign with variable. So if you input ‘shiba’ as your name, $name will be replaced with ‘shiba’ string. Similarly, This example also contain $(date) expression. Here, $() is used to execute command and return output. So, the output of date command is returned to the statement.

Example 2: working with parameters

Write a program to display your name and address which should be passed to script as parameter (command line argument). If any one of the arguments (parameters) is missing, the program should display error message.


#!/bin/bash

if [ $# -eq 0 ];then
echo “Please enter your name and address as paramenter”
echo “Syntax: $0 <your_name> <your_address>”
echo “example: $0 shiba Kathmandu”
exit 1
fi
name=$1
city=$2
echo Welcome $name from $city


Note:
$# : gives number of parameter passed to the script. if you don’t pass any parameter it will be 0.
if [ $# -eq 0 ];then : -eq operator compare two integer variables. And, this statement ends with fi. remember fi is reverse string of if.

Example 3: Print all parameters passed to script


#!/bin/bash
for i in $@
do
echo $i
done


Note: ‘$@’ : lists out all parameters passed to script. ‘for loop’ in the given example gets input from $@. i variable contains value of $@ in each loop. So ‘echo $i’, prints the value in current loop.

Example 4: Write a program to input number in the script.

if the value is 1, it should print “One”
if the value is 2, it should print “Two”
Otherwise print “you haven’t enter 1 or 2″


Note:Using if (assuming 1 and 2 as string instead of integer, in case of integer we need to use -eq, and if we try it and user inputs alphabets there will be an error with -eq so use = to compare input as string)


#!bin/bash
echo “Enter number”
read num

case $num in
1)
echo “One”;;
2)
echo “Two”;;
*)
echo “you haven’t enter 1 or 2″
esac


Alternative,


#!/bin/bash

echo “Enter number”
read num

if [ “$num” = “1” ];then
echo “One”
elif [ “$num” = “2” ];then
echo “Two”
else
echo “you haven’t enter 1 or 2″
fi


Example 5
Write a script where you should input a number and display message that the number lies between 0 to 10 or not.


#/!bin/bash
echo “Enter number”
read num

if [ $num -ge 0 ] && [ $num -lt 10 ];then
echo “You have entered number 1 to 9″
else
echo “You haven’t entered number between 1 and 9″
fi


Note:

&&: and operator is used to and output of two test, if both of them are true, the result will be true else false. || : similarly, you may require to use or operators, where if any one of two operators is true, the result should be true.

Try it:

Additional questions:

  • Write a script which list top 5 processes, only include if the process is not mariadb, write the output to /root/output.txt. Count number of processes which uses greater than 30 process CPU.
  • Write a program to create users from a csv file. csv file contains contains uid,username,password in each row. You should handle possible errors in error like missing file, missing parameters etc.
    myfile.csv contains:
    10023,shiba,mypassword
    10234,reewa,mytest

Leave a Reply

Your email address will not be published.


× 5 = forty

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Recent Posts
Recent Comments
    Archives
    Categories
    Updates on Recent activieies
    Meta