Pages

Thursday 8 March 2012

uinx command and shell script


Assignment – 1

v  Locate lines that are longer than 100 and smaller than 150 characters using grep.

è grep -c '\{101,150\}' keval.txt

grep `^.\{100,150\}$` keval.txt

v  Merge and sort the contents of files A and B and display the sorted output on the screen.

è cat file1 file2 | sort


v  Convert all capital letters in a file to small case.

è tr [A-Z] [a-z] < file1

v  Obtain the cont of users logged into the system.

è who | wc –l

v  How will you grant read and execute permissions to the user and group for a file mca.txt?

è chmod ug+rx mca.txt

v  Write a command to display all hidden files without using ls command.

è echo .*
è ls –al

v  Write a command to display all the ordinary files that having no write permission to owner.

è ls –l | grep "^..-.*"
è ls -l | grep -v '^..w'

v  To select line having exactly 10 alphabet
è grep '[a-ZA-z]'/{10/}

v  Write a command to display those lines, which have exactly 4 characters in file.

è grep '^....$' foo

v  Write a command to count number of blank lines in file.

è grep '^$' a | wc –l

v  Write a command to enlist on the screen, only the directories in the current directory.

è ls –ld

v  Print and sort the login names of all users.

è who|cut -d ' '-f1 | sort

v  Copy a file mca.txt present in the current directory to a directory exam in the parent directory.

è cp.|mca.txt../mca

v  How will you remove duplicate records from the file?

è uniq -d k113.txt
è sort k113.txt | uniq

v  Write a command to display five largest files in the current directory.

è ls -S | head -5

v  Select lines from the file that has exactly three character

è grep '^..$' a.lst

v  Count the number of non-blank lines in the file.
è grep -c '^.*$' a.lst

v  Count the number of characters in the last line of a file.

è tail -1 a.lst | wc –c

v  Delete digits from the file.

è tr -d '[0-9]' k113.txt

v  Write tail command that is equivalent to cat /usr/user1/file1

è tail +1 /usr/user/file1

v  Display only login name of all users that are currently logged in.

è who -H | cut -f1 -d " "

v  Display lines 5th to 10th of a file.

è tail +5 foo | head -5

v  Display contents of file X1. (Do not use cat command)

è more X1

v  Display largest file in current working directory.

è ls -x |sort -n |head -1
è ls -lS | grep "^-" | head -1

v  List files which have write permission for the group.

è ls-l|grep '^....w'

v  Display all files which have read and write permission for the group.

è Ls -l | grep '^...rw.'

v  Display all files which have read, write permission for owner and others.

è ls -l | grep '^rw...rw'

v  Count no. of users who are currently logged in (do not make use of wc command).

è Who | uniq -c
è who | grep –c

v  Convert date in a format mm/dd/yy into dd/mm/yy.

è date +%d/%m/%y

v  List ordinary files in your current directory which are not writable (for user only or owner only).

è ls -l | grep -u '^w'



GTU Script
2)          Write shell script
a)          Accept numbers and perform addition, subtraction, division and multiplication.

è a=10
echo "Enter The Value of A: "
read a
b=10
echo "Enter The Value of B: "
read b
c=0
c=`expr $a + $b`
echo "Addition " $c
c=`expr $a - $b`
echo "Subtraction " $c
c=`expr $a / $b`
echo "Division " $c
c=`expr $a \* $b`
echo "Multiplication " $c

b)          Accept the string and checks whether the string is palindrome or not.

è l=0
count=1
tag=0
echo "Enter the String : "
read str
l=`echo $str |wc -c`
l=`expr $l - 1`
lh=`expr $l / 2`
while [ $count -le $lh ]
do
c1=`echo $str|cut -c$count`
c2=`echo $str|cut -c$l`
    if [ $c1 != $c2 ]
    then
        tag=1
    fi
cnt=`expr $count + 1`
l=`expr $l - 1`
done
if [ $tag -eq 0 ]
then
echo "String is Palindrome"
else
echo "String is not Palindrome"
fi

c)          Accept number and check the number is even or odd, finds the length of the number, sum of the digits in the number.

è echo "Enter number:"
read no
c=`expr $no % 2`
if [ $c -eq 0 ]
then
echo "No is even "
else
echo "No is odd"
fi
cnt=`echo $no | wc -c`
cnt=`expr $cnt - 1`
echo "word count =" $cnt
i=1
while [ $i -le $cnt ]
do
j=`echo $no | cut -c $i`
k=`expr $k + $j`
i=`expr $i + 1`
done
echo "total =" $k

d)          Accept strings and replace a string by another string.

è echo "Enter any string:"
read str
echo "Your string is :" $str
echo "Enter word that your what replace with other word"
read oldstr
echo "Enter new string"
read newstr
str1=`echo $str | sed s/$oldstr/$newstr/g`
str2=`echo $str | replace $oldstr $newstr`
echo "after replacing " $str1
echo "after replacing " $str2

e)          Accept filename and displays last modification time if file exists, otherwise display appropriate message.

è echo "Enter file name"
read fnm

if [ ! -f $fnm ]
then
    echo "File  Not Found"
    exit 1
fi
dt=`date -r $fnm`
echo "modification date is:" $dt

f)           Fetch the data from a file and display data into another file in reverse order.

è echo "enter file name for fatching data and store in other file"
read fnm
if [ ! -f $fnm ]
then
    echo "File not found"
    exit 1
fi
echo "enter new file name:"
read newfile
while read -n1 char;
do
    str=$char$str
done<$fnm
echo $str>$newfile
echo $newfile "successfully created on" `pwd` " path"



3)          Write a script to find the global complete path for any file

è echo "enter file name to find:"
read fnm
ans=0;
if [ ! -f $fnm ]
then
        if [ ! -d $fnm ]
        then
       echo "File not found"
                exit 1
        fi
fi
ans=`find /home -name $fnm `
echo $ans

4)          Write a script to broadcast a message to a specified user or a group of users logged on any terminal.

è echo "Enter the User name :"
read uname
echo "Enter String u want to send (End with Control-D) :"
write $uname
echo -e "\nYour message send successfully."

5)          Write a script to copy the file system from two directories to a new directory in such a way that only the latest file is copied in case there are common files in both the directories.

è EXIT=n
while [ $EXIT != y ]
do
                sleep 1
                echo -e "\n"
echo -e "1. Display PWD
    2. Long Listing
    3. Change Directory
         4. Copy Newest File.
                                                          5. Exit
                      Enter Choice: \c"
                      read ch

                     case $ch in

1)
                clear
                pwd
;;

2)    
                clear
                pwd
                ls -l
;;

3)
                echo -n "Enter Absolute Path to change directory: "
                read apath

                cd $apath

if [ $? -eq 0 ]; then            # We can also check for availibility of directory before 'cd' command by 'test -d $apath' i.e. 'if [ -d $apath ]'
                        clear
                                echo "Working Directory Changed successfully to.."
                        sleep 1
                        pwd
                else
                        clear
                                echo "Please check your PATH."
                fi
;;

4)
                clear
echo "Enter filenames to copy. ( * - for ALL Files, ELSE Separate files by spaces )"
                read allfiles
                if [ -f $allfiles ]; then
                                echo "Enter Absolute path, where to copy these files: "
                        read -e cpath
                        if [ -d $cpath ]; then
                        cp -u "$allfiles" $cpath        # -u copies only when the SOURCE file is newer than the destination file or when the destination file is missing
                        else
                        echo "There is no such a directory!"
                        fi
                else
                                echo "There is/are no such file(s)!"
                fi
;;

5)
                clear
                        echo -n "Exiting.."
                        sleep 1
                        echo -n "."   
                        sleep 1
                        echo -n "."
                        clear
                        exit
;;

*)
                clear
                        echo "Invalid Choice"
;;
esac
done

6)          Write a script to compare identically named files in two different directories and if they are same, copy one of them in a third directory.

è echo "Enter 1st file full path(/home/mca/tejas/) :"
read p1
echo "Enter 2nd file full path(/home/mca/tejas/) :"
read p2
echo "Enter new path for copy"
read dest
a=`cmp $p1 $p2`

if [ $? -eq 0 ]
then
                `cp $p2 $dest`
                echo "File successfully copied on path: "$dest
        else
                echo "File not identically same"
        fi
7)          Write a script to delete zero sized files from a given directory (and all its sub-directories).

è for i in `find /home/mca/diz/diz -type f -size 0c`
do
        echo "$i has Zero size"
        echo `rm $i`
done

8)          Write a script to display the name of those files (in the given directory) which are having multiple links

è temp=$(mktemp tmp.XXXXXXXXXX)
while true; do
        read -e -p "Ente the Absolute Path: " path || exit
        [[ -d $path ]] && break
        echo "Invalid Directory!"
done

for i in $path/*
do
                for j in $path/*
                do
                       base1=`basename $i`
               base2=`basename $j`
                        echo "$i"
               echo "$j"
                                if test "$base1" != "$base2"; then
                                        if test "$base1" -ef "$base2"; then
                                                echo "$base1" >> $temp
                                fi
                        fi
                done
done
cat $temp | uniq
rm $temp

9)          Write a script to display the name of all executable files in the given directory.

è echo "Enter a directory name:"
read dirname
if [ -d $dirname ]
then
        echo "$dirname is exist"
        cd $dirname
                for i in `ls`
                do
                                if [ -f $i ]
                                then
                                                e1=$(ls -l $i|cut -c1,1-10 |cut -c4)
                                                e2=$(ls -l $i|cut -c1,1-10 |cut -c7)
                                                e3=$(ls -l $i|cut -c1,1-10 |cut -c10)
                                if [ $e1 = x ]
                                         then
                                        if [ $e2 = x ]
                                        then
                                                if [ $e3 = x ]
                                                then
                                                        echo "$i is executable file"
                                                fi
                                        fi

                                fi
                        fi

                done
        cd
else
        echo "$dirname doesn't exist"
fi

10)      Write a script to display the date, time and a welcome message (like Good Morning etc.). The time should be displayed with “a.m.” or “p.m.” and not in 24 hours notation.

è time=`date +%H%M%S`
d1=`date +%d/%m/%y`
t1=`date +%I:%M:%S`
echo "date:"$d1
if [ $time -ge 000000 -a $time -lt 120000 ]
then
     echo "time:"$t1" a.m"
 else
    
     echo "time:"$t1" p.m"
 fi
    
if [ $time -lt 120000 -a $time -gt 60000 ]
then
    echo "GooD MorninG"
else
    if [ $time -eq 120000 ]
    then
        echo "GooD NooN"
    else
        if [ $time -gt 120000 -a $time -lt 180000 ]
        then
            echo "GooD AfterNooN"
        else
            if [ $time -gt 180000 -a $time -lt 210000 ]
            then
                echo "GooD EveninG"
            else
                echo "GooD NighT"
            fi
        fi
    fi
fi

11)      Write a script to display the directory in the descending order of the size of each file.

è Echo” Enter Directory : ”
read dir
cd $dir
 ls –Sl

12)       
13)      Write a script for generating a mark sheet after reading data from a file. File contains student roll no, name , marks of three subjects.

è tot=0
line=`cat stud.txt | wc -l`
i=1
echo "STD_ID    NAME            SUB1  SUB2  SUB3  TOTAL   PERCENTAGE    GRADE         RESULT"

while [ $i -le $line ]
do
rollno=`head -$i stud.txt | tail +$i |cut -f1 -d","`
       name=`head -$i stud.txt | tail +$i |cut -f2 -d","`
       sub1=`head -$i stud.txt | tail +$i |cut -f3 -d","`
       sub2=`head -$i stud.txt | tail +$i |cut -f4 -d","`
       sub3=`head -$i stud.txt | tail +$i |cut -f5 -d","`
       tot=`expr $sub1 \+ $sub2 \+ $sub3`
       per=`expr $tot \/ 3`

       if [ $sub1 -ge 35 -a $sub2 -ge 35 -a $sub3 -ge 35 ]
       then
              result="PASS"
                if [ $per -ge 70 ]
                then
                        grade="DISTINCTION"
                elif [ $per -ge 60 ]
                then
                        grade="FIRST CLASS"
                elif [ $per -ge 50 ]
                then
                        grade="SECOND CLASS"
                else
                        grade="PASS CLASS"
                fi
        else
                result="FAIL"
                grade="     ---    "
        fi
        i=`expr $i + 1`

echo "$rollno     $name   $sub1  $sub2     $sub3    $tot      $per      
       $grade        $result"
Done



14)      Write a script to make following file and directory management operations menu based:
Display current directory
List directory
Make directory
Change directory
Copy a file
Rename a file
Delete a file
Edit a file

è echo " 1. Display current Directory"
echo " 2. List Directory"
echo " 3. Make directory "
echo " 4. Change directory "
echo " 5. Copy a file "
echo " 6. Rename a fIle "
echo " 7. Delete a file "
echo " 8. Edit a file "

echo " Enter Your Choice: "
read ch
case $ch in
1)
pwd
       ;;
2)
ls
       ;;
3)
echo “Enter directory name to make : ”
     read dir_name
      mkdir $dir_name
      ;;
4)
cd
      ;;
5)
echo “Enter two file name to copy:”
      read f_name1 f_name2
cp $f_name1 $f_name2
      ;;
6)
echo “Enter file name to rename:”
      read fname
      mv $fname
      ;;
7)
echo “Enter file name to delete : ”
      read fname
rm $fname
;;
8)
echo “Enter file name to edit : ”
read fname
      vi $fname
      ;;
*)
echo “Plz, Enter valid choice…”
;;
esac 

15)      Write a script which reads a text file and output the following
Count of character, words and lines.
File in reverse.
Frequency of particular word in the file.
Lower case letter in place of upper case letter.

è echo "Enter File Name"
read fname
echo "
     1...Count character,words and lines
     2...Reverse File
     3...Found frequency of word
     4...Convert upper to lower
     5...Convert lower to upper"
echo "Enter Choice: "
read ch
case $ch in
1)
echo “Total Character:”
       wc -c $fname
       echo “Total Words:”
       wc -w $fname
       echo “Total Lines:”
       wc -l $fname
       ;;
2)
rev $fname
       ;;
3)
echo “Enter word to find: ”
       read w
       echo “Frequency:”
       grep  -c "$w" $fname
       ;;
4)
       echo “Enter Text:”
       read text
       echo $text | tr "[A-Z]" "[a-z]"
       ;;
5)
       echo “Enter Text:”
       read text
       echo $text | tr "[a-z]" "[A-Z]"
       ;;
*)
       echo “Plz, Enter valic choice”
       ;;
Esac

16)      Write a shell script to check whether the named user is currently logged in or not.

è echo "Enter The User name :\c "
read name
>>temp
echo "The user \c " $name
(who | grep $name >> temp ) && echo " Is currently logged in " || echo "is not logged in "

17)      Write a Script for Simple Database Management System Operation.
Database File Contains Following Fields.
EMP_NO
EMP_NAME
EMP_ADDRESS
EMP_AGE
EMP_GENDER
EMP_DESIGNATION
EMP_BASIC_SALARY
Provide Menu Driven Facility For
VIEW RECORD BASED ON QUERY
ADD RECORD
DELETE RECORD
MODIFY RECORD.
COUNT TOTAL NUMBER OF RECORDS
EXIT

è ch=1
while [ $ch -ne 0 ]
do
clear
echo "1. View record based on query"
      echo "2. Add record"
      echo "3. Delete record"
      echo "4. Edit record"
      echo "5. Count total records"
      echo "0. Exit"
      read ch

      case $ch in
              1)
                      echo "Enter format to display(Separate Fields With Space) : ";
                      read format
                      field="";
                      for f in $format
                      do
                              case $f in
                                      "NO")
                                              field="$field,1";;
                                      "NAME")
                                              field="$field,2";;
                                      "ADDRESS")
                                              field="$field,3";;
                                      "AGE")
                                              field="$field,4";;
                                      "GENDER")
                                              field="$field,5";;
                                      "DESIG")
                                              field="$field,6";;
                                      "BASIC_SALARY")
                                              field="$field,7";;
                              esac
                      done
                      field="echo $field|cut -c2-"
                      echo "------------------------------------------------------------------"
                      cut -d'|' -f`echo $field` dbEMP.dat
                      echo "------------------------------------------------------------------"
                      read
                      ;;
              2)
                      echo "Enter Employee Information : "
                      echo "Emp No. : \c";            read EMP_NO
                      echo "Emp Name : \c";                read EMP_NAME
                      echo "Address : \c";             read EMP_ADDRESS
                      echo "Age : \c";          read EMP_AGE
                      echo "Gender : \c";             read EMP_GENDER
                      echo "Designation : \c";        read EMP_DESIGNATION
                      echo "Basic Salary : \c";       read EMP_BASIC_SALARY
                      echo "$EMP_NO|$EMP_NAME|$EMP_ADDRESS|$EMP_AGE|$EMP_GENDER|$EMP_DESIGNATION|$EMP_BASIC_SALARY" >> dbEMP.dat
                      ;;
              3)
                      echo "Enter Emp No. For Delete : \c";            read EMP_NO
                      del=`grep "^$EMP_NO" dbEMP.dat | wc -l`
                      grep -v "^$EMP_NO" dbEMP.dat > tmpEMP.dat
                      cat tmpEMP.dat > dbEMP.dat
                      rm tmpEMP.dat
                      echo "$del records Deleted"
                      sleep 3
                      ;;
              4)
                      echo "Enter emp no. for modify : \c";            
                      read MOD_EMP_NO
                      mod=`grep "^$MOD_EMP_NO" dbEMP.dat | wc -l`
                      if [ $mod -eq 0 ]
                      then
                              echo "Record not found!!!"
                              sleep 3;
                      else
                              echo "Enter new emp information : "
                              echo "Emp No. : \c";            read EMP_NO
                              echo "Emp Name : \c";                read EMP_NAME
                              echo "Address : \c";             read EMP_ADDRESS
                              echo "Age : \c";          read EMP_AGE
                              echo "Gender : \c";             read EMP_GENDER
                              echo "Designation : \c";        read EMP_DESIGNATION
                              echo "Basic Salary : \c";       read EMP_BASIC_SALARY

                              terminal=`tty`
                              exec < dbEMP.dat                              
                              while read rec
                              do
                                      echo $rec | grep "^$MOD_EMP_NO" >/dev/null
                                      if [ $? -ne 0 ]
                                      then
                                              echo $rec >> tmpEMP.dat
                                      else
                                              echo "$EMP_NO|$EMP_NAME|$EMP_ADDRESS|$EMP_AGE|$EMP_GENDER|$EMP_DESIGNATION|$EMP_BASIC_SALARY" >> tmpEMP.dat
                                      fi
                              done
                              cat tmpEMP.dat > dbEMP.dat
                              rm tmpEMP.dat
                              echo "$mod Record(s) Modifieded..."
                              sleep 3;
                      fi
                      ;;
              5)
                      echo "`grep . dbEMP.dat | wc -l` Records Found..."
                      sleep 3
                      ;;
              0)
                      ;;
      esac
done

18)      Write A Script To Perform Following String Operations Using Menu:
COMPARE TWO STRINGS.
JOIN TWO STRINGS.
FIND THE LENGTH OF A GIVEN STRING.
OCCURRENCE OF CHARACTER AND WORDS
REVERSE THE STRING.

è while true
do
echo "1 Compare two string"
echo "2 Join two string"
echo "3 Find length of given string"
echo "4 Occurrence of char. & words"
echo "5 Reverse the string"
echo "Enter your choice :"
read ch


case $ch in

1)
        echo "Enter String1:"
        read str1
        echo "Enter String2:"
        read str2

        if [ $str1 = $str2 ]
        then
                echo "String is equal"
        else
                echo "String is not equal"
        fi
        ;;
2)
        echo "Enter String1:"
        read str1
        echo "Enter String2:"
        read str2

        str3=$str1$str2
        echo "Join String: $str3"
        ;;
3)
        length=0
        echo "Enter String1:"
        read str1
        length=`expr $str1 | wc -c`
        length=`expr $length - 1`
        echo "Length: $length"
        ;;
4)
        echo "Enter String:"
        read str

        echo "Enter Word to find"
        read word

        echo $str | cat > str1.txt

        grep -o $word str1.txt | cat > str2.txt
        count=`grep -c $word str2.txt`
     
       echo "Count:"$count
      ;;
5)
        echo "Enter String1:"
        read str

        len=`expr $str | wc -c`
        len=`expr $len - 1`
        while [ $len -gt 0 ]
        do
                rev=`expr $str | cut -c $len`
                ans=$ans$rev
                len=`expr $len - 1`
        done
        echo "Reverse String:" $ans
        ;;

*)
        echo "Please enter correct choice"
        ;;
esac   
    echo "Do u want to continue?[y/n]"
    read ans
    if [ $ans = n ]
    then
        exit
    fi
done

19)      Write a script to calculate gross salary for any number of employees
Gross Salary =Basic + HRA + DA.
HRA=10% and DA= 15%.

è ch=1
while [ $ch -eq 1 ]
do
echo "Enter Basic Salary : "
       read Basic

       HRA=`echo "$Basic * 10 / 100"|bc`
       DA=`echo "$Basic * 15 / 100"|bc`
       Gross=`expr $Basic + $HRA + $DA`
       if [ $Gross -le 10000 ]; then
               PT=150
       elif [ $Gross -le 20000 ]; then
               PT=200
       elif [ $Gross -le 30000 ]; then
               PT=400
       else
               PT=550
       fi
       echo "Gross Salary : $Gross"
       echo "Professional Tax : $PT\n"
       echo "Enter 1 For Calculate More..."; read ch
done

20)      Write a script to check whether a given string is palindrome or not.

è echo "Enter String : "
read str
length=0
cnt=1
flag=0
length=`expr $str | wc -c`
length=`expr $length - 1`
temp=`expr $length / 2`
while test $cnt -le $temp
do
rev=`expr $str | cut -c $cnt`
       ans=`expr $str | cut -c $length`

       if [ $rev != $ans ]
       then
               flag=1
       fi

       cnt=`expr $cnt + 1`
       length=`expr $length - 1`
done

if [ $flag -eq 0 ]
then
 echo "String is palindrome"
else
 echo "String is not palindrome"
fi

21)      Write a script to check whether a given number is palindrome or not.

è clear
echo "Enter number :"
read number
rem=0
i=10
revnumber=0
for((;number>0;))
do
        let revnumber=" revnumber * 10 "
        let rem=" number % 10 "
        let number=" number / 10 "
        let revnumber=" revnumber + rem "
done
echo -e "\nReverse number :-\c"$revnumber

22)      Write a script to display all words of a file in ascending order.

è echo  -e "\n Enter file name : "
read fn
for i in `cat $fn`
do
echo $i >> f2.txt
done
sort f2.txt
rm f2.txt

23)      Write a script to display all lines of a file in ascending order.

è echo " Enter File Name"
read fname
sort $fname

24)      Write a script to display the last modified file.

è while true;
do
read -e -p "Enter Directory :" path || exit
      [[ -d $path ]] && break

echo "Invalid path, Try Again!"
done
cd $path
ls * -dpltr | grep -v '/$' | tail -n1
cd $OLDPWD

25)      Write a shell script to add the statement #include <stdio.h> at the beginning of every C source file in current directory containing printf and fprintf.

è while true;
do
read -e -p "Enter Directory: " path || exit
[[ -d $path ]] && break

 echo "Invalid path! Try Again!"
done
path=${path%/}
myargs=`grep -l -e "printf" -e "fprintf" $path/*.c | xargs`
if [ $? -gt 1 ]; then       
echo -n "No Matches were found. "  && exit
fi
temp=$(mktemp tmp.XXXXXXXXX)
for i in $myargs       
do
 echo "Do you want to add '#include <stdio.h>' to $i?"
 read S
case $S in
Y|y|YES|Yes|yes|yeah)
sed '1i\
#include<stdio.h>' "$i" > "$temp"      
mv "$temp" "$i"           
;;
 n|N|NO|no|No|nope)
 echo "Alright! Next.."
 shift
;;
 *)   
echo "Invalid input."
 ;;
Esac
Done
if [ -z $myargs ]; then
echo "No Matches were found. Try another Directory"
else
clear
head -n5 $path/*.c | less
fi
rm $temp

26)      Write a script that behaves both in interactive and non-interactive mode. When no arguments are supplied, it picks up each C program from current directory and lists the first 10 lines. It then prompts for deletion of the file. If the user supplies arguments with the script, then it works on those files only.

è N=$#
ext=c
if test "$N" -eq "0"; then
while true;
do   
read -e -p "Enter Path: " path || exit
        [[ -d $path ]] && break
echo "Invalid Path, Try Again!"
done
path=${path%/}
for i in $path/*.C
do
if [ "$i" != $path/'*.C' ];  then
mv "$i" "${i/.C/}".c  
clear
fi
done
for i in $path/*."$ext"
do
if [ "$i" != "$path"/'*.c' ];
then
clear
echo "File is $i"
              head -n10 "$i" | nl  
              sleep 1        
              rm -i "$i"      
else
echo "There are no matching \"C\" files to Prompt in this directory."
              sleep 2
              clear=no
       fi
done
if test "$clear" != "no";
then 
clear
echo "Remaining C files in the Directory..."
       ls -1 $path/*.c
fi
   
else
for i in $path/*.C
do
if [ "$i" != $path/'*.C' ];
then
mv "$i" "${i/.C/}".c   
fi
done
for i in $*           
do   
clear
i="${i/.c/}"       
i="$(pwd)/$i.c"           
if [ -f "$i" ];
then   
echo "File name is $i"
head -n10 "$i" | nl   
sleep 1
rm -i "$i"
else           
echo "There is no such a file with name: \"$i\" in current working directoy"
sleep 3
fi
done
clear
echo "Remaining C files in the Directory..."
ls -1 *.c
sleep 1
fi

Class Script

v  write a shell script which receives any year from the keyboard and determines that year is leap year or not.
 
è echo "Enter year : "
read year
echo $year|grep "^[0-9]*$" >/dev/null
flg=0
if [ $? -eq 0 ]; then
                    if [ `echo "$year % 4" | bc` -eq 0 -a `echo "$year % 100" | bc` -eq 0 ]; then
                       echo "$year is leap year"
                               flg=1
                    fi
          fi
          if [ $flg -eq 0 ]; then
                    echo "$year is not a leap year"
          fi
 
v  Shell script which receives any no of file name as arguments, user should check whether it is a file or dir. If it is a file then  file name as well as the no of lines ,no of words and chars should be disp.(without WC command).
 
è for f in $*
do
if [ -f $f ]; then
               tmp=`tty`
               exec < $f
               l=0
                c=0
               ch=0
               w=0
               while read line
               do
                           l=`expr $l + 1`
                           c=`expr length "$line"`
                           ch=`expr $c + $ch`
                           echo $c $ch
                           for word in $line
                           do
                                       w=`expr $w + 1`
                           done
               done
               exec < $tmp
               echo "$f IS FILE WITH : "
               echo "   Lines : $l"
               echo "   Characters : $ch"
               echo "   Words : $w"
else
               echo "$f IS DIRECTORY. "
fi
done
 
v  Scripts that behaves both in interactive and non-interactive node. When no of arguments are supplied it picks up C program from current directory. Enlist 1st 10 lines. then prompts deletion of the  file. if the user supplies argument. With the scripts then it works on those file only.
 
è N=$#
ext=c
if test "$N" -eq "0"; then
            while true; 
                       do
                                 read -e -p "Enter Path: " path || exit
                                         [[ -d $path ]] && break         
                                        echo "Invalid Path, Try Again!"
                               done
                    path=${path%/} 
                    for i in $path/*.C
                    do
                                if [ "$i" != $path/'*.C' ]; then
                                         mv "$i" "${i/.C/}".c
                                         clear 
                               fi
                    done
                    for i in $path/*."$ext"
                               do
                                         if [ "$i" != "$path"/'*.c' ]; then
                                                   clear
                                                   echo "File is $i"
                                                   head -n10 "$i" | nl
                                                   sleep 1
                                                   rm -i "$i"
                                         else
                                                   echo "There are no matching \"C\" files to Prompt in this directory."
                                                    sleep 2
                                         fi
                               done
 
                    if test "$clear" != "no"; then
                               clear
                               echo "Remaining C files in the Directory..."
                              ls -1 $path/*.c            
                    fi
          else
                    for i in $path/*.C
                               do
                                         if [ "$i" != $path/'*.C' ]; then
                                                   mv "$i" "${i/.C/}".c
                                                   clear 
                                         fi
                               done
                    for i in $*
                    do    
                               clear
                              i="${i/.c/}"        
                               i="$(pwd)/$i.c"        
                               if [ -f "$i" ]; then    
                               echo "File name is $i"
                               head -n10 "$i" | nl    
                               sleep 1
                               rm -i "$i"
                    else            # Error for non-Existent files.
                    echo "There is no such a file with name: \"$i\" in current working directoy"
                    sleep 3
          fi
          done
          clear
          echo "Remaining C files in the Directory..."
          ls -1 *.c
          sleep 1
fi
 
 
 
v  Scripts that delete all leading and trailing spaces in all lines in a file.  Also remove blank lines from a file.
 
è Clear
echo "Enter file name:"
read f1
if test -f $f1
then
            terminal=`tty`
            exec<$f1
            while read line
            do
                       if  test "$line"
                       then
                                 echo $line
                       fi
            done
fi
 
 
v  Write scripts to display all the words of a file as well as line in descending order.
 
è Clear
echo " Enter File Name"
read fname
sort $fname
 
  OR
 
è echo  -e "\n enter File name "
read fn
for i in `cat $fn`
do
            echo $i >> f2.txt
done
sort f2.txt
rm f2.txt

 

No comments:

Post a Comment