Basic shell script program in Unix/Linux.

The Basic shell script program which is used in Unix.

1. Printing count of arguments passed by command line

echo $#
exit 0

rock@rock-VirtualBox:~$ bash file.sh 1 2 3
3
rock@rock-VirtualBox:~$ bash file.sh 1 2 3 4 5
5

2. Accept Age from user and if user enter 18-60 age then give message ELIGIBLE FOR VOTING and else INVALID

echo Enter the Age:
read age
if (( $age >=18 && $age <=60 ))
then
	echo "ELIGIBLE FOR VOTING"
else
	echo "INVALID"
fi

rock@rock-VirtualBox:~$ bash file.sh
Enter the Age:
13
INVALID
rock@rock-VirtualBox:~$ bash file.sh
Enter the Age:
37
ELIGIBLE FOR VOTING

3. Accept a number from user and test whether it is “POSITIVE” or “ZERO” and “NEGATIVE”.

echo Enter the no:
read no
if (( $no > 0 ))
then
	echo POSITIVE NO
elif(( $no == 0))
then
	echo ZERO
else
	echo NEGATIVE
fi

rock@rock-VirtualBox:~$ bash file.sh
Enter the no:
5
POSITIVE NO
rock@rock-VirtualBox:~$ bash file.sh
Enter the no:
-1
NEGATIVE
rock@rock-VirtualBox:~$ bash file.sh
Enter the no:
0
ZERO

4. Ask user how many times to print hello

Using while loop

echo "Enter no:"
read n
echo ---
i=1
while (( $i <= n))
do
	echo $i. Hello
	((i++))
done


rock@rock-VirtualBox:~$ bash file.sh
Enter no:
5

1. Hello
2. Hello
3. Hello
4. Hello
5. Hello

Using for loop

echo "Enter no:"
read n
echo ---

for (( i=1; i<=n;i++))
do
	echo $i. Hello
	
done

rock@rock-VirtualBox:~$ bash file.sh
Enter no:
5


1. Hello
2. Hello
3. Hello
4. Hello
5. Hello

using util loop

echo "Enter no:"
read n
echo ---
i=1
until (( $i > $n))
do
	echo $i. Hello
	((i++))
done




rock@rock-VirtualBox:~$ bash file.sh
Enter no:
5

1. Hello
2. Hello
3. Hello
4. Hello
5. Hello

5. Check whether user entered number is Prime or not.

  • The expr command used to evaluate a given expression and display its corresponding output.
  • Used for basic operations like addition, subtraction, multiplication, division and modulus on integers.

echo Enter number:
read number
i=2 
f=0 
while test $i -le `expr $number / 2` 
do
	if test `expr $number % $i` -eq 0 
	then
		f=1 
	fi
	i=`expr $i + 1` 
done
if test $f -eq 1 
then
	echo "Not Prime"
else
	echo "Prime"
fi

echo Enter number:
read number
i=2 
f=0 
while [ $i -le `expr $number / 2` ]
do
	if [ `expr $number % $i` -eq 0 ]
	then
		f=1 
	fi
	i=`expr $i + 1` 
done
if [ $f -eq 1 ]
then
	echo "Not Prime"
else
	echo "Prime"
fi

 

rock@rock-VirtualBox:~$ bash file.sh
Enter number:
4
Not Prime
rock@rock-VirtualBox:~$ bash file.sh
Enter number:
57
Not Prime

6. Fibonacci series.

echo "enter the no:"
read no
a=0
b=1
echo "Fibonacci series upto $no:"
for(( i=0; i<no; i++))
do
    echo  "$a"
    c=$((a+b)) 
    a=$b
    b=$c
done


$ bash file.sh
enter the no:
7
Fibonacci series upto 7:
0
1
1
2
3
5
8

7.Check whether user entered number is EVEN or ODD.

echo Enter no:
read n
if [ $((n%2)) -eq 0]
then
       echo "$n is even"
else
       echo "$n is odd" 
fi


rock@rock-VirtualBox:~$ bash file.sh
Enter number:
4
4 is even
rock@rock-VirtualBox:~$ bash file.sh
Enter number:
57
57 is odd

About Ashishkumar Vishwakarma

I am Ashish- a Developer live in Mumbai.

View all posts by Ashishkumar Vishwakarma →

2 Comments on “Basic shell script program in Unix/Linux.”

Leave a Reply