Checking File Attributes using Shell Script


Here’s shell script to check file attributes

#!/bin/bash
read f1
read f2
[ -d $f1 ] && op=”$f1 is a directory” || op=”$f1 is not a directory”
echo “$op”
[ -h $f1 ] && op=”$f1 is symbolic link” || op=”$f1 is not a symbolic link”

 echo “$op”
[ -s $f1 ] && op=”$f1 has some content” || op=”$f1 is empty ”
echo “$op”
[ -w $f1 ] && op=”$f1: You have write permition” || op=”$f1 You don’t have write permition”
echo “$op”
[ -r $f1 ] && op=”$f1 You have read permition” || op=”$f1 You don’t have read permition”
echo “$op”
[ -x $f1 ] && op=”$f1 You have execute permition” || op=”$f1 You don’t have execute write permition”
echo “$op”

[ -N $f1 ] && op=”$f1 has modified after last read” || op=”$f1 has not modified after last read”
echo “$op”
a= stat -c %s $f1
b= stat -c %s $f2
[ $a -lt $b ] && echo “$f2 takes more space on hard-disk than $f1” || echo “$f1 takes more space on hard-disk than $f2”

i don’t think if there’s any need to explain this program because i have used echo to print what arguments are for….

e.g

[ -d $f1 ]                      &&          op=”$f1 is a directory”            ||            op=”$f1 is not a directory”

 (1)                                                            (2)                                                             (3)

If f1 is directory then i have && it with op=”$f1 is directory” OR not directory

so if   (1) is true then (2) is executed because its AND

if  (1) is false then (3) is executed because its OR

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s