1

Output 1:

Enter your value: 12
./testscript.sh: line 4: 12: command not found
Your value is more than 10

Output 2:

Enter your value: 5
./testscript.sh: line 4: 12: command not found
Your value is more than 10

I need to know what is wrong. My Linux test is just tomorrow:(

#!/bin/bash
echo -n "Enter your value: "
read value
if [$value -lt 10]
then
echo "Your value is less than 10"
else
echo "Your value is more than 10"
fi
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Matty
  • 77
  • 1
  • 9
  • 2
    `[` is a command name (try `ls /bin/[` — it often exists); it must be separated from the condition by spaces. The `]` argument must be an argument on its own, separated from the rest of the test condition. This question must be a duplicate of a few dozen to a few hundred questions — the difficulty, as ever, is finding a good one to use as the reference question. – Jonathan Leffler Jul 24 '14 at 14:46
  • `if [ $value -lt 10 ]` – ConcurrentHashMap Jul 24 '14 at 14:47
  • `[ "$value" -lt 10 ]`, rather; quotes are important. – Charles Duffy Jul 24 '14 at 15:13
  • ...btw, this is a FAQ -- seen in http://mywiki.wooledge.org/BashPitfalls#if_.5Bbar.3D.22.24foo.22.5D.3B_then_... and throughout SO. – Charles Duffy Jul 24 '14 at 15:15
  • (also, the syntax of `[ ]` has nothing to do with `if`; `[ ]` can be used without `if`, and `if` can be used without `[ ]`). – Charles Duffy Jul 24 '14 at 15:19

3 Answers3

2

your if statement should be as below. A space before ']' and space after '['

    if [ $value -lt 10 ]

EDIT

As per the comments

you can always add optional ; at the end of the line. The below script would work fine.

a=20;
if [ $a -gt 10 ];
then
echo "true";
else
echo "false";
fi

you need to put your condition in the [ condition ]. The below one does not work.

   ( condition )

It would say command not found. As '(' is not command. where as '[' is a command in order to check a condition

sun_dare
  • 1,146
  • 2
  • 13
  • 33
  • Another question, if I put a ; at the end. It seems like nothing has changed if [ $value -lt 10 ]; Does the ; matter? – Matty Jul 24 '14 at 14:51
  • 1
    @Matty; that's because the semicolon is optional at the end of a line. It becomes mandatory if you write `if [ "$value" -lt 10 ]; then echo Hi; else echo Lo; fi` (which can go all on one line). – Jonathan Leffler Jul 24 '14 at 14:52
  • you don't need a ; at the end. Why are you trying to add a ;. just curious. Its not necessary. – sun_dare Jul 24 '14 at 14:52
  • I read from some books where they put a ; in one of their examples. Plus I find it more logical to have a ; in a language, like C#, it makes it look more neat:) What is the difference between if ( ) and if [ ] ? – Matty Jul 24 '14 at 14:57
  • If you want to demonstrate something canonically correct, use quotes: `[ "$value" -lt 10 ]`. Otherwise, say, `value` could contain `1 = 1 -o 12`, and it would evaluate to true rather than (correctly) being an error. – Charles Duffy Jul 24 '14 at 15:16
2

And the more appropriate way is to use [[ ]] over [ ] when in Bash since you can avoid word splitting and pathname expansion with it. Other conditions can be added as well:

if [[ ! $value =~ [0-9]+ ]]; then
    echo "Invalid input."
elif [[ value -lt 10 ]]; then
    echo "Your value is less than 10."
elif [[ value -eq 10 ]]; then
    echo "Your value is 10."
else
    echo "Your value is more than 10."
fi
konsolebox
  • 72,135
  • 12
  • 99
  • 105
1

To throw yet another option into the mix: bash has (( ... )) -- arithmetic conditions:

if (( $value < 10 )); then ...

http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs

One thing you can do in bash with arithmetic expressions is to drop the $.

if (( value < 10 )); then 

This is documented a bit obscurely in Shell arithmetic: "Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax."

This may appeal to aficionados of C-like languages, but it's a bit out of step with the rest of the languages, and it doesn't apply to all variables (such as special paramaters ($#) and array elements (${foo[3]})).

glenn jackman
  • 238,783
  • 38
  • 220
  • 352