#!/bin/bash POSITIONAL=() while [[ $# -gt 0 ]] do key="$1" case $key in -m|--mac_address) mac_address="$2" shift # past argument shift # past value ;; -n|--device_name) device_name="$2" shift # past argument shift # past value ;; -t|--wait_time) wait_time="$2" shift # past argument shift # past value ;; *) # unknown option POSITIONAL+=("$1") # save it in an array for later shift # past argument ;; esac done set -- "${POSITIONAL[@]}" # restore positional parameters #echo "mac" #echo $mac_address #echo "device_name" #echo $device_name #exit time=30 if [[ -n "$wait_time" ]]; then time=$wait_time fi touch devices.txt bluetoothctl scan on > devices.txt & echo "Waiting for $time seconds." sleep $time #set your own delay here pkill bluetoothctl sleep 10 cat devices.txt if [[ -z "$mac_address" ]]; then echo "Device address argument was not set. Trying to find device address." #grep "ROBBO-R-" devices.txt if [[ -z "$device_name" ]]; then mac_addr=$(grep -m 1 "ROB-R-" devices.txt | cut -d " " -f3) if [[ -z "$mac_addr" ]]; then mac_addr=$(grep -m 1 "ROBBO-R-" devices.txt | cut -d " " -f3) fi else echo "Using device name to find corresponding address." mac_addr=$(grep -m 1 $device_name devices.txt | cut -d " " -f3) fi echo $mac_addr else echo "Using address agrument" mac_addr=$mac_address fi #bluetoothctl pair 60:8A:10:61:AF:19 bluetoothctl pair $mac_addr #sudo rfcomm connect rfcomm0 60:8A:10:61:AF:19 sudo rfcomm connect rfcomm0 $mac_addr