I am working with a group on a GMRS repeater system in Rochester, NY (http://attica575.net) and we wanted to have a dynamic message sent out over the system. With the expertise of Brian Duff, we were able to create a script that looks at a specific folder and will randomly play one of the audio files store within.
#!/bin/bash
# SCRIPT NAME: random_annoucement.sh
# CREATED BY: Justin Grigg & Brian Duff
# CREATED ON: 11/22/2020
# SCRIPT PURPOSE: randomly selects a file and plays it as an annoucment
# SCRIPT PARAMETERS: add debug as a parameter and it will only display output
#
# -------------------------------------------------------------------------
# set script options below. Only edit between " "
# -------------------------------------------------------------------------
#
# set the path to the message folder.
annoucementFolder="/var/lib/asterisk/sounds/custom/announcements"
#
# set node number
nodeNumber="111111"
#
# set if a log should be created "on / off"
log_creation="on"
#
# log file location
log_location="/home/repeater/script_logs"
#
# name of log file (# will create a .txt file with this name. Do not add .txt)
log_file_name="announcement_tracker"
#
# announcement type (localplay = RF only / playback = rf & network)
announce_type="localplay"
#
# -------------------------------------------------------------------------
# please do not edit below this line
# -------------------------------------------------------------------------
# --------------------------
# MESSAGE CREATION
# --------------------------
# Get number of files in the directory
totalFiles="$(ls -1 ${annoucementFolder}|wc -l)"
# randomly selecting a number#
randomIndex="$(shuf -i1-${totalFiles} -n1)"
# building the full variable name
selected_message="$(ls -1 ${annoucementFolder}|awk "NR==${randomIndex}"|sed -e 's/\.wav//' -e 's/\.gsm//' -e 's/\.ulaw//')"
# --------------------------
# DEBUG
# --------------------------
# DEBUG: testing to ensure variable name worked correctly
if [[ $1 -eq "debug" ]]; then
echo -e "Total files in ${annoucementFolder}: ${totalFiles}\nRandom index: ${randomIndex}\nSelected message: ${selected_message}\nNode number: ${nodeNumber}"
fi
# --------------------------
# MESSAGE PLAY
# --------------------------
# IF > checks to see if script is running as a debug. Plays file if not
if [[$1 -ne "debug" ]]; then
## Play the message - ASTRISK COMMAND
/usr/sbin/asterisk -rx "rpt {$announce_type} ${nodeNumber} ${annoucementFolder}/${selected_message}"
fi
# --------------------------
# LOG CREATION
# --------------------------
# IF > checks to see if a log should be created
if [[$log_creation -eq "on" ]]; then
# timestamp used for logfile
timestamp=$(date)
# sets the path for the log
path=$log_location"/"$log_file_name".txt"
# writes the timestamp in the log
echo >> $path $timestamp
# writes the entry of what file was selected
echo -e "Total files in ${annoucementFolder}: ${totalFiles} - Selected message: ${selected_message}">> $path
# writes a line in the log for easier reading
echo ------------------ >> $path
fi