Shell Script - (File and Directory)

  1. The problem statement, all variables and given/known data:

My problem is that when I made up a non-existing directory, it prints out "hiii". It didn't enter the first if statement. It works if my directory exist.

  1. Relevant notes:

The question is here:
http://farm9.staticflickr.com/8065/8...54e64904_b.jpg

  1. The attempts at a solution (include all code and scripts):
    Shell Script - (file and directory)

My Work:

for fileOrDirectory in $*     ## I'm not sure if I should use $*
do

##############
if test -d $fileOrDirectory    #check if argument is a directory.
then

    if test ! -e  $fileOrDirectory    #check if the directory does exist, exit!
    then
        echo Directory "$fileOrDirectory" does not exist.        ## not sure how to print out the current directory.
        exit 1  #exit failure.
    else                     # make directory executable and readable  .
        chmod a+rx $fileOrDirectory
        echo Directory $fileOrDirectory is now made public.
    fi
fi
    
###########

if test -f $fileOrDirectory    #check if argument is a file.
then
    if test ! -e  $fileOrDirectory    #check if the file does exist, exit!
    then
        echo File "$fileOrDirectory" does not exist.
        exit 1    #exit failure.
    else                     # make file readable. 
        chmod a+r $fileOrDirectory
        echo File $fileOrDirectory is now made public.
    fi
fi
    echo  hiii
done

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

York University, Toronto, Prof. Bil, cse203 1

If the filename doesn't exist, why would you expect it to enter the first if statement? To put it differently, if something doesn't exist, how could it possibly be either a directory or a file? test -d and test -f both fail if their argument doesn't exist (which, by the way, means that both of your tests for existence, test ! -e, are pointless, as they can never succeed since they follow a successful test -d or test -f, both of which are predicated on existence).

A simpler approach would be to simply set the mode depending on whether a file or directory exists, instead of having multiple code paths leading to different chmod commands.

Regards,
Alister

Ok, I get you.

If I test -d and test -f, and fail, how can I tell if the path takes me to a file or a directory?
Because for the error message, I need to say something like:

echo "File 'abc.txt' does not exist."

The whole point is that test -d fails fails then file is not a pathname naming an existing directory. Once you know it is not a directory if test -f fails fails then file is not a pathname naming an existing regular file. It isn't clear to me whether you want to use test -e file (a file of any type exists with that name) or test -f file (a regular file exists with that name). The directions in your assignment are to change the mode one way if the current argument names a directory, change the mode another way if it is an existing file that is not a directory, and report an error if the file doesn't exist.

Note that according to the standards a directory is a file. Your comments make it sound like a directory is not a file (and the assigment text has the same ambiguity). There are several types of files including: directories, regular files, block special files, character special files, pipes, symbolic links, sockets, etc. Some people say "file" when they mean "regular file". Some people (and the standards) say "file" when they mean a file of any type. If your professor hasn't discussed this, you may want to ask for clarification of your assigment to determine if you're supposed to use chmod a+r for all files that are not directories or to only use chmod a+r for regular files.

Should you be checking whether or not the invocations of chmod succeed?