# Bash Scripting & Tools

Bash scripts can be used for various purposes such as executing specific commands, automating tasks, provisioning, etc; including the ability to consume apx- system tools. This is useful when you want to automate things such as scanning and pairing nearby bt speakerphones, or authorize a particular network, perform a health check on the device, and more.

# Bash script

To write a bash script you can create a new file using vi or nano.

$ nano bash_example.sh

and enter the following contents

#!/bin/bash
echo "my first bash script"

then change the script to be executable with the

$ chmod +x bash_example.sh

Execute the script with ./bash_example.sh

Now change the echo for a system tool command such as activating output 1

#!/bin/bash
sudo apx-io set OUT1 true
$ ./bash_example.sh

and if you do a sudo apx-io get out1 you'll see it returns "true"

# Crontab

Crontab is a tool that is used for running specific tasks on a regular interval. It can be useful to run tasks periodically like resetting the wifi or activating an output.

To start using crontab you can use the crontab -e command

$ crontab -e

This will pull up an editor with a list of all the currently running crons.

The format for cron is as follow:

* * * * * /path/to/script.sh
│ │ │ │ └───> Day of Week (0-7) (0 & 7 are "Sunday")
│ │ │ └─────> Month of Year (1-12)
│ │ └───────> Day of Month (1-31)
│ └─────────> Hour (0-23)
└───────────> Minute (0-59)

For example, if we want to reset the wifi every 1hr, we can accomplish this with a bash script like:

wifi_reset.sh

#!/bin/bash
sudo apx-wifi reset

and execute it with crontab:

*/10 * * * * /data/users/syrus4g/wifi_reset.sh

Another example could be to start the hotspot on every system reboot, to accomplish this we could create a script like this

start_hotspot.sh

#!/bin/bash
sudo apx-hotspot ssid Syrus4G
sudo apx-hotspot pass 123456

and execute it with crontab

@reboot /data/users/syrus4g/start_hotspot.sh
Last Updated: 4/30/2021, 10:30:25 AM