Scripts - testing conditions

Hi everyone,

My name is Liviu and I’m new on this forum although I’ve been using OpenSUSE for more than one year. I’m currently trying to learn scripting in BASH and encountered following issue: the condition testing doesn’t always seems to work properly.

Following example is part of a script I’m writing for a quick file transfer to Dropbox:

**if test -z “$1”; #checking if first parameter was entered (executes fine)
then
echo “Option not entered. Enter s for the Shared directory or p for the Private directory”;
exit 1;
fi
if test “$1”!=“s” && “$1”!=“p”; #first parameter must either be s or p otherwise the script will exit with code 1
then
echo “Invalid option entered. Enter s for Shared, p for Private”;
exit 1;
else
dir="$1";
src=pwd;
dest=’/home/Liviu/Dropbox’;
fi

**When debugging with bashdb I got following issues:

  • a “command not found” warning for the 6th line: if test “$1”!=“s” && “$1”!=“p”
  • the “else” section seems executes even if the first script parameter is different than s or p
    First section which checks if the first parameter was entered (-z) executes properly.

Am I doing something wrong? I tried to figure out if there are any syntax errors but I couldn’t find any. And when using ] instead of test I also get weird script behavior.
Thanks in advance for your help!
Liviu

PS. Is there any way to debug scripts using EMACS? I’m using this tool for script writing and generally for text document processing.

First, welcome to the forums, and to bash scripting.

Second, use code tags whenever you post raw output from anything. These
are, in the web interface, the little ‘#’ button down below the main text
window, and it helps preserve text exactly so nothing is lost from you to
us, or vice versa.

Third, all scripts need a shebang line. Not really, but you should always
have it as the very first line. For bash stuff:

Code:

#!/bin/bash

Fourth, debugging in bash is easy to do with the -x and -v options to the
bash executable. You can even make this automatic by using the shebang
line in your script:

Code:

#!/bin/bash -xv

or do it on the fly:

Code:

bash -xv /path/to/your/script

Next, bash really really really cares about spaces. In the version of
your script that I see you do not have spaces around your conditional
operators, and you need them.

Code:

“$1”!=“s”
#should be
“$1” != “s”

Finally, ‘&&’ is what you use, when outside conditionals, to run two
commands at once (the second depending on the success of the first).
Since you are not telling bash differently, you have one test and then you
are telling it to run something else. Try this instead:

Code:

if “$1” != “s” && “$1” != “p” ]]; #first parameter must either be s or
p otherwise the script will exit with code 1

Within the brackets bash knows this is an AND operator and not an operator
to run one command and then another if the first was true.


Good luck.

If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below…

Thank you very much. It works!