Error adding variables
up vote
0
down vote
favorite
What is wrong with my script?
Script:
Success: $(grep success * | grep B2B | wc -l)
etc_error: $(grep etc_error * | grep B2B | wc -l)
map_timeout: $(grep map_timeout * | grep B2B | wc -l)
success=$(grep success * | grep B2B | wc -l)
etc=$(grep etc_error * | grep B2B | wc -l)
map=$(grep map_timeout * | grep B2B | wc -l)
sum=$(($success + $etc + $map))
total=$(($success / $sum))
echo $total
Error:
-bash: + + : syntax error: operand expected (error token is "+ ")
Thanks!
linux bash shell-script shell
add a comment |
up vote
0
down vote
favorite
What is wrong with my script?
Script:
Success: $(grep success * | grep B2B | wc -l)
etc_error: $(grep etc_error * | grep B2B | wc -l)
map_timeout: $(grep map_timeout * | grep B2B | wc -l)
success=$(grep success * | grep B2B | wc -l)
etc=$(grep etc_error * | grep B2B | wc -l)
map=$(grep map_timeout * | grep B2B | wc -l)
sum=$(($success + $etc + $map))
total=$(($success / $sum))
echo $total
Error:
-bash: + + : syntax error: operand expected (error token is "+ ")
Thanks!
linux bash shell-script shell
The first three lines ought to give youCommand not found
errors inbash
, unless you have scripts calledSuccess:
,etc_error:
andmap_timeout:
in yourPATH
.
– Kusalananda
6 hours ago
1
Either you're not showing the full script or you're not showing the full error messages. Try also running withbash -x
to see what happens.
– Stéphane Chazelas
5 hours ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
What is wrong with my script?
Script:
Success: $(grep success * | grep B2B | wc -l)
etc_error: $(grep etc_error * | grep B2B | wc -l)
map_timeout: $(grep map_timeout * | grep B2B | wc -l)
success=$(grep success * | grep B2B | wc -l)
etc=$(grep etc_error * | grep B2B | wc -l)
map=$(grep map_timeout * | grep B2B | wc -l)
sum=$(($success + $etc + $map))
total=$(($success / $sum))
echo $total
Error:
-bash: + + : syntax error: operand expected (error token is "+ ")
Thanks!
linux bash shell-script shell
What is wrong with my script?
Script:
Success: $(grep success * | grep B2B | wc -l)
etc_error: $(grep etc_error * | grep B2B | wc -l)
map_timeout: $(grep map_timeout * | grep B2B | wc -l)
success=$(grep success * | grep B2B | wc -l)
etc=$(grep etc_error * | grep B2B | wc -l)
map=$(grep map_timeout * | grep B2B | wc -l)
sum=$(($success + $etc + $map))
total=$(($success / $sum))
echo $total
Error:
-bash: + + : syntax error: operand expected (error token is "+ ")
Thanks!
linux bash shell-script shell
linux bash shell-script shell
edited 6 hours ago
asked 6 hours ago
Cyril
332
332
The first three lines ought to give youCommand not found
errors inbash
, unless you have scripts calledSuccess:
,etc_error:
andmap_timeout:
in yourPATH
.
– Kusalananda
6 hours ago
1
Either you're not showing the full script or you're not showing the full error messages. Try also running withbash -x
to see what happens.
– Stéphane Chazelas
5 hours ago
add a comment |
The first three lines ought to give youCommand not found
errors inbash
, unless you have scripts calledSuccess:
,etc_error:
andmap_timeout:
in yourPATH
.
– Kusalananda
6 hours ago
1
Either you're not showing the full script or you're not showing the full error messages. Try also running withbash -x
to see what happens.
– Stéphane Chazelas
5 hours ago
The first three lines ought to give you
Command not found
errors in bash
, unless you have scripts called Success:
, etc_error:
and map_timeout:
in your PATH
.– Kusalananda
6 hours ago
The first three lines ought to give you
Command not found
errors in bash
, unless you have scripts called Success:
, etc_error:
and map_timeout:
in your PATH
.– Kusalananda
6 hours ago
1
1
Either you're not showing the full script or you're not showing the full error messages. Try also running with
bash -x
to see what happens.– Stéphane Chazelas
5 hours ago
Either you're not showing the full script or you're not showing the full error messages. Try also running with
bash -x
to see what happens.– Stéphane Chazelas
5 hours ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
This doesn’t explain everything — given your variable declarations, they should have a value — but since you’re using arithmetic expansion, you don’t need $
to introduce variables; undefined variables then default to 0:
sum=$((success + etc + map))
total=$((success / sum))
echo "$total"
Ideally you should check that sum
is non-zero before dividing, but at least you’ll get a sensible error message with this approach even if you don’t check sum
(“division by 0” rather than “syntax error: operand expected”, in bash).
The error you’re getting comes from
sum=$(($success + $etc + $map))
For some reason, success
, etc
and map
are all undefined or empty. The shell expands the above to
sum=$(( + + ))
and then tries to expand that, which results in the syntax error you quote.
This is rather surprising though since
success=$(grep success * | grep B2B | wc -l)
should produce a value...
BTW, You may want to improve the above to:
success=$(grep success ./* | grep -c B2B)
or, if B2B
is to be matched on the content of the success lines and not on the file names:
success=$(cat ./* | grep success | grep -c B2B)
or with GNU grep
or compatible:
success=$(grep -h success ./* | grep -c B2B)
of course if you default to zero mathematics forbid dividing by zero :)
– francois P
6 hours ago
add a comment |
up vote
0
down vote
you have to check your variables before the addition, because if one of then is not set or has no numeric value
the addition syntax will fail
you can defaulty set vars to 0 before setting then to avoid that.
What do you mean check variables before addition? Can you give some examples? Thanks!
– Cyril
6 hours ago
@Cyril Basic debugging: Output the values of the variables so that you can inspect them and see whether they are what you expect them to be.
– Kusalananda
4 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
This doesn’t explain everything — given your variable declarations, they should have a value — but since you’re using arithmetic expansion, you don’t need $
to introduce variables; undefined variables then default to 0:
sum=$((success + etc + map))
total=$((success / sum))
echo "$total"
Ideally you should check that sum
is non-zero before dividing, but at least you’ll get a sensible error message with this approach even if you don’t check sum
(“division by 0” rather than “syntax error: operand expected”, in bash).
The error you’re getting comes from
sum=$(($success + $etc + $map))
For some reason, success
, etc
and map
are all undefined or empty. The shell expands the above to
sum=$(( + + ))
and then tries to expand that, which results in the syntax error you quote.
This is rather surprising though since
success=$(grep success * | grep B2B | wc -l)
should produce a value...
BTW, You may want to improve the above to:
success=$(grep success ./* | grep -c B2B)
or, if B2B
is to be matched on the content of the success lines and not on the file names:
success=$(cat ./* | grep success | grep -c B2B)
or with GNU grep
or compatible:
success=$(grep -h success ./* | grep -c B2B)
of course if you default to zero mathematics forbid dividing by zero :)
– francois P
6 hours ago
add a comment |
up vote
2
down vote
This doesn’t explain everything — given your variable declarations, they should have a value — but since you’re using arithmetic expansion, you don’t need $
to introduce variables; undefined variables then default to 0:
sum=$((success + etc + map))
total=$((success / sum))
echo "$total"
Ideally you should check that sum
is non-zero before dividing, but at least you’ll get a sensible error message with this approach even if you don’t check sum
(“division by 0” rather than “syntax error: operand expected”, in bash).
The error you’re getting comes from
sum=$(($success + $etc + $map))
For some reason, success
, etc
and map
are all undefined or empty. The shell expands the above to
sum=$(( + + ))
and then tries to expand that, which results in the syntax error you quote.
This is rather surprising though since
success=$(grep success * | grep B2B | wc -l)
should produce a value...
BTW, You may want to improve the above to:
success=$(grep success ./* | grep -c B2B)
or, if B2B
is to be matched on the content of the success lines and not on the file names:
success=$(cat ./* | grep success | grep -c B2B)
or with GNU grep
or compatible:
success=$(grep -h success ./* | grep -c B2B)
of course if you default to zero mathematics forbid dividing by zero :)
– francois P
6 hours ago
add a comment |
up vote
2
down vote
up vote
2
down vote
This doesn’t explain everything — given your variable declarations, they should have a value — but since you’re using arithmetic expansion, you don’t need $
to introduce variables; undefined variables then default to 0:
sum=$((success + etc + map))
total=$((success / sum))
echo "$total"
Ideally you should check that sum
is non-zero before dividing, but at least you’ll get a sensible error message with this approach even if you don’t check sum
(“division by 0” rather than “syntax error: operand expected”, in bash).
The error you’re getting comes from
sum=$(($success + $etc + $map))
For some reason, success
, etc
and map
are all undefined or empty. The shell expands the above to
sum=$(( + + ))
and then tries to expand that, which results in the syntax error you quote.
This is rather surprising though since
success=$(grep success * | grep B2B | wc -l)
should produce a value...
BTW, You may want to improve the above to:
success=$(grep success ./* | grep -c B2B)
or, if B2B
is to be matched on the content of the success lines and not on the file names:
success=$(cat ./* | grep success | grep -c B2B)
or with GNU grep
or compatible:
success=$(grep -h success ./* | grep -c B2B)
This doesn’t explain everything — given your variable declarations, they should have a value — but since you’re using arithmetic expansion, you don’t need $
to introduce variables; undefined variables then default to 0:
sum=$((success + etc + map))
total=$((success / sum))
echo "$total"
Ideally you should check that sum
is non-zero before dividing, but at least you’ll get a sensible error message with this approach even if you don’t check sum
(“division by 0” rather than “syntax error: operand expected”, in bash).
The error you’re getting comes from
sum=$(($success + $etc + $map))
For some reason, success
, etc
and map
are all undefined or empty. The shell expands the above to
sum=$(( + + ))
and then tries to expand that, which results in the syntax error you quote.
This is rather surprising though since
success=$(grep success * | grep B2B | wc -l)
should produce a value...
BTW, You may want to improve the above to:
success=$(grep success ./* | grep -c B2B)
or, if B2B
is to be matched on the content of the success lines and not on the file names:
success=$(cat ./* | grep success | grep -c B2B)
or with GNU grep
or compatible:
success=$(grep -h success ./* | grep -c B2B)
edited 6 hours ago
Stéphane Chazelas
293k54547888
293k54547888
answered 6 hours ago
Stephen Kitt
156k23342414
156k23342414
of course if you default to zero mathematics forbid dividing by zero :)
– francois P
6 hours ago
add a comment |
of course if you default to zero mathematics forbid dividing by zero :)
– francois P
6 hours ago
of course if you default to zero mathematics forbid dividing by zero :)
– francois P
6 hours ago
of course if you default to zero mathematics forbid dividing by zero :)
– francois P
6 hours ago
add a comment |
up vote
0
down vote
you have to check your variables before the addition, because if one of then is not set or has no numeric value
the addition syntax will fail
you can defaulty set vars to 0 before setting then to avoid that.
What do you mean check variables before addition? Can you give some examples? Thanks!
– Cyril
6 hours ago
@Cyril Basic debugging: Output the values of the variables so that you can inspect them and see whether they are what you expect them to be.
– Kusalananda
4 hours ago
add a comment |
up vote
0
down vote
you have to check your variables before the addition, because if one of then is not set or has no numeric value
the addition syntax will fail
you can defaulty set vars to 0 before setting then to avoid that.
What do you mean check variables before addition? Can you give some examples? Thanks!
– Cyril
6 hours ago
@Cyril Basic debugging: Output the values of the variables so that you can inspect them and see whether they are what you expect them to be.
– Kusalananda
4 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
you have to check your variables before the addition, because if one of then is not set or has no numeric value
the addition syntax will fail
you can defaulty set vars to 0 before setting then to avoid that.
you have to check your variables before the addition, because if one of then is not set or has no numeric value
the addition syntax will fail
you can defaulty set vars to 0 before setting then to avoid that.
answered 6 hours ago
francois P
942114
942114
What do you mean check variables before addition? Can you give some examples? Thanks!
– Cyril
6 hours ago
@Cyril Basic debugging: Output the values of the variables so that you can inspect them and see whether they are what you expect them to be.
– Kusalananda
4 hours ago
add a comment |
What do you mean check variables before addition? Can you give some examples? Thanks!
– Cyril
6 hours ago
@Cyril Basic debugging: Output the values of the variables so that you can inspect them and see whether they are what you expect them to be.
– Kusalananda
4 hours ago
What do you mean check variables before addition? Can you give some examples? Thanks!
– Cyril
6 hours ago
What do you mean check variables before addition? Can you give some examples? Thanks!
– Cyril
6 hours ago
@Cyril Basic debugging: Output the values of the variables so that you can inspect them and see whether they are what you expect them to be.
– Kusalananda
4 hours ago
@Cyril Basic debugging: Output the values of the variables so that you can inspect them and see whether they are what you expect them to be.
– Kusalananda
4 hours ago
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481633%2ferror-adding-variables%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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
The first three lines ought to give you
Command not found
errors inbash
, unless you have scripts calledSuccess:
,etc_error:
andmap_timeout:
in yourPATH
.– Kusalananda
6 hours ago
1
Either you're not showing the full script or you're not showing the full error messages. Try also running with
bash -x
to see what happens.– Stéphane Chazelas
5 hours ago