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!










share|improve this question
























  • 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




    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















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!










share|improve this question
























  • 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




    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













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!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 6 hours ago

























asked 6 hours ago









Cyril

332




332












  • 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




    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


















  • 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




    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
















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










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)





share|improve this answer























  • of course if you default to zero mathematics forbid dividing by zero :)
    – francois P
    6 hours ago


















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.






share|improve this answer





















  • 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











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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)





share|improve this answer























  • of course if you default to zero mathematics forbid dividing by zero :)
    – francois P
    6 hours ago















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)





share|improve this answer























  • of course if you default to zero mathematics forbid dividing by zero :)
    – francois P
    6 hours ago













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)





share|improve this answer














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)






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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












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.






share|improve this answer





















  • 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















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.






share|improve this answer





















  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

Morgemoulin

Scott Moir

Souastre