首页 > 解决方案 > Basic if else scripting

问题描述

I hope you all are doing great. I am new to Linux shell scripting. I am trying to learn basic shell scripting and i started with with if else condition and I am stuck in it since a week. I have read as many articles as I could but I am not understanding what is the problem here. I know this must a very basic for you guys but please help me :)

Here is the script that I wrote

#!/bin/sh

count=102

if [$count -gt 100]

then

echo "$count is greater than 100"

else

echo "$count is less than 100"

fi

When I execute this script, I am getting below error.

./count.sh: line 5: [102: command not found
102 is less than 100

*I also tried taking user input not just with integer but with string also, still getting the same error. please help :)

Thank you in advance

标签: linuxshellscripting

解决方案


you need to provide space after [ and before ]. For e.g

[ $count -gt 100 ]

Sample code :

count=102
if [ $count -gt 100 ]
then
echo "$count is greater than 100"
else
echo "$count is less than 100"
fi

推荐阅读