Thumb

Bash script example for practice


6/14/2016 12:00:00 AM

How to run bash?

To run bash program in your computer follow the instruction:

Step1: go to Desktop and create a file first.sh [you can use terminal/normally to create file]

Step2: copy and paste any one of my example code into your created  .sh extension  file.

Step3: go to terminal and follow the command

           cd Desktop           [go to the directory where your .sh extension file exist]

           chmod 755 first.sh

           ./first.sh

Step: Now output of your code will show

Example1 (bash echo/ bash array example):

#!/bin/bash
S=”Unix programming”
echo $S
Course=(CSE100 CSE103 MATH101)
echo ${Course[2]}
echo ${Course[*]}
echo ${Course[#]}

 Example2 (bash pipe):

#!/bin/bash
$who(1)  wc-e|_|
Cal  a.jpg|d.jpg|c.jpg|b.jpg
a=10
a=’expr  $a+1’
b=’expr 4+10\*5’
check=’expr  $a\>$b’

Example3 (bash case example):

#!/bin/bash
echo  “Type yes or no”
read a
case $a in y*|Y*)  echo  Yes!!;;
     n*|N*) echo No!!;;
     *) echo Wrong ans!!;;
esac

Example4 (bash do while):

#!/bin/bash
while read n
do
       echo You typed $n
       if [$n=”quite”]; then
       echo “quite)”
done

 

About Teacher

Reza Karim

Software Engineer

More about him