# 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 (opens new window) 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

# LFTP

lftp is a file transfer program that which allows us to manage files and upload them to remove servers via ftp and other protocols.

To get started we recommend creating subdirectories on a remote server using the device IMEI, followed by a subfolder for the type of media you want to upload:

# remote ftp server
root/
├── 867698041101234/
│   ├── images/
│   │   ├── 1616442180-photo.jpeg
│   ├── videos/
│   │   ├── 1616442284-driver_cam_hard_braking.mp4
├── 867698041105678/
│   ├── images/
│   ├── videos/
│   ├── logs/

With the above in place you can upload media like this

# upload a photo
lftp -u username,password -p 4455 -e 'put /data/users/syrus4g/fatigue_sensor/1620415933-fatigue_remind.jpeg -o 867698041101234/images/1620415933-fatigue_remind.jpeg; bye' ftp://ftp.mysite.com

# upload a video
lftp -u username,password -p 4455 -e 'put /data/users/syrus4g/video/1624969585_hard_braking_driver_cam.mp4 -o 867698041101234/videos/1624969585_hard_braking_driver_cam.mp4; bye' ftp://ftp.mysite.com

lftp includes a command for mirroring the contents of a source folder with the remote server, make sure to add a -c option to continue the file transfer in case there's an interruption. Also you can use the --reverse flag to put all files from the Syrsu to the remote server, including subdirectories and their contents.

# reverse mirror images to destination server
lftp -u username,password -p 4455 -e 'mirror -c --reverse /data/users/syrus4g/fatigue_sensor/ 867698041101234/images/; bye' ftp://ftp.mysite.com

# reverse mirror videos to destination server
lftp -u username,password -p 4455 -e 'mirror -c --reverse /data/users/syrus4g/video/ 867698041101234/videos/; bye' ftp://ftp.mysite.com

once mirrored you can use the -n option to upload only newer files

# upload new images only
lftp -u username,password -p 4455 -e 'mirror -n --reverse /data/users/syrus4g/fatigue_sensor/ 867698041101234/images/; bye' ftp://ftp.mysite.com

# upload new videos only
lftp -u username,password -p 4455 -e 'mirror -n --reverse /data/users/syrus4g/video/ 867698041101234/videos/; bye' ftp://ftp.mysite.com

another useful option is to force ssl, which refuses to send the password in clear when the remote server does not support ssl

# force ssl connection to remote server
lftp -u username,password -p 4455 -e 'set ftp:ssl-force=true put /data/users/syrus4g/fatigue_sensor/1616442180-photo.jpeg 867698041101234/images/1616442180-photo.jpeg; bye' ftp://ftp.mysite.com
Last Updated: 6/29/2021, 10:17:18 PM