Sunteți pe pagina 1din 13

S.No.

1
2
3
4
5
6
7
8
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.

1|Page

Particulars
Add two numbers
Copy one file into another
Find maximum of three numbers.
Addition, Subtraction, Multiplication & Division of two numbers.
Sum of digits of a number.
Fibonacci Series
Check whether user have entered a char. digit or special symbol.
Palindrome
Pyramid Pattern
Right triangle og numbers Pattern
Stair-case Pattern
Double Pyramid Pattern
Sorting of Integer Array
Search an element in Array
Find MAX. & MIN. No. in Array
Print Array in reverse order.
Check whether a file is regular,directory, char. specific or block file.
Check whether a file has read, write, execute permissions or not?
Compare two strings.
Check whether two files are identical or not?
Locks the terminal
Monitors Disk-space
Changes user-accounts password automatically
Digital clock
Records keystroke
Percentage usage of CPU by different processes.
Percenteage usage of MEMORY by different processes.

SS 1.Write a shell script that adds two numbers entered by user.


echo
echo
read
echo
read
echo
expr

"Enter Two Numbers"


n "a:"
a
n "b:"
b
n "sum of $a and $b is:"
$a + $b

SS 2.Write a shell script that displays the contents of one file & copies the contents of one
file to another.
vi file1
cat file1
cp file1 file2
cat file2

SS 3.Write a shell script that finds the greatest of three numbers entered by user.

echo -n "Enter the first number:"


read number1
echo -n "Enter the second number:"
read number2
echo -n "Enter the third number:"
read number3
if test $number1 -gt $number2 -a $number1 -gt $number3
then
echo $number1 is greatest of the three
elif test $number2 -gt $number1 -a $number2 -gt $number3
then
echo $number2 is greatest of the three
else
echo $number3 is greatest of the three
fi

2|Page

SS 4.Write a shell script for performing Addition, Subtraction, Multiplication & Division on
the two numbers entered by user.
echo
read
echo
read
echo
expr
echo
expr
echo
expr
echo
expr

n
a
n
b
n
$a
n
$a
n
$a
n
$a

Enter number1:
Enter number2:
addition:
+ $b
subtraction:
- $b
division:
/ $b
#you
multiplication:
\* $b

will get the integer part only in ans.

SS 5.Write a shell script that finds & prints the sum of all digits of a number entered by
user.

#shell script to calculate the sum of digits of a number

echo "enter the number"


read number
echo $number
d=0
sum=0
while [$number -ne 0]
do
d=`expr $number % 10`
sum=`expr $sum + $d`
number=`expr $number / 10`
done
echo sum of digits=$sum

SS 6.Write a shell script that prints the Fibonacci series up to n(entered by user)
terms.
3|Page

#shell script to print the Fibonacci series

echo enter the number of terms


read number
a=0
b=1
c=0
echo Fibonacci series up to $number terms
echo $a
echo $b
for (( i=2 ; i<$number ; i++ ))
do
c=`expr $a + $b`
a=`expr $b`
b=`expr $c`
echo $c
done

SS 7.Write a shell script that tells whether the user has entered a character, a digit or
any special character.
#check whether the user has entered a digit or char or any special character

echo n Give the input:


read a
case $a in
[0-9])
echo u have entered a digit ;;
[a-z])
echo u have entered a lowercase character ;;
[A-Z])
echo u have entered an uppercase character ;;
*)
echo you have entered a special character ;;
esac

SS 8.Write a shell script that tells whether the entered string is palindrome or not.
#shell script to check whether a given string is palindrome or not

echo n "Enter a string:"


read string
len=`expr $string | wc -c`
len=`expr $len - 1`
4|Page

i=1
j=`expr $len / 2`
while test $i -le $j
do
k=`echo $string | cut -c $i`
l=`echo $string | cut -c $len`
if test $k != $l
then
echo "String is not Palindrome."
exit
fi
i=`expr $i + 1`
len=`expr $len - 1`
done
echo "String is Palindrome."

SS 9.Write a shell script that draws a pattern given on the right side.
for (( i=1 ; i<10 ; i++ ))
do
for (( j=10 ; j>i ; j-- ))
do
echo -n " "
done
for (( k=1 ; k<i ; k++ ))
do
echo -n e "\033[32m* "
done
echo e \033[0m
#this
done

*
**
***
****
*****
******

#this will make the stars green


will make the fonts effect back to original

SS 10.Write a shell script that draws a pattern given on the right side.
for (( i=1 ; i<7 ; i++ ))
do
for (( j=1; j<i ; j++ ))
do
echo -n -e "\033[33m $j"
done
echo e \033[0m
done
5|Page

1
12
123
1234
12345

SS 11.Write a shell script that draws a pattern given on the right side.
echo _
for((i=0;i<8;i++))
do
for((j=0;j<=i;j++))
do
echo -n " "
done
echo n " l_"
echo
done

_
|_
|_
|_
|_

#two blank spaces

SS 12.Write a shell script that draws a pattern given on the right side.
for (( i=1 ; i<10 ; i++ ))
do
for (( j=10 ; j>i ; j-- ))
do
echo -n " "
done
for (( k=1 ; k<i ; k++ ))
do
echo -n "| "
done
echo
done
for (( i=10 ; i>=1 ; i-- ))
do
for (( j=i ; j<=9 ; j++ ))
do
echo -n " "
done
for (( k=1 ; k<=i-1 ; k++ ))
do
echo -n "| "
done
echo " "
done

6|Page

|
||
|||
||||
|||||
||||
|||
||
|

SS 13.Write a shell script that sorts the array of integers in increasing order.
#shell script to sort numbers
echo enter the number of terms to be sorted
read n
echo enter the numbers
for((i=0;i<n;i++))
do
read a[$i]
done
echo sorted list is:
for ((i=$n;i>0;i--))
do
for((j=0;j<i-1;j++))
do
if test ${a[$j]} -gt ${a[`expr $j + 1`]}
then
t=${a[$j]}
a[$j]=${a[`expr $j + 1`]}
a[`expr $j + 1`]=$t
fi
done
done
for((i=0;i<n;i++))
do
echo ${a[$i]}
done

SS 14.Write a shell script that searches for an element in an array.


echo n Enter i:
#i
flag=2
read i
echo enter the elements of array
for((n=0;n<$i;n++))
do
read a[$n]
done
echo enter the number you want to search
read no
for((n=0;n<$i;n++))
7|Page

is no of elements of array

do
if test ${a[$n]} -eq $no
then
flag=1
echo number found at $n position
fi
done
if test $flag -eq 2
then
echo $no not found in array
fi

SS 15.Write a shell script that finds MAX. & MIN. no. in an array.
#shell script that finds the max and min of an array
echo n Enter i:
#i is no of elements of array

read i
echo Enter the elements of array
for((n=0;n<$i;n++))
do
read a[$n]
done
max=${a[0]}
min=${a[0]}
for((n=0;n<$i;n++))
do
if test ${a[$n]} -gt $max
then
max=${a[$n]}
fi
if test ${a[$n]} -lt $min
then
min=${a[$n]}
fi
done
echo maximum of the array is $max
echo minimum of the array is $min

8|Page

SS 16.Write a shell script that prints the elements of array in reverse order.
echo n Enter i:
#i is no of elements of
read i
echo e \033[32mEnter the elements of array\033[0m
for((n=0;n<$i;n++))
do
read a[$n]
done
echo e \033[32mArray in the reverse order is\033[0m
for((n=i-1;n>=0;n--))
do
echo ${a[$n]}
done

array

SS 17.Write a shell script that checks whether a file is regular file, directory, char. specific
file or block file.
echo n Enter the file name:
read file
if test -s $file -a -f $file
then
echo $file is a regular file having size greater than 0
elif test -f $file
then
echo $file is regular file having size zero
fi
if test -d $file
then
echo -e "\033[32m $file is a directory \033[0m "
fi
if test -c $file
then
echo -e "\033[32m $file is a character specific file \033[0m "
fi
if test -b $file
then
echo -e "\033[32m $file is a block specific file \033[0m "
fi

9|Page

SS 18.Write a shell script to check whether a file has read, write, execution permission or not?
#to test whether a file has read, write & execute permission & print accordingly

echo n Enter the file name:


read file
if test -r $file
then
echo $file has read permission on it.
fi
if test -w $file
then
echo $file has write permission on it.
fi
if test -x $file
then
echo $file has execute permission on it.
fi

SS 19.Write a shell script that compares two strings.


#shell script to compare two strings

clear
echo -e "\033[34m enter first string \033[0m"
read string1
echo enter second string
read string2
if test $string1 = $string2
then
echo $string1 and $string2 are similar
else
echo $string1 and $string2 are different
fi

SS 20.Write a shell script that checks whether the two files are identical or different &
displays msg. accordingly.
#shell script that compares two files and prints the difference

echo enter the name of first file


read file1
echo enter the name of second file
read file2
if cmp $file1 $file2
then
10 | P a g e

echo files are identical


else
echo files are different
fi

SS 21.Write a shell script that gets the terminal locked & it can be unlocked only by entering
right password.
pa=smvdu
trap "rm -f $pa" 0 1 2 5
clear
for (( i=0;i>=0;i++ ))
do
echo enter password
stty -echo
read p
if test $p = $pa
then
stty sane
clear
echo -e "\033[32mwelcome to the terminal \033[0m"
break
else
clear
echo -e "\033[31mwrong password \033[0m"
echo
fi
done

SS 22.Write a shell script that monitors disk space & sends appropriate warning.
#shell script that monitors disk space and sends appropriate warning
warn=55
#can be set to the desired level

clear
df -h | cut -c 1-10,33-37
sum=`df -h|grep -vE "%CPU"|cut -c 34-37|awk '{sum +=$1} END
{print sum}'`
echo
echo -e "\033[36mtoatal disk use(%):$sum"
if test $sum -gt $warn
then
echo
echo -e "\033[32mDisk is almost Full!!!!"
11 | P a g e

echo
echo -e "\033[45mBe Ready with a new one\033[0m"
echo
fi

SS 23.Write a shell script that changes the password of user accounts automatically.
while ((1))
do
echo -e "\033[31mEnter the users name \033[0m"
read user
if grep -q $user /etc/passwd
then
echo -e "\033[32m $user's account exists \033[0m"
echo -e "\033[33mChanging Password for $user"
echo passnew|passwd --stdin $user
#passnew will be the
else
echo "\033[32m $user's account doesn't exist \033[0m
fi
done

SS 24.Write a shell script that displays the digital clock on terminals screen.
while((1))
do
clear
x=`date | cut -c 12-19`
tput cup 18 60
echo -e "\033[5m \033[32m$x \033[0m"
sleep 1
done

SS 25.Write a shell script that records the keystrokes.


stty -echo
keystroke=$(head -c1)
stty sane
echo -n "you pressed $keystroke"

12 | P a g e

new password

SS 26.Write a shell script that shows the (%) usage of CPU by different processes & displays
the total CPU usage.
ps aux|cut -c 12-20,66-100
echo
echo
echo -e -n "\033[32mTotal %CPU usage:"
echo -n `ps aux|grep -vE "%CPU"|cut -c 16-20|awk '{sum +=$1}
END {print sum}'`
echo %
echo -e "\033[0m"

SS 27.Write a shell script that displays the status of memory & processes using it.
ps aux|cut -c 12-15,21-25,65-100
echo
echo
echo -e -n "\033[32mTotal %MEMORY usage:"
echo -n `ps aux|grep -vE "%MEM"|cut -c 21-23|awk '{sum +=$1}
END {print sum}'`
echo %
echo -e "\033[0m"

13 | P a g e

S-ar putea să vă placă și