1

I am need to convert BASH script to SH:

#!/bin/bash
upper_limit=1500
lower_limit=1
middle=750
while [[ $lower_limit != $middle ]]
do
ping -M do -s $middle -c 1 8.8.8.8 &> /dev/null
if [ $? == "0" ]
then
lower_limit=$middle
else
upper_limit=$middle
fi
middle=$(( ($upper_limit + $lower_limit) / 2 ))
done
echo $middle

When i am just change to #!/bin/sh i have an error:

./test.sh: 6: [[: not found

I can't understand, what is wrong. Thanks for help.

Gektor
  • 31
  • 3

1 Answers1

0

This might be what you want (I'm sticking to your style)

upper_limit=1500
lower_limit=1
middle=750
while ! [ $lower_limit = $middle ]
do
ping -M do -s $middle -c 1 8.8.8.8 > /dev/null 2>&1
if [ $? = "0" ]
then
lower_limit=$middle
else
upper_limit=$middle
fi
middle=$(( ($upper_limit + $lower_limit) / 2 ))
done
echo $middle
M. Nejat Aydin
  • 9,597
  • 1
  • 7
  • 17
  • This code is missing quite a lot of quoting. – Charles Duffy Sep 27 '22 at 17:16
  • @CharlesDuffy Yes. Was just sticking to the OP's style. For this particular case, quotes aren't needed since the variables are initialized and don't contain whitespaces or glob characters. But I'd definitely quote them anyway if I were to write this code from scratch. – M. Nejat Aydin Sep 27 '22 at 17:27