Home > Software design >  Bash script with arguments
Bash script with arguments

Time:09-12

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

CodePudding user response:

Didn't really spend much time but I found two main errors. I recommend using a proper IDE so that you don't have to spend much time debugging syntax issues.

  1. There should not be any spaces when doing an assignment. Note, no spaces around =
ip=$(ipconfig getifaddr en0)
  1. If condition with OR should be as follows. Note the spaces
  if [ "$1" == "android" ] || [ "$1" == "a" ]

function main(){

  local device=false

  check $@

  local ip="localhost"

  if $android
    then
      echo "Android true"
      if $device
        then
          echo "Androind and device true"
          echo "Running On Android Device"
          ip=$(ipconfig getifaddr en0)
        else
            echo "Androind true and device false"
          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(){
    echo "inside platform "
  if [ "$1" == "android" ] || [ "$1" == "a" ]; then 
    echo "android"
    android=true
  elif [ "$1" == "ios" ] || [ "$1" == "i" ]; then 
    echo "ios"
    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 $@

Running

─ bash test.sh -p i -d
inside platform 
ios
Running On IOS Device
IP: 192.168.1.113

╰─ bash test.sh -p a -d
inside platform 
android
Android true
Androind and device true
Running On Android Device
IP: 192.168.1.113
  • Related