Friday, October 7, 2022

[SOLVED] Convert BASH script to SH script

Issue

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.


Solution

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


Answered By - M. Nejat Aydin
Answer Checked By - Robin (WPSolving Admin)