Friday 30 March 2018

Shell script to display file contents from middle

#Display file contents between a range
echo "Enter file name"
read fname
echo "Enter start and end line"
read m n

if [ -f $fname ]
then
      tail -n +$m $fname>tfile
      range=`expr $n - $m`
      echo $range
      head -$range tfile

else
      echo "'$fname' does not exist"
fi



OUTPUT
csc@csc-SVE1513CYNB ~ $ sh dispmid.sh
Enter file name
file2
Enter start and end line
2 5
3
102 Babu CEO
103 Banu CIO
101 Anu MD

Shell script to check whether file is executable, readable and writeable

#Check access permission of the given file
echo "Enter file name: \c"
read file
if [ -e $file ]
then
    echo "'$file' is"
    if [ -x $file ]
    then
       echo "   executable"
    fi
    if [ -r $file ]
    then
       echo "    readable"
    fi
    if [ -w $file ]
    then
       echo "    writeable"
    fi
    if [ -r $file -a -w $file ]
    then
       echo "     both readable and writeable"
    fi
else
    echo "'$file' does not exist"
fi




OUTPUT
csc@csc-SVE1513CYNB ~ $ sh ftesh.sh
Enter file name: a.out
'a.out' is
   executable
    readable
    writeable
     both readable and writeable

Shell script to delete files of zero size

#Delete files of size zero in the current directory
for i in *
do
  if [ -f $i -a ! -s $i ]
  then
     
          echo "File $i size is zero"
          rm -i $i
    
   fi
done



OUTPUT
csc@csc-SVE1513CYNB ~ $ sh rmzerofile.sh
File 'hello' size is zero
rm: remove regular empty file ‘hello’? y
File 'well' size is zero
rm: remove regular empty file ‘well’? y


Thursday 29 March 2018

Shell script to display files of size greater than the specified size

#Display files of size greater than the specified size
echo "Enter size"
read size
for i in *
do
    if [ -f $i ]
    then
          s=`ls -l $i|tr -s ' '|cut -d ' ' -f5`

          if [ $s -gt $size ]
          then
              echo $i
          fi
    fi
done


OUTPUT
csc@csc-SVE1513CYNB ~ $ sh fsize.sh
Enter size
286
Firefox_wallpaper.png
a.out
archive.tar
arrsort.sh
drdl.sh
grp.c
logcnt.sh
occur.sh
oddeven02.sh
palin.sh
present.sh

Shell script to display the number of users logged-in

#Display the number of logged in users and the first logged in user info
count=`who|cut -d ' ' -f1|sort -u|wc -l`
echo "Number of users logged in: $count"

echo "First login user details"
echo "------------------------"
fu=`who|head -n 1|cut -d ' ' -f1`
echo "User name = $fu"
echo "Logged in Terminal = `who|head -n 1|tr -s ' '|cut -d " " -f2`"
echo "Log in date = `who|head -n 1|tr -s ' '|cut -d ' ' -f3`"
echo "Log in time = `who|head -n 1|tr -s ' '|cut -d ' ' -f4`"


OUTPUT
csc@csc-SVE1513CYNB ~ $ sh logcnt.sh
Number of users logged in: 1
First login user details
-------------------------------------
User name = csc
Logged in Terminal = tty7
Log in date = 2018-03-28
Log in time = 20:11

Shell script to check whether the given input is a file or directory

#Shell script to check whether the given input is a file or directory
for i in $*
do
    if  test -d $i
    then         
        echo "$i is a directory file"
    elif test -f $i
    then
        echo "$i is an ordinary file"
    else
       echo "$i does not exist"
    fi
done



OUTPUT
csc@csc-SVE1513CYNB ~ $ sh fcheck.sh sxc well sasha
sxc does not exist
well is an ordinary file
sasha is a directory file

Shell script to display filenames in uppercase

#shell script to display filename to uppercase
if  [ $# -eq 0 ]
then
       echo “Pass an argument”
else
    for file in $*
    do
           if test -f $file
           then
                 echo  $file|tr ‘[a-z]’ ‘[A-Z]’
           else
                 echo “File $file does not exist”
           fi
    done
fi










OUTPUT
csc@csc-SVE1513CYNB ~ $ sh upper.sh well simple.c shan
WELL
SIMPLE.C
File shan does not exist

Shell script to add the numbers divisible by 3 in the range 50 to 100

#Shell script to add the numbers divisible by 3 in the range 50 to 100
s=0
for((i=50;i<=100;i++))
do
if [ `expr $i % 3` = 0 ]
then
     echo "$i "
     s=`expr $s + $i`
fi
done
echo  "Sum of the numbers divisible by 3 between 50 to 100 is $s"





OUTPUT
csc@csc-SVE1513CYNB ~ $ bash sumdiv.sh
51
54
57
60
63
66
69
72
75
78
81
84
87
90
93
96
99
Sum of the numbers divisible by 3 between 50 to 100 is 1275
 

Listing files having read, write and access permissions

#shell script to display files having rwx permission

echo "Files with Read, Write and Execute Permissions"
for file in *
do
if [ -f $file ]
then
   if [ -r $file -a -w $file -a -x $file ]
   then
       echo $file
   fi
fi
done



OUTPUT

csc@csc-SVE1513CYNB ~ $ sh access.sh
Files with Read, Write and Execute Permissions
a.out

Shell scrit to check whether the two file contents are same


#Shell script to compare the contents of two files
echo "Enter file 1"
read file1
echo "Enter file 1"
read file2
val=`cmp $file1 $file2`
if [ -z "$val" ]
then
   echo "Files have same content"
   rm $file1
   echo "$file1 removed"
else
   echo "File contents are different"
fi



OUTPUT
csc@csc-SVE1513CYNB ~ $ sh same.sh
Enter file 1
file
Enter file 1
file
Files have same content
file removed

csc@csc-SVE1513CYNB ~ $ sh same.sh
Enter file 1
file2
Enter file 1
file3
File contents are different

Wednesday 28 March 2018

Shell script to search for a number in a list of positional parameters

#shell script to check whether the given number is present
n=$#
i=0
echo "Enter the number to check: \c"
read num
while [ $n -gt 0 ]
do
     i=`expr $i + 1`
     if [ $1 -eq $num ]
     then
           pos=$i
           break;
    fi
    shift
    n=`expr $n - 1`
done
if [ $n -gt 0 ]
then
     echo "Number $num is present in the list at position $pos"
else
     echo "Number not in the list"
fi


OUTPUT
csc@csc-SVE1513CYNB ~ $ sh present.sh 1 2 3 4 5
Enter the number to check: 5
Number 5 is present in the list at position 5

csc@csc-SVE1513CYNB ~ $ sh present.sh 1 2 3 4 5
Enter the number to check: 6
Number not in the list

Shell script to display the last modification time of a file

#shell script to display the  last modification time of a file
echo  "Enter name of the file:"
read n
if  [ -e $n ]
then
        echo 'Last modification time is' `ls -l $n | cut -d" " -f 6,7,8`
else
        echo "file does not exist"
fi


OUTPUT
csc@csc-SVE1513CYNB ~ $ sh mtime.sh
Enter name of the file:
oddeven02.sh
Last modification time is Mar 28 15:21

Shell script to find the sum of odd and even numbers in an array separately


#shell script to sum the odd and even numbers in an array
echo "Enter the array elements "
read -a array
o=0
e=0
for((i=0;i<${#array[@]};i++))
do
    n=`expr ${array[$i]} % 2 `
    if [ $n -eq 0 ];then
          e=`expr $e + ${array[$i]}`
    else
          o=`expr $o + ${array[$i]}`
    fi
done
echo "The sum of even numbers in the given array.. $e"
echo "The sum of odd numbers in the given array..   $o"


OUTPUT
csc@csc-SVE1513CYNB ~ $ bash oddeven02.sh
Enter the array elements
1 2 3 4 5 6 7 8 9
The sum of even numbers in the given array.. 20
The sum of odd numbers in the given array.. 25

Shell script to count the number of occurrences of a given digit in a number



#Shell script to count the number of occurrences of a given digit in a number
count=0
echo "enter the number"
read n
num=$n
echo "Enter the number to be checked"
read f
while [ $n -gt 0 ]
do
    m=`expr $n % 10 `
    if [ $m -eq $f ];then
           count=`expr $count + 1`
    fi
    n=`expr $n / 10 `
done
echo "The number $f has occurred $count times in the number $num"


OUTPUT
csc@csc-SVE1513CYNB ~ $ sh occur.sh
enter the number
12131425 
Enter the number to be checked
1
The number 1 has occurred 3 times in the number 12131425

Friday 16 March 2018

Extract substring from a given string until the specified character

This program reads a string and a character and displays part of the string up-to the specified character.
Example
Given string
welcome
Given character
c
OUTPUT
wel

//Program to extract a sub string up-to a specified character

#include<stdio.h>
#include<stdlib.h>

int main()
{
      int i=0;
      char s[100],c;

      printf("enter string\n");
      scanf("%s",s);
      printf("enter character ");
      scanf(" %c",&c);
     while (s[i]!='\0')
    {
           if (s[i]==c)
                   break;
   
           printf("%c",s[i]);
              i++;
      }

    return 0;
}






OUTPUT
Enter string
good_morning
Enter character_
good

Note: After reading a string, the following scanf() to read a character will not wait for input, since the newline(\n) character at the end of the string is taken as an input to that character.
Hence,write the scanf() function to read the character as scanf(" %c",&c); 
Note there is blank space before % symbol. 

Wednesday 14 March 2018

Counting common characters in two strings

This C program reads two strings s1 and s2 and outputs the number of characters that are common in both strings.
For example, if the input strings are
hello
welcome

output will be
3

Explanation
The common characters are e, l, o
Also a boundary condition is included which requires the length of the two strings must be between 3 and 100.

PROGRAM

#include<stdio.h>
#include <stdlib.h>

int main()
{
  char s1[100],s2[100],d[100];
  int i=0,j=0,n=0,m=0,count=0,k;

  printf("Enter two strings\n");
  scanf("%s%s",s1,s2);
  while(s1[i++] != '\0')
    n=i;
  while(s2[j++] != '\0')
    m=j;
    j=0;
  if ((n >= 3 && n<100) && (m >=3 && m < 100))
  {
          for(i=0;i<n;i++)

         {
              for(k=0;k<=j;k++)

             {
                  if(s1[i] == d[k])
                      break;
              }
              if(k>j)
                      d[j++]=s1[i];
         }
          d[j]='\0';

    for(i=0;i<=j-1;i++)
    {
          for(k=0;k<m;k++)
          {
             if(d[i] == s2[k])
             {
                 count++;

                 break;
             }
        }
    }   
 
  printf("\n%d",count);
  }

return 0;    
}

OUTPUT
Enter two strings
india
china
3

Monday 12 March 2018

C program to rearrange the unique characters in a string in descending order

The following program reads a string as an input, displays the unique letters in the string in descending order.
For example, if the input is
innovation
the output of the program will be
vtonia

//Program to sort unique characters in a string

# include <stdio.h>

int main()
{
    char s[100],d[100],temp;
    int i=0,n,j=0,k;
    printf("Enter string\n");
    scanf("%s",s);
    while (s[i++]!='\0');
    n=i;
    if (n>=3 && n<100)
   {
       for(i=0;i<n;i++)
      {
          for(k=0;k<=j;k++)
         {
             if (s[i]==d[k])
             break;
         }
        if (k>j)
           d[j++]=s[i];
      }
       d[j]='\0';
  

      for(i=0;i<j-1;i++)
      for(k=i+1;k<j;k++)
       {
            if (d[i]<d[k])
            {
                temp=d[i];
                d[i]=d[k];
                d[k]=temp;
            }
       }
       printf("String after sorting\n");
       printf("-----------------------\n");
       printf("%s",d);
  }  
    return 0;
}


OUTPUT
Enter string                                                                                                                         
innovation   
                                                                                                                        
String after sorting                                                                                                                 
-----------------------                                                                                                              
vtonia