Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



The Korn Shell: Linux and Unix Shell Programming Manual (3rd Edition)    (ISBN: 0201675234)


 

 List Price: $44.95
 Our Price: $44.95
 Used Price: $20.14

 Release Date: 05 January, 2001
 Manufacturer: Addison Wesley Professional (Paperback)
 Sales Rank: 120,706

 Author: Anatole Olczak









More Info

 File Testing 2001-11-23 02:09:44
  sh 
Category: source:sh:files
Description: An example of file testing with bash
Platform: unix
Author: detour
Viewed: 4234
Rating: 3.3/5 (12 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
filetest.sh
#!/bin/bash
#
# File stats written by detour@metalshell.com
# 
# Example of file testing with bash
#
# -O $file	true if owned by EUID
# -G $file	true if owned by EGID
# -e $file	exists
# -z $file	zero file
# -n $file	nonzero file
# -r $file	readable	 
# -w $file	writeable
# -x $file	executable
# -f $file	plain file
# -d $file	directory
# -L $file	symbolic link
# -p $file	named pipe or FIFO
# -S $file	socket
# -b $file	block special file
# -c $file	character special file
# -u $file	setuid
# -g $file	setgid
# -k $file	sticky
# -t $file	true if opened to a tty
#
# http://www.metalshell.com/
#

echo -n "Enter the filename to test: "
read

if [ -e $REPLY ]; then
  echo "## FILE STATUS OF $REPLY ##"
  if [ -z $REPLY ]; then
        echo "# File is empty."
  fi
  if [ -n $REPLY ]; then
        echo "# File is not empty."
  fi
  if [ -r $REPLY ]; then
        echo "# You have permission to read this file."
  fi
  if [ -w $REPLY ]; then
        echo "# You have permission to write to this file."
  fi
  if [ -x $REPLY ]; then
        echo "# You have permission to execute this file."
  fi
  if [ -f $REPLY ]; then
        echo "# This is a plain file."
  fi
  if [ -d $REPLY ]; then
        echo "# $REPLY is a directory"
  fi
else
  echo "File not found. Exiting."
  exit 1
fi

exit 0
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?