Syntax prob. Passing a directory to $1 and testing it.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Syntax prob. Passing a directory to $1 and testing it.
# 1  
Old 06-28-2008
Syntax prob. Passing a directory to $1 and testing it.

I'm running this simple little test:
Code:
#!/bin/sh 

if [ -d $1 ] ; then
        echo $1
else
        echo "Usage:`basename $0` dir"
fi

echo "The end of the script."

The idea is, to test if you have passed a dir to the script. The problem is, it seems to exit the if statement when $1 is null:
Code:
[benjo@hp tmp]$ ./test /tmp/
/tmp/
The end of the script.

[benjo@hp tmp]$ ./test 

The end of the script.
[benjo@hp tmp]$

Does -d have some funky handling i don't know about? Should i be testing against -z or -n first?
What am i missing?
# 2  
Old 06-28-2008
Try this
Code:
#!/usr/bin/ksh93

CMD=$(basename $0)
USAGE="Usage: $CMD dir"

[[ $# < 1 ]] && {
    print $USAGE
    exit 1
}

[[ -d $1 ]] || {
    print "ERROR: $1 is not a directory"
    print $USAGE
    exit 2
}

print "Directory: $1"

exit 0

# 3  
Old 06-28-2008
You've just exposed me to a whole new world of scripting. I've been teaching myself bash/sh and it's hard to find the good stuff. Thankyou.
# 4  
Old 06-28-2008
Quote:
Originally Posted by benjo
I've been teaching myself bash/sh and it's hard to find the good stuff.
Here is the fpmurphy's script (bourne shell version)
Code:
#!/bin/sh
CMD=$(basename $0)
USAGE="Usage: $CMD dir"

if [ $# -lt 1 ]; then
    printf "%s\n" "$USAGE"
    exit 1
elif [ -d $1 ]; then
    echo "Directory: $1"
    exit 0
else
    printf "%s\n%s\n" \
    "ERROR: $1 is not a directory" \
    "$USAGE"
    exit 2
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

File and directory testing

original post -- I have a korn shell script that does some things that depend on creating and writing a file in a directory. I'm looking for a more elegant/efficient way to do the check than what I'm using now: if ] then print "Creating ${STGDIR}/${SHOW}" mkdir... (3 Replies)
Discussion started by: Dalej
3 Replies

2. Shell Programming and Scripting

To find ls of similar pattern files in a directory by passing the variable.

Hi, I have file in my $datadir as below :- SAT_1.txt SAT_2.txt BAT_UD.lst BAT_DD1.lst DUTT_1.txt DUTT_la.txt Expected result :- should get all the above file in $<Filename>_file.lst Below is my code :- for i in SAT BAT DUTT do touch a.lst cd $datadir (1 Reply)
Discussion started by: satishmallidi
1 Replies

3. Shell Programming and Scripting

PERL: testing directory on windows platform

Hi Gurus, kindly analyse the following for me, please OS: Windows 7 Code location: C:\ Output: "Program Files not being recognised" "System Volume Information is a directory" "Windows not being recognised" main { my @dirlist = <*>; foreach my $fn... (0 Replies)
Discussion started by: biglau
0 Replies

4. Shell Programming and Scripting

Problem passing directory as argument with awk

I'm trying to figure out what's getting passed as the argument when I try to pass a directory as an argument, and I'm getting incredibly strange behavior. For example, from the command line I'm typing: nawk -f ./test.awk ~ test.awk contains the following: { directory = $NF print... (13 Replies)
Discussion started by: mrplainswalker
13 Replies

5. Shell Programming and Scripting

Help with Passing argument and testing

Hi all First of all thanks for everyone to read by doubt.Am beginner in shell scripting Following are my doubts i have to pass an argument to shellscript how can i do that second i have to test the argument and shows error when nothing is passes third i have to match exact argument... (3 Replies)
Discussion started by: zeebala1981
3 Replies

6. Programming

c++ string prob

hi could some body help me? im making a program that writes in a file.txt how can i compare my "char x" to another char? #include<iostream> #include<fstream> #include<string.h> using namespace std; int main() { char x; char y; cout << "enter title" << endl; (0 Replies)
Discussion started by: rave03
0 Replies

7. Shell Programming and Scripting

Shell Script Passing Parameters For Directory Listing

i have this basic piece of code that i am trying to debug to accept input parameter to be able to display a directory listing of files. cd /u02/app/eatv/dev/out CURDIR=`pwd` echo directory listing of $CURDIR echo if ; then ls -latr else ls -latr $1 fi basically if the script... (9 Replies)
Discussion started by: wtolentino
9 Replies

8. Shell Programming and Scripting

testing if a file is a directory

i have written this simple script called isdir.sh #! /bin/bash dir=$1 _ls=`ls $dir` for file in $_ls do if then echo "D $file" fi donethe output is not right. for example $ ./isdir.sh src ***no output*** but i have in ~/src some directories drwxr-xr-x 2... (5 Replies)
Discussion started by: and77
5 Replies

9. UNIX for Dummies Questions & Answers

Testing existence of a file /directory

hey guys How can i test existence of a file /directory in a directory in a script thanks (2 Replies)
Discussion started by: ajaya
2 Replies

10. Shell Programming and Scripting

prob

When i am trying to $uncompress tress.dmp.Z I am getting tress.dmp.Z permission denied. What action i have to perform to unzip or uncompress It has rw-r--r-- permissions When i am trying to change the permissions chmod 744 it says. chmod: Warning: can't change tress.dmp.Z Just... (1 Reply)
Discussion started by: dreams5617
1 Replies
Login or Register to Ask a Question