I am not experienced with bash. I am trying to write a bash script on mac to help with running flutter development environment. I am using flag options to select the type of the environment and then at the end the script shall tell flutter what ip address to use.
function main(){
check $@
local ip = "localhost"
if [ $android ]
then
if [ $device ]
then
echo "Running On Android Device"
$ip = $(ipconfig getifaddr en0)
else
echo "Running On Android Simulator"
$ip = "10.0.2.2"
fi
else
if [ $device ]
then
echo "Running On IOS Device"
$ip = $(ipconfig getifaddr en0)
else
echo "Running On IOS Simulator"
$ip = "localhost"
fi
fi
echo "IP: $ip"
flutter run --dart-define="IP=$ip"
}
function check(){
local OPTIND opt i
while getopts ":p:d" opt; do
case $opt in
p) platform $OPTARG;;
d) device;;
/?) help;exit 1 ;;
:) echo "Option -$OPTARG requires an argument.";exit 1 ;;
*) echo "Invalid option: -$OPTARG";exit 1 ;;
esac
done
}
function platform(){
if ["$1" == "android"] || ["$1" == "a"]
then android = true
elif ["$1" == "ios"] || ["$1" == "i"]
then android = false
else echo "Invalid platform, running on android by default"
fi
}
function device(){
device = true
}
function help(){
echo "This script only accepts the following options:"
echo "-p [android|a|ios|i] : select android platform"
echo "-d : run on actual device"
}
main $@
But this script fails and not sure why:
% bash run.sh -p a -d
run.sh: line 50: [a: command not found
run.sh: line 50: [a: command not found
run.sh: line 52: [a: command not found
run.sh: line 52: [a: command not found
Invalid platform, running on android by default
zsh: segmentation fault bash run.sh -p a -d