Problem with using if/then statement: `((: == : syntax error: operand expected (error token is “== ”)`
up vote
0
down vote
favorite
I try to implement positional parameters in bash script and check them, but have some issue in if/then switch statement.
Script:
#set "default" parameter
query=""
while [ "$1" != "" ]; do
case $1 in
-q | --query ) shift
query=$1
;;
esac
shift
done
#query cannot be empty
if (($query == ""))
then
echo 'no query'
exit
fi
echo "query - $query"
All works well when I set the correct parameters:
$ ./script.sh -q request
query - request
-q
parameter cannot be empty and I need to validate it. So, when the command line looks like:
$ ./script.sh -q
or
$ ./script.sh
I get this error:
./main.sh: line 13: ((: == : syntax error: operand expected (error token is "== ")
query -
How to correctly implement if
operator in this case?
command-line bash scripts
add a comment |
up vote
0
down vote
favorite
I try to implement positional parameters in bash script and check them, but have some issue in if/then switch statement.
Script:
#set "default" parameter
query=""
while [ "$1" != "" ]; do
case $1 in
-q | --query ) shift
query=$1
;;
esac
shift
done
#query cannot be empty
if (($query == ""))
then
echo 'no query'
exit
fi
echo "query - $query"
All works well when I set the correct parameters:
$ ./script.sh -q request
query - request
-q
parameter cannot be empty and I need to validate it. So, when the command line looks like:
$ ./script.sh -q
or
$ ./script.sh
I get this error:
./main.sh: line 13: ((: == : syntax error: operand expected (error token is "== ")
query -
How to correctly implement if
operator in this case?
command-line bash scripts
1
This script lacks a shebang.
– dessert
Nov 15 at 22:29
Who and why lowered my question ??
– Valentyn Hruzytskyi
Nov 15 at 22:40
@wjandrea, thanks. Comments I did edit. But all this code I need to put, becauseif
statement have different error in different parameters in command line (if parameter exist - I did not get an error inif...
line), so I showed all.
– Valentyn Hruzytskyi
Nov 15 at 23:06
The edits are a huge improvement, thanks. I've deleted my earlier comment.
– wjandrea
Nov 15 at 23:15
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I try to implement positional parameters in bash script and check them, but have some issue in if/then switch statement.
Script:
#set "default" parameter
query=""
while [ "$1" != "" ]; do
case $1 in
-q | --query ) shift
query=$1
;;
esac
shift
done
#query cannot be empty
if (($query == ""))
then
echo 'no query'
exit
fi
echo "query - $query"
All works well when I set the correct parameters:
$ ./script.sh -q request
query - request
-q
parameter cannot be empty and I need to validate it. So, when the command line looks like:
$ ./script.sh -q
or
$ ./script.sh
I get this error:
./main.sh: line 13: ((: == : syntax error: operand expected (error token is "== ")
query -
How to correctly implement if
operator in this case?
command-line bash scripts
I try to implement positional parameters in bash script and check them, but have some issue in if/then switch statement.
Script:
#set "default" parameter
query=""
while [ "$1" != "" ]; do
case $1 in
-q | --query ) shift
query=$1
;;
esac
shift
done
#query cannot be empty
if (($query == ""))
then
echo 'no query'
exit
fi
echo "query - $query"
All works well when I set the correct parameters:
$ ./script.sh -q request
query - request
-q
parameter cannot be empty and I need to validate it. So, when the command line looks like:
$ ./script.sh -q
or
$ ./script.sh
I get this error:
./main.sh: line 13: ((: == : syntax error: operand expected (error token is "== ")
query -
How to correctly implement if
operator in this case?
command-line bash scripts
command-line bash scripts
edited Nov 16 at 2:29
wjandrea
7,73642258
7,73642258
asked Nov 15 at 22:11
Valentyn Hruzytskyi
2009
2009
1
This script lacks a shebang.
– dessert
Nov 15 at 22:29
Who and why lowered my question ??
– Valentyn Hruzytskyi
Nov 15 at 22:40
@wjandrea, thanks. Comments I did edit. But all this code I need to put, becauseif
statement have different error in different parameters in command line (if parameter exist - I did not get an error inif...
line), so I showed all.
– Valentyn Hruzytskyi
Nov 15 at 23:06
The edits are a huge improvement, thanks. I've deleted my earlier comment.
– wjandrea
Nov 15 at 23:15
add a comment |
1
This script lacks a shebang.
– dessert
Nov 15 at 22:29
Who and why lowered my question ??
– Valentyn Hruzytskyi
Nov 15 at 22:40
@wjandrea, thanks. Comments I did edit. But all this code I need to put, becauseif
statement have different error in different parameters in command line (if parameter exist - I did not get an error inif...
line), so I showed all.
– Valentyn Hruzytskyi
Nov 15 at 23:06
The edits are a huge improvement, thanks. I've deleted my earlier comment.
– wjandrea
Nov 15 at 23:15
1
1
This script lacks a shebang.
– dessert
Nov 15 at 22:29
This script lacks a shebang.
– dessert
Nov 15 at 22:29
Who and why lowered my question ??
– Valentyn Hruzytskyi
Nov 15 at 22:40
Who and why lowered my question ??
– Valentyn Hruzytskyi
Nov 15 at 22:40
@wjandrea, thanks. Comments I did edit. But all this code I need to put, because
if
statement have different error in different parameters in command line (if parameter exist - I did not get an error in if...
line), so I showed all.– Valentyn Hruzytskyi
Nov 15 at 23:06
@wjandrea, thanks. Comments I did edit. But all this code I need to put, because
if
statement have different error in different parameters in command line (if parameter exist - I did not get an error in if...
line), so I showed all.– Valentyn Hruzytskyi
Nov 15 at 23:06
The edits are a huge improvement, thanks. I've deleted my earlier comment.
– wjandrea
Nov 15 at 23:15
The edits are a huge improvement, thanks. I've deleted my earlier comment.
– wjandrea
Nov 15 at 23:15
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
(( ... ))
is for arithmetic evaluation.
You should use [ ... ]
(just like you did in the while
condition) or bash's extended test syntax [[ ... ]]
- and remember to quote "$query" and leave whitespace around the [
and ]
operators:
if [ "$query" == "" ]
Alternatively, use the -z
empty string test:
if [ -z "$query" ]
For general help with questions like this, try www.shellcheck.net or install the shellcheck
package from the universe
repository.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
(( ... ))
is for arithmetic evaluation.
You should use [ ... ]
(just like you did in the while
condition) or bash's extended test syntax [[ ... ]]
- and remember to quote "$query" and leave whitespace around the [
and ]
operators:
if [ "$query" == "" ]
Alternatively, use the -z
empty string test:
if [ -z "$query" ]
For general help with questions like this, try www.shellcheck.net or install the shellcheck
package from the universe
repository.
add a comment |
up vote
1
down vote
accepted
(( ... ))
is for arithmetic evaluation.
You should use [ ... ]
(just like you did in the while
condition) or bash's extended test syntax [[ ... ]]
- and remember to quote "$query" and leave whitespace around the [
and ]
operators:
if [ "$query" == "" ]
Alternatively, use the -z
empty string test:
if [ -z "$query" ]
For general help with questions like this, try www.shellcheck.net or install the shellcheck
package from the universe
repository.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
(( ... ))
is for arithmetic evaluation.
You should use [ ... ]
(just like you did in the while
condition) or bash's extended test syntax [[ ... ]]
- and remember to quote "$query" and leave whitespace around the [
and ]
operators:
if [ "$query" == "" ]
Alternatively, use the -z
empty string test:
if [ -z "$query" ]
For general help with questions like this, try www.shellcheck.net or install the shellcheck
package from the universe
repository.
(( ... ))
is for arithmetic evaluation.
You should use [ ... ]
(just like you did in the while
condition) or bash's extended test syntax [[ ... ]]
- and remember to quote "$query" and leave whitespace around the [
and ]
operators:
if [ "$query" == "" ]
Alternatively, use the -z
empty string test:
if [ -z "$query" ]
For general help with questions like this, try www.shellcheck.net or install the shellcheck
package from the universe
repository.
answered Nov 15 at 22:36
steeldriver
64.6k11100171
64.6k11100171
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1093318%2fproblem-with-using-if-then-statement-syntax-error-operand-expected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
This script lacks a shebang.
– dessert
Nov 15 at 22:29
Who and why lowered my question ??
– Valentyn Hruzytskyi
Nov 15 at 22:40
@wjandrea, thanks. Comments I did edit. But all this code I need to put, because
if
statement have different error in different parameters in command line (if parameter exist - I did not get an error inif...
line), so I showed all.– Valentyn Hruzytskyi
Nov 15 at 23:06
The edits are a huge improvement, thanks. I've deleted my earlier comment.
– wjandrea
Nov 15 at 23:15