Shell Script - (File and Directory)

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Shell Script - (File and Directory)
# 1  
Old 11-25-2012
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.


2. Relevant notes:

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


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

My Work:

Code:
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

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

York University, Toronto, Prof. Bil, cse203 1
# 2  
Old 11-25-2012
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

Last edited by alister; 11-25-2012 at 06:41 PM..
# 3  
Old 11-25-2012
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:

PHP Code:
echo "File 'abc.txt' does not exist." 

Last edited by spider-man; 11-25-2012 at 06:46 PM..
# 4  
Old 11-26-2012
Quote:
Originally Posted by spider-man
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:

PHP Code:
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

2. Shell Programming and Scripting

What is the function of the following lines at the top of a shell script file: Directory and Script?

The file starts like this: Directory: <path to the script> Script: <script fife name> #!bin/ksh ##Comments <actual script> What is the use of the first two lines in the script? What if I save the file without them? What will be the effect? They are not comments. Im very new to this,... (4 Replies)
Discussion started by: remytom
4 Replies

3. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

4. Shell Programming and Scripting

Automating using shell script : edit the file in a directory

I am trying to automate hadoop installation procedure using shell script. It involves go to perticular directory and add some more lines to the file /etc/sysctl.conf. How this can be done? Regards Navaz (1 Reply)
Discussion started by: Abdul Navaz
1 Replies

5. Shell Programming and Scripting

Shell script to monitor new file in a directory and mail the file content

Hi I am looking for a help in designing a bash script on linux which can do below:- 1) Look in a specific directory for any new files 2) Mail the content of the new file Appreciate any help Regards Neha (5 Replies)
Discussion started by: neha0785
5 Replies

6. Homework & Coursework Questions

Shell Script - (file and directory)

The question is here: http://farm9.staticflickr.com/8065/8217767191_2154e64904_b.jpg 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. My Work: for fileOrDirectory in $* # all... (1 Reply)
Discussion started by: spider-man
1 Replies

7. Shell Programming and Scripting

delete only particular file in directory shell script

Hi, what function do we use to delete only particular file that we want in directory shell script for example I want only to delete test.txt in directory how to do it ? in sh Thank (1 Reply)
Discussion started by: guidely
1 Replies

8. Shell Programming and Scripting

Shell script for a writing the directory structure to a file

Hi All, I am new user of shell scripting has come up with a problem. that I have a directory structure like : Home | |--------A | |----trunk | |-------A_0_1/ | | | |-------A_0_2/ | |--------B | ... (6 Replies)
Discussion started by: bhaskar_m
6 Replies

9. Shell Programming and Scripting

getting : No such file or directory while executing a shell script

Hi all, I am getting : No such file or directory while executing a shell script. But i have that corresponding file in the corresponding path. It also have executable rights. Please help me out in this Thanks in advance. Ananthi.U (7 Replies)
Discussion started by: ananthi_ku
7 Replies

10. Shell Programming and Scripting

shell script to search content of file with timestamps in the directory

hello, i want to make a script to search the file contents in my home directory by a given date and output me the line that has the date... (10 Replies)
Discussion started by: psychobeauty
10 Replies
Login or Register to Ask a Question