Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to if/else statement in shell script

I'm receiveing an error on a simple script when using if/else statement.
The code:

#!/bin/sh
count=100
if [$count > 3]; then
echo "Test IF"
fi

The error: /bin/ash: line 6: [100: not found

like image 318
Shad Reese Avatar asked Apr 17 '26 12:04

Shad Reese


1 Answers

#!/bin/sh 

count=100;
if [ "$count" -gt 3 ]; then
  echo "Test IF";
fi

Correct your syntax: spaces must be used around [ and ], parameter expansions must be quoted, and -gt is appropriate for numeric comparisons inside of [ ]. > in sh is used as redirection operator; if you want to use it in arithmetical comparison, you must use the bash-only syntax

$(( $count > 3 ))
like image 158
Piotr Wadas Avatar answered Apr 19 '26 07:04

Piotr Wadas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!