0

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 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)

1 Answer 1

2

$? 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 
Sign up to request clarification or add additional context in comments.

6 Comments

There should be a space between if and [. $1 and $2 should be quoted to avoid field splitting and pathname generation. || separates commands so you need if [ ! -e "$2" ] || [ ! -d "$2" ]; then (although the -e test is redundant here, -d already returns false if it does not exist).
Wow, now that you state it, I should have gotten it on my own. I have not even attempted to try it on my own shell, but just from looking at it, it appears to be 100% correct. I appreciate the fast response. Thank you.
@user518073 - no problem. Make sure to check the edits I did per @jiles suggestions.
I did not even notice the mistakes, I typed them properly, the spaces as well as the "$1", however, the redundant -e has been noted.
Checking for an empty $1 can be tricked into a false negative (even though an empty parameter is not always useful). It's best to check $# explicitly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.