|
|
| Learning the bash Shell, 2nd Edition (ISBN: 1565923472) |
 |
List Price: $29.95
Our Price: $20.97
Used Price: $18.59
Release Date: January, 1998
Manufacturer: O'Reilly & Associates (Paperback)
Sales Rank: 3,708
Author: Cameron Newham, Bill Rosenblatt
|
More Info
|
|
|
Directory Listing
|
2002-04-17 01:52:08
|
| |
sh
|
|
|
|
|
Category: source:sh:files
|
|
Description: List files in a directory and display some properities of each.
|
|
Platform: unix
|
|
Author: mind
|
|
Viewed: 4836
|
|
Rating: 3.7/5 (19 votes)
|
|
|
| If you have any questions about
this piece of code or still need help, try posting your question on the forum. |
#!/bin/sh
#
# For example - mind [mind@metalshell.com]
#
# This is an example on using the for function to list everything
# in a directory and print whether a file is a directory and
# readable or a normal file and readable.
#
#
# -a file True if file exists.
# -b file True if file is a block special.
# -c file True if file is a character special.
# -d file True if file is a directory.
# -e file True if file exists.
# -f file True if file is a regular file.
# -g file True if file is setgid.
# -h file True if file is a symbolic link.
# -k file True if file is sticky.
# -p file True if file is a named pipe.
# -r file True if file is readable.
# -s file True if file's size is greater than zero.
# -t fd True if fd is an open file descriptor.
#
# [ http://www.metalshell.com ]
#
string="This is our string"
Directory="/"
#Directory="/tmp/" # Make sure you include a '/' after the directory
# Heres a short example on using for to print each word
# in string containing words..
for str in $string ; do
echo "$str"
done
# Check to make sure the directory is valid before scanning through it
if [ ! -d $Directory ]; then
echo "Sorry but $Directory is an invalid directory"
exit
fi
# Begin our for statement
for FILE in $Directory* ; do
# Check if $FILE is a directory
if [ -d $FILE ]; then
# Check if the directory is readable
if [ -r $FILE ]; then
echo "(Readable Directory): $FILE"
else
echo "(Non-Readable Directory): $FILE"
fi
fi
# Check if $FILE is a regular file
if [ -f $FILE ]; then
# Check if the regular file is readable
if [ -r $FILE ]; then
echo "(Readable File): $FILE"
else
echo "(Non-Readable File): $FILE"
fi
fi
# End of our for statement
done
|
|
|