--------------------------------------------------- | Date: 2002-04-17 01:52:08 | | Filename: directory.sh | | Author: mind@metalshell.com | | | | http://www.metalshell.com/ | --------------------------------------------------- #!/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