Bash shell - Check if value is valid directory.

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Bash shell - Check if value is valid directory.
# 1  
Old 11-14-2011
Bash shell - Check if value is valid directory.

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

The script usage will be as follows:
library.third source_directory [destination directory]

- Your script will display an appropriate error message and exit with status 3 if no parameters are given
- Your script will display an appropriate error message and exit with status 4 if the source directory does not exist
- Your script will display an appropriate error message and exit with status 5 if the source is not a directory
- Your script will display an appropriate error message and exit with status 6 if destination directory does not exist
- Your script will display an appropriate error message and exit with status 7 if destination is not a directory

2. Relevant commands, code, scripts, algorithms:

Simple Unix Bash commands have been used in this program, that is all that has been taught in the course so far. By this, I mean if statements and basic looping, nothing complex.

3. The attempts at a solution (include all code and scripts):

The section I am having trouble with is:
Your script will display an appropriate error message and exit with status 5 if the source is not a directory.

The statement does not recognize any file type as a directory.

Code:
DIR="$1"

if [ $# -lt 1 ]
then
        echo "Incorrect Usage"
        exit 3

elif [ ! -d "$DIR" ]
then
        echo "Directory does not exist!"
        exit 4

elif [ "$DIR" != -d ]
then
        echo "Not a Directory"
        exit 5
else
        echo "Path is okay"

fi



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

Seneca College of Applied Arts of Technology, Toronto, Canada, Michal Heidenreich, OPS105
# 2  
Old 11-14-2011
you almost got it:
Code:
....
elif [ ! -e "$DIR" ] 
then         
   echo "Directory does not exist!"         
   exit 4  
elif [ ! -d "$DIR" ] 
then         
    echo "Not a Directory"        
    exit 5
....

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 11-14-2011
You're almost there.

Code:
elif [ "$DIR" != -d ]

This checks if the string "$DIR" is the same as the string "-d", which of course it isn't.

You had it right in the last statement, with [ ! -d "$DIR" ]. That causes an error if "$DIR" isn't a dir or it doesn't exist.

So you really have it covered with the first two. Just chop out all of this:
Code:
elif [ "$DIR" != -d ]
then
        echo "Not a Directory"
        exit 5

and you'll be okay.

---------- Post updated at 04:11 PM ---------- Previous update was at 04:10 PM ----------

Oh, and if they insist you check if it exists or not seperately, check with [ ! -e "$DIR" ] before you check with -d.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to check directory and create missing folder from file

In the below bash I am trying to ensure that all folders (represented by $folders) in a given directory are created. In the file f1 the trimmed folder will be there somewhere (will be multiple trimmed folders). When that trimmed folder is found (represented by $S5) the the contents of $2 printed... (19 Replies)
Discussion started by: cmccabe
19 Replies

2. Shell Programming and Scripting

Check for valid hostnames

Hello, I am trying to develop a script to check for valid hostnames. Below are the prerequisites for a valid hostname which I got from wiki : Hostnames are composed of series of labels concatenated with dots, as are all domain names. For example, "en.wikipedia.org" is a hostname. Each label... (8 Replies)
Discussion started by: rahul2662
8 Replies

3. Shell Programming and Scripting

Check if time format is valid

How can I validate if time (HH:MM:SS) argument is valid? I got this from web but I can't modify it to exit the script if the time argument is invalid. echo $1 | awk -F ':' '{ print ($1 <= 23 && $2 <= 59 && $3 <= 59) ? "good" : "bad" }' ex: ./script.ksh 12:34:21 = okay ./script.ksh... (10 Replies)
Discussion started by: erin00
10 Replies

4. Shell Programming and Scripting

Check if a string is a valid timestamp in UNIX.

Hi all, I have date and time value in a string, I want to check if it is a valid date and time. Need help on this. Thanks (7 Replies)
Discussion started by: Pratiksha Mehra
7 Replies

5. Shell Programming and Scripting

Need a script to check if an argument is valid shell variable

I need a script that should print 'yes' if the argument is a valid shell variable name else 'No' if it is not a valid shell variable. A valid one begins with an alphabet or percentage (%) character and is followed by zero or more alphanumberic or percentage (%) characters. For example: $... (6 Replies)
Discussion started by: pingiliarjun
6 Replies

6. Shell Programming and Scripting

How to check the user input to be valid using shell script?

How to check the user input to be valid using shell script? The valid input is in the format like as follows. 1. It can only have r,w,x or a hyphen and nothing else. 2. ensure the r, w, x are in the correct order. for example: rwxr-xr-x is a valid format. Thanks (5 Replies)
Discussion started by: hyeewang
5 Replies

7. UNIX for Dummies Questions & Answers

How to check if file contains valid strings?

Hi All, I am a newbie...I would like to have a function which ll check if a file contains valid strings before "=" operator. Just to give you my requirement: assume my file has content: hello= gsdgsd sfdsg sgdsg sgdgdg world= gggg hhhh iiiii xxxx= pppp ppppp pppp my... (5 Replies)
Discussion started by: rtagarra
5 Replies

8. Shell Programming and Scripting

how to check for valid password

I need to check if an account has a valid password. Would something like this work? read ACCNAME if grep -q "^$ACCNAME:\$6:" /etc/shadow; thenI noticed every entry in my shadow file that has a password starts with $6 ... it works for my current setup, but would it always work? I can't test... (4 Replies)
Discussion started by: ADay2Long
4 Replies

9. Shell Programming and Scripting

to check whether a directory or filename path is valid or not

the script on excution should take a directory path from useran a numric input and it should check indicate whether its write or not? if the cmmd sh<script-name>,dir/path.<500>" is greater than 500 in size should be copied to dir ,temp in pwd and display the mesage'files of 2000 bytes hav been... (4 Replies)
Discussion started by: arukr
4 Replies

10. Shell Programming and Scripting

How to check for a valid numeric input

Hi Folks, I'm using bash script. I would like to check whether input is a number or not.(Only positive numbers).. if space or non numeric is entered, it should say "invalid input". pls help.. thanks in adv. Br/// Vijay. (1 Reply)
Discussion started by: Vijayakumarpc
1 Replies
Login or Register to Ask a Question