LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   10 command not found (https://www.linuxquestions.org/questions/linux-newbie-8/10-command-not-found-4175670104/)

spalisetty 02-23-2020 07:36 AM

10 command not found
 
Hello,
I am working on learning if-else statement, no matter what I do, it is throwing error. I am working on Ubuntu.

#! /bin/bash
count=10
if [$count -eq 10]
then
echo "condition is true"
fi


Kindly help

spalisetty 02-23-2020 07:38 AM

ubuntu@ip-172-31-92-85:~/suman$ cat ifelse.sh
#! /bin/bash
count=10
if [$count -eq 10]
then
echo "condition is true"
fi
ubuntu@ip-172-31-92-85:~/suman$ ./ifelse.sh
./ifelse.sh: line 3: [10: command not found
ubuntu@ip-172-31-92-85:~/suman$

berndbausch 02-23-2020 07:45 AM

You need white space after the [, otherwise the shell thinks you want to run a program named [10. Also white space before the ].

spalisetty 02-23-2020 07:48 AM

Perfect, thank you, I have another question please, in the same lines as the operator. I see that the string equals the operator '='. What is assignment operator represented as?

pan64 02-23-2020 08:19 AM

you might want to use www.shellcheck.net to check your srcipt.
also please use [code]here comes your code[/code] tags to post scripts. That will make it more readable.

berndbausch 02-23-2020 08:25 AM

Quote:

Originally Posted by spalisetty (Post 6093278)
Perfect, thank you, I have another question please, in the same lines as the operator. I see that the string equals the operator '='. What is assignment operator represented as?

The assignment operator is =. Inside the [ ... ] construct, use -eq to check equality. For arithmetic comparisons, you can also use the double parentheses:
Code:

if ((count==11))
then echo it's equal
else echo it's not
fi

Note the absence of the $ sign. I think a single = works as well in this context. It could be that this is not POSIX-compliant.

chrism01 02-23-2020 10:38 PM

You should probably bookmark this https://www.tldp.org/LDP/abs/html/comparison-ops.html

spalisetty 02-23-2020 10:49 PM

Thank You Chris


All times are GMT -5. The time now is 12:25 PM.