Simple shell programs - Code

Latest

programs

Thursday, October 6, 2011

Simple shell programs


(a) Write a shell script that displays a list of all files in the current directory to which the user has read write and execute permissions
(b) Develop an interactive script that asks for a word and file name and then tells how many times that word occurred in the file.
(c) Write a shell script to perform the following string operations.
1) To extract a sub string from a given string
2) To find the length of a given string

(a) PROGRAM

# File Name : list.sh

#!/bin/bash
read -p "Enter a directory name : " dn
if [ -d $dn ]; then
printf "\nFiles in the directory $dn are :\n"
 for fn in `ls $dn`
 do
  if [ -d $dn/$fn ];  then
      printf "<$fn> Directory "
   elif [ -f $dn/$fn ]
     then
      printf "$fn File "
     fi
 if [ -r $dn/$fn ];  then
   printf " Read"
 fi
 
 if [ -w $dn/$fn ];then
    printf " Write"
  fi
  
 if [ -x $dn/$fn ];then
    printf " Execute"
  fi
  printf "\n"
 done
else
 printf "\n$dn not exists or not a directory"
fi







(b) PROGRAM

# File Name : wcount.sh
#!/bin/bash
read -p "Enter a file name : " fn
if test -f $fn
then
 echo "The contents of the file $fn is :"
 cat $fn
  echo "No. of Line          : `wc -l $fn`"
  echo "No. of Words      : `wc -w $fn`"
  echo "No. of Characters: `wc -c $fn`"
else
 echo "$fn is not exists or not a file"
fi

(c) PROGRAM

Print “Enter the String:\c”
read  strIn
strlen=${# strIn}
print the string length is : $strlen
$ strlen.scr

O/P:
            Enter the String: Now is the time
            The String length : 15


No comments:

Post a Comment