BASH Scripting assistance needed
Well, I am currently a novice shell scripter. I have a basic knowledge of other programming languages such as Javascript for HTML, JScript, VB.NET, and C++. (C++ Not so much, just dipped my feet in the water for that). Most recently, my recent project has been attempting to learn as much as I can about Bash shell scripting. The current issue is understanding status error codes, as well as checking if certain parameters contain directories or if it exists at all. I am working from a book, with tutorials and review questions. Unfortunately there is no answer key, or even hints for that matter.
- Display an error message and exit with status 1 if no parameters are given
- Display an error message and exit with status 2 if source is not a directory
- Display an error message and exit with status 3 if destination does not exist or is not a directory
The above is what I must do, so far the first one I believe I have done correctly, if not please, correct me, or guide me in the proper direction. As I learn best when samples are given, I thought I would ask for assistance.
if [ $? ]; then
echo "You must supply at least one parameter"
exit开发者_StackOverflow 1
fi
#The above is the part I am pretty sure is correct.
if [ $? -d $directory "$1" ]; then
echo "$directory is not a directory"
exit 2
fi
#The above was self written. I am almost positive it is wrong.
if [ $? -lt 2 ]; then
set "$1" .pwd
fi
#the above was given to me from the book as a reference point to start (tutorial)
$?
is the return code of a command you execute. Maybe you are thinking it's a return code of the current script. In any case it's not doing what you think it's doing.
All of my examples assume your command is run like so: script [source] [destination]
Error message if no parameters are given:
if [ ! "$#" ]; then
echo "please supply a parameter"
exit 1
fi
Display error if source is not a directory
if [ ! -d "$1" ]; then
echo "$1 is not a directory"
exit 2
fi
Display error if destination doesn't exist or isn't a directory
if [ ! -d "$2" ]; then
echo "$2 doesn't exist or isn't a directory"
exit 3
fi
精彩评论