Thursday 3 October 2013

How to check whether a file is a shell script file (.sh) or not

   As we know that the first line of a shell script file contains #!bin/bash,
   so we will check whether a file contains the text bin/bash
  
   Arguments of a shell script file is identified as $1,$2,$2....
   $1 is the name of the executing script file itself, and arguments of
   the script files are $2,$3 ....
  
   So lets us write a shell script file isscript.sh
  
   #!bin/bash
  
   if grep "bin/bash" "$1"
   then
    echo "File is a shell script file"
   else
    echo "File is not a shell script file"
   fi
  
   now suppose we want to see whether a file named abc.sh is a
   shell script file or not
  
   so we run the isscript file like this
   $./isscript.sh abc.sh
  
   Now the shell script takes abc.sh as argument $1
   grep "bin/bash" "$1" checks whether the pattern "bin/bash"
   exists in the shell script abc.sh, if exists then command
   echo "File is a shell script file" is executed otherwise
   echo "File is not a shell script file" is executed.
  
  
  

No comments: