Tuesday, January 12, 2016

write a shell script to check whether given name exists in that file or not.(Assume there is a file “Names” with some strings.)

# search.sh
# Program :         Assume there is a file “Names” with some strings. write a shell
                             script to check whether given name exists in that file or not
# Author :           141740
# Date :              17/03/2015

echo "Enter the name to search"
read name

flg=0
for s in `cat Names`
do
        if [ $s == $name ]
        then
                flg=1
                break
        fi
done

echo
if [ $flg -eq 1 ]
then
        echo "Name is present in the file"
else
        echo "Name is not present in the file"
fi
Output:
 [141740@localhost ~]$ cat Names
chaithra
Supritha
Neenu
Melody

[141740@localhost ~]$ sh search.sh
Enter the name to search
chaithra

Name is present in the file

[141740@localhost ~]$ sh search.sh
Enter the name to search
dsfcx

Name is not present in the file


[141740@localhost ~]$

write a shell script to find maximum and minimum integer in that file or not.(Assume there is a file “Numbers” with some integers)

# minmax.sh
# Program :         Assume there is a file “Numbers” with some integers, write a shell script to find maximum and minimum integer in that file or not. 
# Author :           141740
# Date :              17/03/2015

max=`cut -d ' ' -f1 numbers`
min=`cut -d ' ' -f1 numbers`

for num in `cat numbers`
do
        if [ $num -gt $max ]
        then
                max=$num
        fi
done

for num in `cat numbers`
do
        if [ $num -lt $min ]
        then
                min=$num
        fi
done


echo "The max elements is $max"
echo "The min element is $min"


Output:
[141740@localhost ~]$ cat numbers
10 20 30 40 50
[141740@localhost ~]$ sh minmax.sh
The max elements is 50
The min element is 10

[141740@localhost ~]$












Shell script to display all the files which start from a specific letter? For e.g., if letter = s then all files started with letter s should be displayed from the current directory.

# letter.sh
# Program :         To display all the files which start from a specific letter? For e.g., if letter = s then all files started with letter s should be displayed from the current directory. 
# Author :           sujith
# Date :              17/03/2015

echo "Enter the letter"
read letter

flag=0
len=$(echo "$letter" | wc -c)

if [ $len -eq 2 ]
then
        echo "The names of files starting with $len"
        for str in `ls`
        do
                c=$(echo "$str" | cut -c1)
                if [ $c == $letter ]
                then
                        if [ -f $str ]
                        then
                                flag=1
                                echo $str
                        fi
                fi
        done
        if [ $flag -eq 0 ]
        then
                echo "No files which start with the letter $letter"
        fi
else
        echo "Enter only one letter"
fi


Output:
[141740@localhost ~]$ sh letter.sh
Enter the letter
s
The names of files starting with 2
salary.c
sample
sample.c
samplex.c
search.sh
she116.sh
shel16.sh
shell10.sh
shell11.sh
shell12.sh
shell13.sh
shell1.sh
shell2.sh
shell3.sh
shell4.sh
shell5.sh
shell6.sh
shell7.sh
shell8.sh
shell9.sh
sin.c
sinloop.c
sqaure1.c
sqaure.c
square1.c
square.c
stack.cpp
std.c
strcmp.c
strconcat.c
string.l
strlen.c
strpal.sh
str.sh
structure.cpp
sum.c

[141740@localhost ~]$

[141740@localhost ~]$ sh letter.sh
Enter the letter
z
The names of files starting with 2
No files which start with the letter z


[141740@localhost ~]$

Write a shell script to count number of lines, number of words and number of character in a given file with menu options.

# menu.sh
# Program :         Write a shell script to count number of lines, number of words and number of character in a given file with menu options.
# Author :           sujith
# Date :              17/03/2015

echo "Enter the filename"
read file

echo "menu"
echo "1->lines"
echo "2->words"
echo "3->characters"
echo "4->exit"

echo "Enter the choice"
read choice

case $choice in
        1) set `wc -l $file`
         l=`echo $1`
         echo "no of lines in $file is:$l"
         continue;;
        2) set `wc -w $file`
         w=`echo $l`
         echo "no of words in $file is: $w"
         continue;;
        3) set `wc -c $file`
         c=`echo $l`
         echo "no of characters in $file is : $c"
         continue;;
        4)
        echo "Invalid choice"
        break;
esac


Output:
[141740@localhost ~]$ sh menu.sh
Enter the filename
str.sh
menu
1->lines
2->words
3->characters
4->exit
Enter the choice
1
no of lines in str.sh is:29

[141740@localhost ~]$

[141740@localhost ~]$ sh menu.sh
Enter the filename
str.sh
menu
1->lines
2->words
3->characters
4->exit
Enter the choice
2
no of words in str.sh is:77

[141740@localhost ~]$

[141740@localhost ~]$ sh menu.sh
Enter the filename
str.sh
menu
1->lines
2->words
3->characters
4->exit
Enter the choice
3
no of characters in str.sh is : 357

[141740@localhost ~]$









Shell program to accept a string and display as shown in the following example, suppose str = “RAJESH”

# str.sh
# Program :         To accept a string and display as shown in the following example, suppose str = “RAJESH”
                    





R




R
A



R
A
J


R
A
J
E

R
A
J
E
S
R
A
J
E
S
H

# Author :           sujith
# Date :              17/03/2015

echo "Enter the word to print"
read str

len=$(echo "$str" | wc -c)
a=`expr $len \* 2 - 2`
len=`expr $len - 1`
echo "len=$len"
i=1

while [ $i -le $len ]
do
        b=1
        while [ $b -le $a ]
        do
                echo -n " "
                b=`expr $b + 1`
        done
        a=`expr $a - 2`
        l=1
        while [ $l -le $i ]
        do
                ch=$(echo "$str" | cut -c$l)
                echo -n " $ch"
                l=`expr $l + 1`
        done
        echo " "
        i=`expr $i + 1`
done



Output:
[141740@localhost ~]$ sh str.sh
Enter the word to print
RAJESH
len=6





R




R
A



R
A
J


R
A
J
E

R
A
J
E
S
R
A
J
E
S
H

[141740@localhost ~]$




Shell program to check whether given string is palindrome or not.

# shellpalindrum.sh
# Program :         To check whether given string is palindrome or not.
# Author :           sujith
# Date :              16/03/2015

echo ”To check whether given string is palindrome or not
echo "Enter the string"
read str

len=$(echo "$str" | wc -c)

while [ $len -gt 0 ]
do
ch=$(echo "$str" | cut -c $len)
          echo -n "$ch"
          s1=$s1$ch
          len=`expr $len - 1`
done
echo ""

if [ $s1 != $str ]
then
echo " The string is not palindrome"
else
echo "The string is palindrome"
fi

OUTPUT:
[141740@localhost ~]$ sh shellpalindrum.sh
To check whether given string is palindrome or not
Enter the string
malayalam
malayalam
The string is palindrome
[141740@localhost ~]$

[141740@localhost ~]$ sh shellpalindrum.sh
To check whether given string is palindrome or not
Enter the string
english
hsilgne
 The string is not palindrome
[141740@localhost ~]$


Shell program to convert a decimal number into its binary equivalent.

# deci2bin.sh
# Program :          To convert a decimal number into its binary equivalent.
# Author :           sujith
# Date :              16/03/2015

echo "Enter the num"
read n

val=0
power=1

while [ $n  -ne 0 ]
       do
        r=`expr $n % 2`
        val=`expr $r \* $power + $val`
        power=`expr $power \* 10`
        n=`expr $n \/ 2`
      done

echo "Binary equivalent=$val"



OUTPUT
[141740@localhost ~]$ sh deci2bin.sh
Enter the num
6
Binary equivalent=110
[141740@localhost ~]$ sh shellprime.sh



Shell program to find out whether the given number is prime or not.

# shellprime.sh
# Program :         To find out whether the given number is prime or not.
# Author :             sujith
# Date :              16/03/2015

clear
echo "Enter the number"
read n
flag=0
i=2
if test $n -lt 1
 then
 flag=1
fi

 while test $i -lt $n
        do
        r=$(echo "$n%$i" | bc)

         if test $r -eq 0
         then
           flag=1
         fi
        i=`expr $i + 1`
        done

if test $flag -eq 0
then
echo "It is a prime number"
else
echo "It is not  a prime number"
fi


Output:

[141740@localhost ~]$ sh shellprime.sh
Enter the number
2
It is a prime number
[141740@localhost ~]$

Enter the number
8
It is not  a prime number
[141740@localhost ~]$