Hi
I am pretty new to bash scripting.I am trying to write the if statement in bash and it give me error. Can you please help me what I am doing wrong in If statement?
if [$DB_SIZE%$kk==0] && [$DB_SIZE/$kk<=$M] && [$DB_SIZE/$kk >= $divide]
then
fector=$kk;
divide=$DB_SIZE/$kk;
echo "factor value:$fector"
echo"divide value:$divide"
fi
line 1: [1024%2==0]: command not found
The numeric operations like / work in $(( )) .
Within [ ] and [[ ]] you do numerical comparison with -eq (equivalent), -gt (greater than), -le (less|equal), etc.
Within $(( )) you do not need to $ -prefix referenced variables.
if [[ $(( DB_SIZE % kk )) -eq 0 && $(( DB_SIZE / kk )) -le $M && $(( DB_SIZE / kk )) -ge $divide ]]
then
Also the shell can handle && . Sometimes this is better readable
if [[ $(( DB_SIZE % kk )) -eq 0 ]] && [[ $(( DB_SIZE / kk )) -le $M ]] && [[ $(( DB_SIZE / kk )) -ge $divide ]]
then
---------- Post updated at 12:22 ---------- Previous update was at 12:08 ----------
The previous should work in all Posix-compliant shells.
The following works in bash (and ksh and zsh I think):
if (( DB_SIZE % kk == 0 && DB_SIZE / kk <= M && DB_SIZE / kk >= divide ))
then