LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-23-2020, 07:36 AM   #1
spalisetty
LQ Newbie
 
Registered: Feb 2020
Location: India
Posts: 28

Rep: Reputation: Disabled
Smile 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

Last edited by spalisetty; 02-23-2020 at 07:42 AM.
 
Old 02-23-2020, 07:38 AM   #2
spalisetty
LQ Newbie
 
Registered: Feb 2020
Location: India
Posts: 28

Original Poster
Rep: Reputation: Disabled
Smile

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$
 
Old 02-23-2020, 07:45 AM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
You need white space after the [, otherwise the shell thinks you want to run a program named [10. Also white space before the ].
 
2 members found this post helpful.
Old 02-23-2020, 07:48 AM   #4
spalisetty
LQ Newbie
 
Registered: Feb 2020
Location: India
Posts: 28

Original Poster
Rep: Reputation: Disabled
Smile

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?
 
Old 02-23-2020, 08:19 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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.
 
1 members found this post helpful.
Old 02-23-2020, 08:25 AM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by spalisetty View Post
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.
 
Old 02-23-2020, 10:38 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You should probably bookmark this https://www.tldp.org/LDP/abs/html/comparison-ops.html
 
1 members found this post helpful.
Old 02-23-2020, 10:49 PM   #8
spalisetty
LQ Newbie
 
Registered: Feb 2020
Location: India
Posts: 28

Original Poster
Rep: Reputation: Disabled
Thank You Chris
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] No package 'x11' found No package 'xext' found No package 'xdamage' found No package 'xfixes' found No package 'x11-xcb' found Jigsaw Linux From Scratch 14 02-23-2021 08:35 PM
[SOLVED] ext4 with "Mount count" > "Maximum mount count" is not fscked catkin Linux - General 3 10-03-2013 07:34 AM
DBD::mysql::st execute failed: Column count doesn't match value count at row 1 shifter Programming 2 02-24-2010 07:42 PM
Need a way to count sub-directories and get a total count Mo-regard Linux - Newbie 1 08-14-2009 09:10 AM
Should posts in general count on your post count? Joey.Dale General 16 01-27-2004 01:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration