?

Log in

No account? Create an account

bashfiletests.sh

« previous entry | next entry »
Nov. 19th, 2005 | 12:53 am
mood: bouncybouncy

#!/bin/bash

file=$1
file2=$2

echo "tests for $file:"
if [ -a $file ]; then echo "-a: file exists."; fi
if [ -b $file ]; then echo "-b: file exists and is a block special file."; fi
if [ -c $file ]; then echo "-c: file exists and is a character special file."; fi
if [ -d $file ]; then echo "-d: file exists and is a directory."; fi
if [ -e $file ]; then echo "-e: file exists."; fi
if [ -f $file ]; then echo "-f: file exists and is a regular file."; fi
if [ -g $file ]; then echo "-g: file exists and is set-group-id."; fi
if [ -h $file ]; then echo "-h: file exists and is a symbolic link."; fi
if [ -k $file ]; then echo "-k: file exists and its sticky bit is set."; fi
if [ -p $file ]; then echo "-p: file exists and is a named pipe (FIFO)."; fi
if [ -r $file ]; then echo "-r: file exists and is readable."; fi
if [ -s $file ]; then echo "-s: file exists and has a size greater than zero."; fi
if [ -t $file ]; then echo "-t: file descriptor is open and refers to a terminal."; fi
if [ -u $file ]; then echo "-u: file exists and its set-user-id bit is set."; fi
if [ -w $file ]; then echo "-w: file exists and is writable."; fi
if [ -x $file ]; then echo "-x: file exists and is executable."; fi
if [ -O $file ]; then echo "-O: file exists and is owned by the effective user id."; fi
if [ -G $file ]; then echo "-G: file exists and is owned by the effective group id."; fi
if [ -L $file ]; then echo "-L: file exists and is a symbolic link."; fi
if [ -S $file ]; then echo "-S: file exists and is a socket."; fi
if [ -N $file ]; then echo "-N: file exists and has been modified since it was last read."; fi

if [ "$file2" == "" ]; then exit; fi

if [ $file -nt $file2 ]; then
  echo "-nt: file1 is newer (according to modification date) than file2, or file1 exists and file2 does not."
fi
if [ $file -ot $file2 ]; then
  echo "-ot: file1 is older than file2, or if file2 exists and file1 does not."
fi
if [ $file -ef $file2 ]; then
  echo "-ef: file1 and file2 refer to the same device and inode numbers."
fi


yeah, I'm sure there's a better way to do this. But it amused me to copy/paste a chunk of the bash manpage and make it into a script.

Link | Leave a comment | Share

Comments {2}

bash

from: anonymous
date: Nov. 19th, 2005 07:01 pm (UTC)
Link

why bash?

Reply | Thread

Triple Entendre

Re: bash

from: triple_entendre
date: Nov. 20th, 2005 01:29 am (UTC)
Link

Why Texas? It's where I live.

Reply | Parent | Thread