1) Shell Script To Print Your Name: Sub:Operating System
1) Shell Script To Print Your Name: Sub:Operating System
clear
Echo “ enter your name:”
Read n
Echo “$n”
OUTPUT :
COMPUTER
1
Sub:Operating System
clear
echo "Enter the first number:"
read a
if [ $a -gt $b ]
then
echo "GREATER NUMBER IS:"$a
else
echo "GREATER NUMBER IS:"$b
fi
OUTPUT :
GREATER NUMBER IS : 52
2
Sub:Operating System
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 :
3
Sub:Operating System
clear
while true
do
echo "ENTER THE NUMBER LESS THEN 50"
read no
OUTPUT :
4
Sub:Operating System
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
5
Sub:Operating System
OUTPUT :
6
Sub:Operating System
clear
echo "ENTER THE STRING :"
read a
b=`echo -n $a | wc -c`
#echo "value of b is :"$b
j=$b
if [ $j -eq 0 ]
then
echo $d
exit
fi
done
OUTPUT :
7
Sub:Operating System
clear
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 :
8
Sub:Operating System
clear
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 :
10
Sub:Operating System
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 ;;
OUTPUT :
1. ADDITION
2. SUBTRACTON
3. MULTIPLICATION
4. DIVISION
11
Sub:Operating System
5. MODULOUS
6. EXIT
clear
case $month in
jan*) echo entered month is january;;
feb*) echo entered month is february;;
*) echo ENTERED MONTH COULD NOT BE IDENTIFIED;;
esac
OUTPUT :
JANUARY
12
Sub:Operating System
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
clear
echo "ENTER THE FILENAME"
read fn
w=`wc -w $fn`
c=`wc -c $fn`
l=`wc -l $fn`
OUTPUT :
TOTAL NO OF CHARCTER IS : 70
TOTAL NO OF WORDS IS : 20
TOTAL NO OF LINES IS : 3
14
Sub:Operating System
clear
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 :
15
Sub:Operating System
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
OUTPUT :
ENTER THE FIRST FILENAME : DATA1
ENTER THE SECOND FILENAME : DATA2
16
Sub:Operating System
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 :
17
Sub:Operating System
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 :
18
Sub:Operating System
18) $ vi nestedfor.sh
for (( i = 1; i <= 5; i++ )) ### Outer for loop ###
do
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
19
Sub:Operating System
20
Sub:Operating System
done
if [ $i -eq 3 ]; then
echo -n "______"
echo -n -e ">> Powerd Server.\n"
else
echo "~~~~~"
fi
done
25)
MAX_NO=0
clear
21
Sub:Operating System
clear
clear
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
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
Syntax:
break n
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
This statement is useful when an error has occurred but you want to try to execute the
next iteration of the loop.
continue n
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
Value of a is 10\n
DATE = `date`
echo "Date is $DATE"
UP = `date ; uptime`
echo "Uptime is $UP"
echo ${var:-"Variable is not set"}
echo "1 - 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"
26
Sub:Operating System
27