Bash string to integer comparison












2














Here I see the [[ allows comparison between string and integer.



    [usr1@host dir]$ echo $count1
[1] "0"

[usr1@host dir]$ echo $count2
13188

[usr1@host dir]$ if [[ $count1 -ne $count2 ]]
> then
> echo "NE"
> fi
bash: [[: [1] "0": syntax error: operand expected (error token is "[1] "0"")

#this worked fine at one point
if [[ $count1 -ne $count2 ]] then
echo "NE"
fi
syntax error near unexpected token 'then'

if [[ $count1 -ne $count2 ]]; then
echo "NE"
fi
[[: [1] "0": syntax error: operand expected (error token is "[1] "0"")


I am very confused with the way the syntax works. How do we tackle different scenarios? How to prevent the syntax errors irrespective of the value changes(im guessing thats the reason it gives error now).










share|improve this question
























  • (1) Does your $count1 value really include brackets and quotes?  What sort of result do you expect from input like that? (2) In your first example, the ]] and the then are on different lines.  If you put them on the same line, you must put a ; between them.
    – G-Man
    Dec 19 '18 at 5:54










  • @G-Man Simply comparing the count of records from 2 different sources. Which syntax should i be using?
    – sjd
    Dec 19 '18 at 6:02












  • @G-Man Ooh Sorry .. Now only i understood the "[1]" was part of the output of count1. Will try again.. Thanks
    – sjd
    Dec 19 '18 at 6:21










  • @sjd: If the output of count1 is really: [1] "0", then != should be used in if condition to compare the string and a number. Eg: if [[ "$count1" != "$count2" ]] ;then echo "NE"; fi
    – User123
    Dec 19 '18 at 6:42


















2














Here I see the [[ allows comparison between string and integer.



    [usr1@host dir]$ echo $count1
[1] "0"

[usr1@host dir]$ echo $count2
13188

[usr1@host dir]$ if [[ $count1 -ne $count2 ]]
> then
> echo "NE"
> fi
bash: [[: [1] "0": syntax error: operand expected (error token is "[1] "0"")

#this worked fine at one point
if [[ $count1 -ne $count2 ]] then
echo "NE"
fi
syntax error near unexpected token 'then'

if [[ $count1 -ne $count2 ]]; then
echo "NE"
fi
[[: [1] "0": syntax error: operand expected (error token is "[1] "0"")


I am very confused with the way the syntax works. How do we tackle different scenarios? How to prevent the syntax errors irrespective of the value changes(im guessing thats the reason it gives error now).










share|improve this question
























  • (1) Does your $count1 value really include brackets and quotes?  What sort of result do you expect from input like that? (2) In your first example, the ]] and the then are on different lines.  If you put them on the same line, you must put a ; between them.
    – G-Man
    Dec 19 '18 at 5:54










  • @G-Man Simply comparing the count of records from 2 different sources. Which syntax should i be using?
    – sjd
    Dec 19 '18 at 6:02












  • @G-Man Ooh Sorry .. Now only i understood the "[1]" was part of the output of count1. Will try again.. Thanks
    – sjd
    Dec 19 '18 at 6:21










  • @sjd: If the output of count1 is really: [1] "0", then != should be used in if condition to compare the string and a number. Eg: if [[ "$count1" != "$count2" ]] ;then echo "NE"; fi
    – User123
    Dec 19 '18 at 6:42
















2












2








2







Here I see the [[ allows comparison between string and integer.



    [usr1@host dir]$ echo $count1
[1] "0"

[usr1@host dir]$ echo $count2
13188

[usr1@host dir]$ if [[ $count1 -ne $count2 ]]
> then
> echo "NE"
> fi
bash: [[: [1] "0": syntax error: operand expected (error token is "[1] "0"")

#this worked fine at one point
if [[ $count1 -ne $count2 ]] then
echo "NE"
fi
syntax error near unexpected token 'then'

if [[ $count1 -ne $count2 ]]; then
echo "NE"
fi
[[: [1] "0": syntax error: operand expected (error token is "[1] "0"")


I am very confused with the way the syntax works. How do we tackle different scenarios? How to prevent the syntax errors irrespective of the value changes(im guessing thats the reason it gives error now).










share|improve this question















Here I see the [[ allows comparison between string and integer.



    [usr1@host dir]$ echo $count1
[1] "0"

[usr1@host dir]$ echo $count2
13188

[usr1@host dir]$ if [[ $count1 -ne $count2 ]]
> then
> echo "NE"
> fi
bash: [[: [1] "0": syntax error: operand expected (error token is "[1] "0"")

#this worked fine at one point
if [[ $count1 -ne $count2 ]] then
echo "NE"
fi
syntax error near unexpected token 'then'

if [[ $count1 -ne $count2 ]]; then
echo "NE"
fi
[[: [1] "0": syntax error: operand expected (error token is "[1] "0"")


I am very confused with the way the syntax works. How do we tackle different scenarios? How to prevent the syntax errors irrespective of the value changes(im guessing thats the reason it gives error now).







bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 19 '18 at 8:34









ilkkachu

55.9k784155




55.9k784155










asked Dec 19 '18 at 5:42









sjd

1132




1132












  • (1) Does your $count1 value really include brackets and quotes?  What sort of result do you expect from input like that? (2) In your first example, the ]] and the then are on different lines.  If you put them on the same line, you must put a ; between them.
    – G-Man
    Dec 19 '18 at 5:54










  • @G-Man Simply comparing the count of records from 2 different sources. Which syntax should i be using?
    – sjd
    Dec 19 '18 at 6:02












  • @G-Man Ooh Sorry .. Now only i understood the "[1]" was part of the output of count1. Will try again.. Thanks
    – sjd
    Dec 19 '18 at 6:21










  • @sjd: If the output of count1 is really: [1] "0", then != should be used in if condition to compare the string and a number. Eg: if [[ "$count1" != "$count2" ]] ;then echo "NE"; fi
    – User123
    Dec 19 '18 at 6:42




















  • (1) Does your $count1 value really include brackets and quotes?  What sort of result do you expect from input like that? (2) In your first example, the ]] and the then are on different lines.  If you put them on the same line, you must put a ; between them.
    – G-Man
    Dec 19 '18 at 5:54










  • @G-Man Simply comparing the count of records from 2 different sources. Which syntax should i be using?
    – sjd
    Dec 19 '18 at 6:02












  • @G-Man Ooh Sorry .. Now only i understood the "[1]" was part of the output of count1. Will try again.. Thanks
    – sjd
    Dec 19 '18 at 6:21










  • @sjd: If the output of count1 is really: [1] "0", then != should be used in if condition to compare the string and a number. Eg: if [[ "$count1" != "$count2" ]] ;then echo "NE"; fi
    – User123
    Dec 19 '18 at 6:42


















(1) Does your $count1 value really include brackets and quotes?  What sort of result do you expect from input like that? (2) In your first example, the ]] and the then are on different lines.  If you put them on the same line, you must put a ; between them.
– G-Man
Dec 19 '18 at 5:54




(1) Does your $count1 value really include brackets and quotes?  What sort of result do you expect from input like that? (2) In your first example, the ]] and the then are on different lines.  If you put them on the same line, you must put a ; between them.
– G-Man
Dec 19 '18 at 5:54












@G-Man Simply comparing the count of records from 2 different sources. Which syntax should i be using?
– sjd
Dec 19 '18 at 6:02






@G-Man Simply comparing the count of records from 2 different sources. Which syntax should i be using?
– sjd
Dec 19 '18 at 6:02














@G-Man Ooh Sorry .. Now only i understood the "[1]" was part of the output of count1. Will try again.. Thanks
– sjd
Dec 19 '18 at 6:21




@G-Man Ooh Sorry .. Now only i understood the "[1]" was part of the output of count1. Will try again.. Thanks
– sjd
Dec 19 '18 at 6:21












@sjd: If the output of count1 is really: [1] "0", then != should be used in if condition to compare the string and a number. Eg: if [[ "$count1" != "$count2" ]] ;then echo "NE"; fi
– User123
Dec 19 '18 at 6:42






@sjd: If the output of count1 is really: [1] "0", then != should be used in if condition to compare the string and a number. Eg: if [[ "$count1" != "$count2" ]] ;then echo "NE"; fi
– User123
Dec 19 '18 at 6:42












1 Answer
1






active

oldest

votes


















2














Your count1 variable contains the string [1] "0". This is a eight character string that is not an integer.



Even if the value had been just "0", the test [[ $count1 -ne $count2 ]] with $count1 being "0" is very different from [[ "$count1" -ne "$count2" ]] with $count1 being the single character string 0.






share|improve this answer



















  • 1




    even worse, count1 seems to contain the seven characters [1] "0". Same syntax error, though.
    – ilkkachu
    Dec 19 '18 at 8:37










  • @ilkkachu Ah, I missed that. I'll update in a few minutes. Thanks!
    – Kusalananda
    Dec 19 '18 at 9:17










  • Actually the data in count1 was transferred from a R Function. Which always returns the index [1] with the result. once that issue is resolved i cound fix it thanks
    – sjd
    Dec 19 '18 at 12:00











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',
autoActivateHeartbeat: false,
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%2f489829%2fbash-string-to-integer-comparison%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Your count1 variable contains the string [1] "0". This is a eight character string that is not an integer.



Even if the value had been just "0", the test [[ $count1 -ne $count2 ]] with $count1 being "0" is very different from [[ "$count1" -ne "$count2" ]] with $count1 being the single character string 0.






share|improve this answer



















  • 1




    even worse, count1 seems to contain the seven characters [1] "0". Same syntax error, though.
    – ilkkachu
    Dec 19 '18 at 8:37










  • @ilkkachu Ah, I missed that. I'll update in a few minutes. Thanks!
    – Kusalananda
    Dec 19 '18 at 9:17










  • Actually the data in count1 was transferred from a R Function. Which always returns the index [1] with the result. once that issue is resolved i cound fix it thanks
    – sjd
    Dec 19 '18 at 12:00
















2














Your count1 variable contains the string [1] "0". This is a eight character string that is not an integer.



Even if the value had been just "0", the test [[ $count1 -ne $count2 ]] with $count1 being "0" is very different from [[ "$count1" -ne "$count2" ]] with $count1 being the single character string 0.






share|improve this answer



















  • 1




    even worse, count1 seems to contain the seven characters [1] "0". Same syntax error, though.
    – ilkkachu
    Dec 19 '18 at 8:37










  • @ilkkachu Ah, I missed that. I'll update in a few minutes. Thanks!
    – Kusalananda
    Dec 19 '18 at 9:17










  • Actually the data in count1 was transferred from a R Function. Which always returns the index [1] with the result. once that issue is resolved i cound fix it thanks
    – sjd
    Dec 19 '18 at 12:00














2












2








2






Your count1 variable contains the string [1] "0". This is a eight character string that is not an integer.



Even if the value had been just "0", the test [[ $count1 -ne $count2 ]] with $count1 being "0" is very different from [[ "$count1" -ne "$count2" ]] with $count1 being the single character string 0.






share|improve this answer














Your count1 variable contains the string [1] "0". This is a eight character string that is not an integer.



Even if the value had been just "0", the test [[ $count1 -ne $count2 ]] with $count1 being "0" is very different from [[ "$count1" -ne "$count2" ]] with $count1 being the single character string 0.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 19 '18 at 12:12

























answered Dec 19 '18 at 8:19









Kusalananda

121k16229372




121k16229372








  • 1




    even worse, count1 seems to contain the seven characters [1] "0". Same syntax error, though.
    – ilkkachu
    Dec 19 '18 at 8:37










  • @ilkkachu Ah, I missed that. I'll update in a few minutes. Thanks!
    – Kusalananda
    Dec 19 '18 at 9:17










  • Actually the data in count1 was transferred from a R Function. Which always returns the index [1] with the result. once that issue is resolved i cound fix it thanks
    – sjd
    Dec 19 '18 at 12:00














  • 1




    even worse, count1 seems to contain the seven characters [1] "0". Same syntax error, though.
    – ilkkachu
    Dec 19 '18 at 8:37










  • @ilkkachu Ah, I missed that. I'll update in a few minutes. Thanks!
    – Kusalananda
    Dec 19 '18 at 9:17










  • Actually the data in count1 was transferred from a R Function. Which always returns the index [1] with the result. once that issue is resolved i cound fix it thanks
    – sjd
    Dec 19 '18 at 12:00








1




1




even worse, count1 seems to contain the seven characters [1] "0". Same syntax error, though.
– ilkkachu
Dec 19 '18 at 8:37




even worse, count1 seems to contain the seven characters [1] "0". Same syntax error, though.
– ilkkachu
Dec 19 '18 at 8:37












@ilkkachu Ah, I missed that. I'll update in a few minutes. Thanks!
– Kusalananda
Dec 19 '18 at 9:17




@ilkkachu Ah, I missed that. I'll update in a few minutes. Thanks!
– Kusalananda
Dec 19 '18 at 9:17












Actually the data in count1 was transferred from a R Function. Which always returns the index [1] with the result. once that issue is resolved i cound fix it thanks
– sjd
Dec 19 '18 at 12:00




Actually the data in count1 was transferred from a R Function. Which always returns the index [1] with the result. once that issue is resolved i cound fix it thanks
– sjd
Dec 19 '18 at 12:00


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489829%2fbash-string-to-integer-comparison%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre