0% found this document useful (0 votes)
252 views

1) Shell Script To Print Your Name: Sub:Operating System

The document contains 16 code snippets of shell scripts with descriptions. The scripts perform tasks like: 1) Taking user input and printing it 2) Comparing two numbers and printing the greater 3) Repeatedly asking for the capital of Gujarat until the correct answer is given 4) Continuously taking a number less than 50 and printing its cube 5) Printing a triangle of increasing numbers 6) Finding the sum of the digits of a given number 7) Reversing a string 8) Printing the Fibonacci series up to a given range 9) Checking if a number is prime 10) Performing arithmetic operations using a case statement 11) Identifying months from abbreviations

Uploaded by

Yashvi Patel
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
252 views

1) Shell Script To Print Your Name: Sub:Operating System

The document contains 16 code snippets of shell scripts with descriptions. The scripts perform tasks like: 1) Taking user input and printing it 2) Comparing two numbers and printing the greater 3) Repeatedly asking for the capital of Gujarat until the correct answer is given 4) Continuously taking a number less than 50 and printing its cube 5) Printing a triangle of increasing numbers 6) Finding the sum of the digits of a given number 7) Reversing a string 8) Printing the Fibonacci series up to a given range 9) Checking if a number is prime 10) Performing arithmetic operations using a case statement 11) Identifying months from abbreviations

Uploaded by

Yashvi Patel
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 27

Sub:Operating System

1)SHELL SCRIPT TO PRINT YOUR NAME

clear
Echo “ enter your name:”
Read n
Echo “$n”

OUTPUT :

ENTER YOUR NAME : COMPUTER

COMPUTER

1
Sub:Operating System

2)SHELL SCRIPT TO FIND GREATER NUMBER BETWEEN TWO


NUMBER.

clear
echo "Enter the first number:"
read a

echo "Enter the second number:"


read b

if [ $a -gt $b ]
then
echo "GREATER NUMBER IS:"$a
else
echo "GREATER NUMBER IS:"$b
fi

OUTPUT :

ENTER THE FIRST NUMBER : 25


ENTER THE SECOND NUMBER : 52

GREATER NUMBER IS : 52

2
Sub:Operating System

3) SHELL SCRIPT THAT ASK CAPITAL OF GUJARAT AND


REPEAT THE QUESTION UNTIL USER GIVES CORRECT
ANSWER

while true
do
echo "ENTER THE CAPITAL OF GUJARAT"
read a

if [ $a = "gandhinagar" -o $a =
"GANDHINAGAR" ]
then
echo "you are right"
exit
fi
continue
done

OUTPUT :

ENTER THE CAPITAL OF GUJARAT : GANDHINAGAR

YOU ARE RIGHT

3
Sub:Operating System

4) ENTER THE NUMBER LESS THEN 50 AND DISPLAY THE


CUBE OF NUMBER IT SHOULD BE CONTINUE AS
USER WANTS

clear

while true
do
echo "ENTER THE NUMBER LESS THEN 50"
read no

if [ $no -lt 50 -a $no -gt 0 ]


then
s=`expr $no \* $no \* $no`
echo "Cube of $no IS $s"
else
echo "PLEASE ENTER VALID NUMBER"
fi

echo "DO YOU WANT TO CONTINUE(Y/N):"


read ans

if [ $ans = "y" -o $ans = "Y" ]


then
continue
else
exit
fi
done

OUTPUT :

ENTER THE NUMBER LESS THEN 50 : 25


CUBE OF 25 IS 15625
DO YOU WANT TO CONTINUE(Y/N): N

4
Sub:Operating System

5) SHELL SCRIPT TO PRINT THE FOLLOWING TRIANGLE


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

clear
echo “Enter the value of n”
read n
for (( i=1; i<=$n; i++ ))
do
for (( j=1; j<=i; j++ ))
do
echo -n "$j"
done
echo ""
done

OUTPUT :

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

This will produce the following result. It is important to


note how echo -n works here. Here -n option lets echo
avoid printing a new line character.

5
Sub:Operating System

6) SHELL SCRIPT TO FIND SUM OF ALL DIGIT FOR GIVEN


NUMBER.

Echo “enter any number “


Read n
s=0
d=0
while [ $n -gt 0 ]
do
d=`expr $n % 10`
s=`expr $s + $d`
n=`expr $n / 10`
done
echo "Sum of digit for number is”$s

OUTPUT :

ENTER ANY NUMBER : 33

SUM OF DIGIT FOR NUMBER IS : 6

6
Sub:Operating System

7) SHELL SCRIPT FOR REVERSE STRING

clear
echo "ENTER THE STRING :"
read a

b=`echo -n $a | wc -c`
#echo "value of b is :"$b

j=$b

echo "THE REVERSE STRING IS :"


while true
do
c=`echo $a | cut -c $j`
d=$d$c
j=`expr $j - 1`

if [ $j -eq 0 ]
then
echo $d
exit
fi
done

OUTPUT :

ENTER THE STRING : COMPUTER

THE REVERSE STRING IS RETUPMOC

7
Sub:Operating System

8)SHELL SCRIPT FOR FIBONACCI SERIES

clear

echo "ENTER THE RANGE OF SERIES"


read n
clear
echo "FIBONACCI SERIES UPTO" $n "AS BELOW"
num1=0
num2=1

echo $num1
echo $num2

while true
do
num3=`expr $num1 + $num2`
if [ $num3 -gt $n ]
then
break
fi
echo $num3
num1=$num2
num2=$num3

done

OUTPUT :

ENTER THE RANGE OF SERIES : 33

FIBONACCI SERIES UPTO 33 AS BELOW :


1 1 2 3 5 8 13 21

8
Sub:Operating System

9) SHELL SCRIPT FOR PRIME NO

clear

echo "ENTER THE LOWER LIMIT FOR THE PRIME NO"


read low

echo "ENTER THE UPPER LIMIT FOR THE PRIME NO"


read up

echo "value of low and up are :"$low","$up

echo "PRIME NO BETWEEN " $low "AND"$up "ARE AS


BELOW"

while [ $low -le $up ]


do

i=2

flag1=0
while [ $i -lt $low ]
do
j=`expr $low % $i`
# echo "vlaue of j is "$j
if [ $j -eq 0 ]
then
flag1=1
fi

i=`expr $i + 1`

done
if [ $flag1 -eq 1 ]
then
echo $low "is not prime no"
else
echo $low "is prime no"
fi

9
Sub:Operating System

low=`expr $low + 1`
done

OUTPUT :

ENTER THE LOWER LIMIT FOR THE PRIME NO : 1


ENTER THE UPPER LIMIT FOR THE PRIME NO : 17
value of low and up are : 1 , 17
PRIME NO BETWEEN 1 AND 17 ARE AS BELOW
1 2 3 5 7 11 13
1 IS PRIME NO

10
Sub:Operating System

10) SHELL SCRIPT TO CHECK ALL ARITHMETIC OPERATION


USING CASE STATEMENT

echo "Enter the first number:"


read a

echo "Enter the second number:"


read b

echo "1. ADDITION"


echo "2. SUBTRACTION"
echo "3. MULTIPLICATION"
echo "4. DIVISION"
echo "5. MODULOUS"
echo "6. EXIT"

echo "ENTER YOUR CHOICE"


read ch

case $ch in
1) echo ans =`expr $a + $b`;;
2) echo ans =`expr $a - $b`;;
3) echo ans =`expr $a \* $b`;;
4) echo ans =`expr $a / $b`;;
5) echo ans =`expr $a % $b`;;
6) exit ;;

*) echo "INVALID CHOICE ....ENTER ONLY BETWEEN 1..TO ...7"


Esac

OUTPUT :

NTER FIRST NUMBER : 8


ENTER SECOND NUMBER : 4

1. ADDITION
2. SUBTRACTON
3. MULTIPLICATION
4. DIVISION

11
Sub:Operating System

5. MODULOUS
6. EXIT

ENTER YOUR CHOICE : 1


ANS = 12

11) SHELL SCRIPT WHICH DISPLAYS JANUARY IF WE ENTER


JAN,JANU,JANUA,JANUARY ETC.

clear

echo "ENTER THE MONTH"


read month

case $month in
jan*) echo entered month is january;;
feb*) echo entered month is february;;
*) echo ENTERED MONTH COULD NOT BE IDENTIFIED;;
esac

OUTPUT :

ENTER THE MONTH : JANU

JANUARY

12
Sub:Operating System

12) SHELL SCRIPT THAT GRID THE USER BY SAYING GOOD


MORNING,GOOD AFTERNOON,GOOD EVENING ACCORDING
TO SYSTEM TIME
clear

date >dt
s=`cut -c 12-13 dt`

echo $s
if [ $s -lt 12 ]
then
echo "GOOD MORNING"
elif [ $s -gt 12 -a $s -lt 16 ]
then
echo "GOOD AFTERNOON"
elif [ $s -gt 15 -a $s -lt 24 ]
then
echo "GOOD EVENING"
# else
# echo "good night"
fi

OUTPUT :

GOOD MORNING

13
Sub:Operating System

13)SHELL SCRIPT WHICH COUNT NO OF WORDS,CHARACTERS


AND LINES WITHOUT TAKING CONSIDERATION OF BLANK
SPACES,TAB SPACES

clear
echo "ENTER THE FILENAME"
read fn

w=`wc -w $fn`
c=`wc -c $fn`
l=`wc -l $fn`

echo "TOTAL NO OF CHARACTER IS"$c


echo "TOTAL NO OF WORDS IS"$w
echo "TOTAL NO OF LINES IS"$l

OUTPUT :

ENTER THE FILENAME: DATA

TOTAL NO OF CHARCTER IS : 70
TOTAL NO OF WORDS IS : 20
TOTAL NO OF LINES IS : 3

14
Sub:Operating System

14) SHELL SCRIPT TO CHANGE THE FILE PERMISSION


ACCORDING TO USER'S REQUEST

clear

echo "ENTER THE FILE NAME"


read fn

echo "DO YOU WANT TO REVOKE READ/WRITE PERMISSION


(Y/N):?"
read ans

if [ $ans='y' -o $ans='Y' ]
then
echo "ENTER THE OCTAL VALUE FOR THE CHMOD(MAXIMUM
777)"
read c

chmod $c $fn

else
exit
fi

OUTPUT :

ENTER THE FILE NAME : DATA


DO YOU WANT TO REVOKE READ/WRITE PERMISSION (Y/N) :? Y
ENTER THE OCTAL VALUE FOR THE CHMOD 750

15
Sub:Operating System

15) SHELL SCRIPT TO ACCEPT TWO FILENAMES,IF BOTH


EXIST THEN CONTENTS OF FIRST FILE SHOULD
APPENDED IT.IF SECOND FILE DOES NOT EXIST THEN
CREATE IT WITH THE CONTENTS OF FIRST FILE

echo "ENTER THE FIRST FILE NAME"


read a
echo "ENTER THE SECOND FILE NAME"
read b

t1=0
t2=0

if [ -f $f1 ]
then
echo "first file exist in the directory"
t1=1
else
t1=0
fi

if [ -f $f2 ]
then
echo "second file exist in the directory"
t2=1
else
t2=0
fi

if [ $t1 -eq 1 -a $t2 -eq 1 ]


then
cat < $a >> $b
else
cp $a $b
fi

OUTPUT :
ENTER THE FIRST FILENAME : DATA1
ENTER THE SECOND FILENAME : DATA2

16
Sub:Operating System

16) SHELL SCRIPT FOR DIRECTORY PERMISSION

clear
Echo "Enter the name of the directory"
read a

if [ -d $a ]
then
echo "given name is of directory"
else
echo "it's not a directory"
fi

OUTPUT :

ENTER THE NAME OF THE DIRECTORY : CE


GIVEN NAME OF DIRECTORY CE

17
Sub:Operating System

17) SHELL SCRIPT TO CHECK WHETHER FILE WHOSE NAME


IS READ FROM KEYBOARD EIST AND READABLE

clear
echo "enter the file name"
read fn

if [ -f $fn ]
then
if [ -r $file ]
then
echo "FILE IS READABLE"

else
echo "FILE IS NOT READABLE"
fi
else
echo "FILE IS NOT EXIST"
fi

OUTPUT :

ENTER THE FILE NAME : DATA


FILE IS READABLE

18
Sub:Operating System

18) $ vi nestedfor.sh
for (( i = 1; i <= 5; i++ ))      ### Outer for loop ###
do

    for (( j = 1 ; j <= 5; j++ )) ### Inner for loop ###


    do
          echo -n "$i "
    done

  echo "" #### print the new line ###

done

Output:

11111
22222
33333
44444
55555

19) $ vi chessboard
for (( i = 1; i <= 9; i++ )) ### Outer for loop ###
do
   for (( j = 1 ; j <= 9; j++ )) ### Inner for loop ###
   do
        tot=`expr $i + $j`
        tmp=`expr $tot % 2`
        if [ $tmp -eq 0 ]; then
            echo -e -n "\033[47m "
        else
            echo -e -n "\033[40m "
        fi
  done
 echo -e -n "\033[40m" #### set back background colour to black
 echo "" #### print the new line ###
done

20) echo "Can you see the following:"

for (( i=1; i<=5; i++ ))


do
for (( j=1; j<=i; j++ ))
do
echo -n "$i"
done
echo ""
done
21) echo "Can you see the following:"

19
Sub:Operating System

for (( i=1; i<=5; i++ ))


do
for (( j=1; j<=i; j++ ))
do
echo -n "$j"
done
echo ""
done
22) echo "Climb the steps of success"

for (( i=1; i<=5; i++ ))


do
for (( j=1; j<=i; j++ ))
do
echo -n " |"
done
echo "_ "
done
23) echo "Stars"

for (( i=1; i<=5; i++ ))


do
for (( j=1; j<=i; j++ ))
do
echo -n " *"
done
echo ""
done
23) echo "Stars"

for (( i=1; i<=5; i++ ))


do
for (( j=1; j<=i; j++ ))
do
echo -n " *"
done
echo ""
done

for (( i=5; i>=1; i-- ))


do
for (( j=1; j<=i; j++ ))
do
echo -n " *"
done
echo ""
done
24) clear

for (( i=1; i<=3; i++ ))


do
for (( j=1; j<=i; j++ ))
do
echo -n "|Linux"
done
echo "______"

20
Sub:Operating System

done

for (( i=3; i>=1; i-- ))


do
for (( j=1; j<=i; j++ ))
do
echo -n "|Linux"
done

if [ $i -eq 3 ]; then
echo -n "______"
echo -n -e ">> Powerd Server.\n"
else
echo "~~~~~"
fi
done
25)
MAX_NO=0

echo -n "Enter Number between (5 to 9) : "


read MAX_NO

if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then


echo "I ask to enter number between 5 and 9, Okay"
exit 1
fi

clear

for (( i=1; i<=MAX_NO; i++ ))


do
for (( s=MAX_NO; s>=i; s-- ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " $i"
done
echo ""
done

for (( i=1; i<=MAX_NO; i++ ))


do
for (( s=MAX_NO; s>=i; s-- ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " ."
done
echo ""
done

echo -e "\n\n\t\t\tI hope you like it my stupidity (?)"


26) MAX_NO=0

21
Sub:Operating System

echo -n "Enter Number between (5 to 9) : "


read MAX_NO

if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then


echo "I ask to enter number between 5 and 9, Okay"
exit 1
fi

clear

for (( i=1; i<=MAX_NO; i++ ))


do
for (( s=MAX_NO; s>=i; s-- ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " $i"
done
echo ""
done

for (( i=1; i<=MAX_NO; i++ ))


do
for (( s=MAX_NO; s>=i; s-- ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " ."
done
echo ""
done

echo -e "\n\n\t\t\tI hope you like it my stupidity (?)"


27) MAX_NO=0

echo -n "Enter Number between (5 to 9) : "


read MAX_NO

if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then


echo "I ask to enter number between 5 and 9, Okay"
exit 1
fi

clear

for (( i=1; i<=MAX_NO; i++ ))


do
for (( s=MAX_NO; s>=i; s-- ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))

22
Sub:Operating System

do
echo -n " ."
done
echo ""
done
###### Second stage ######################
##
##
for (( i=MAX_NO; i>=1; i-- ))
do
for (( s=i; s<=MAX_NO; s++ ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " ."
done
echo ""
done

echo -e "\n\n\t\t\tI hope you like it my stupidity (?)"

23
Sub:Operating System

a = 10
a=10
until [ $a -lt 10 ]
do
echo $a
a = `expr $a + 1`
done
a = 0
while [ "$a" -lt 10 ] # this is loop1
do
b = "$a"
while [ "$b" -ge 0 ] # this is loop2
do
echo -n "$b "
b = `expr $b - 1`
done
echo
a = `expr $a + 1`
done
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0

The break Statement


The break statement is used to terminate the execution of the entire loop, after
completing the execution of all of the lines of code up to the break statement. It then steps
down to the code following the end of the loop.

Syntax:

break n

Here n specifies the nth enclosing loop to the exit from.

a = 0

while [ $a -lt 10 ]
do
echo $a
if [ $a -eq 5 ]
then
break

24
Sub:Operating System

fi
a = `expr $a + 1`
done

For loop

for var1 in 1 2 3
do
for var2 in 0 5
do
if [ $var1 -eq 2 -a $var2 -eq 0 ]
then
break 2
else
echo "$var1 $var2"
fi
done
done

The continue statement


The continue statement is similar to the break command, except that it causes the
current iteration of the loop to exit, rather than the entire loop.

This statement is useful when an error has occurred but you want to try to execute the
next iteration of the loop.

continue n

Here n specifies the nth enclosing loop to continue from.

NUMS = "1 2 3 4 5 6 7"

for NUM in $NUMS


do
Q = `expr $NUM % 2`
if [ $Q -eq 0 ]
then
echo "Number is an even number!!"
continue
fi
echo "Found odd number"
done

What is Substitution?
The shell performs substitution when it encounters an expression that contains one or
more special characters.

25
Sub:Operating System

Here, the printing value of the variable is substituted by its value. Same time, "\n" is
substituted by a new line −

#!/bin/sh

a = 10
echo -e "Value of a is $a \n"

You will receive the following result. Here the -e option enables the interpretation of
backslash escapes.

Value of a is 10

Following is the result without -e option −

Value of a is 10\n
DATE = `date`
echo "Date is $DATE"

USERS = `who | wc -l`


echo "Logged in user are $USERS"

UP = `date ; uptime`
echo "Uptime is $UP"
echo ${var:-"Variable is not set"}
echo "1 - Value of var is ${var}"

echo ${var:="Variable is not set"}


echo "2 - Value of var is ${var}"

unset var
echo ${var:+"This is default value"}
echo "3 - Value of var is $var"

var = "Prefix"
echo ${var:+"This is default value"}
echo "4 - Value of var is $var"

echo ${var:?"Print this message"}


echo "5 - Value of var is ${var}"
for (( i=1; i <= 5; i++ ))
do
echo "Random number $i: $RANDOM"
done
for i in 1 2 3 4 5
do
echo "Random number $i: $RANDOM"
done
for i in $(seq 1 5)
do
echo "Random number $i: $RANDOM"
done

26
Sub:Operating System

27

You might also like