That doesn't do what you think it does. (I made the same mistake when I first started shell scripting.)
The greater-than symbol, >, is a redirection operator, not a comparison operator (unless it is escaped, in which case it is string comparison, not numerical). What you need is:
#!/bin/bash
# scriptname : A script to do something with a directory
#
# Parameter validation
if [ ! $# -eq 1 ]
then
echo "Usage: scriptname directory"
exit 1
fi
# Save supplied parameter
mydir="$1"
# Does the directory exist?
if [ ! -d "${mydir}" ]
then
echo "Directory $1 not found"
exit 1
fi
# Processing continues here
@cfajohnson
I only agree with the simplification of the "not" condition in the first command you post. The rest is pedantic and naive and reduces the resiliance of the script to duff parameters and similar environment variable names.
If I thought that there were lots of parameters I would have posted:
mydir="${1}"
When you get into 5,000 line Shell scripts you take all precautions.
@cfajohnson
Please tell me how you would deal with the 10th 11th and 12th parameter to the Shell script?
Is $1 unambiguous in every context in a Shell script? This is a rhetorical question because I know that you know the answer.
${1} is totally unambiguous.
Whether or not it it is strictly necessary in the context of that particular statement.
Btw. this sort of interchange between professionals is fun and I enjoy it.
The specification does not matter. If you write paranoid scripts which assume that the parameters and data may not conform to specification they will be much more robust when the bad data comes.