I'm in the process of making a very large UCI defaults script which completely automates the setup of my main WRT1900ACSv2 router from start to finish. I'm hoping to share it on the Wiki once it's complete as it may aid people trying to get their head around UCI commands and I think lots of people would find it useful for getting their router up and running quickly from factory resets/custom firmware images etc.
The reason I'm asking for help is because I've styled the script using ANSI escape codes and echo commands which details what's happening in the script. My Unix knowledge is basic so I'm hoping someone can offer some help so that can finish the script and add ideas etc.
By default I hide all output from commands by redirecting stdout and stderr file descriptors to /dev/null and then redirecting a new file descriptor 3 to stdout. It was my lazy way of not having to put &>/dev/null at the end of every line (there's over 1000 lines!)
# Hide all output by default
exec 3>&1 &>/dev/null
For an echo to show in the script output (stdout) I use >&3 at the end:
# Set default radio settings
echo -n "Configuring default radio settings... " >&3
uci batch <<-"EOF"
set wireless.radio0.beacon_int='400'
set wireless.radio0.txpower='23'
set wireless.radio0.legacy_rates='0'
set wireless.radio0.country='GB'
set wireless.radio0.noscan='1'
set wireless.radio0.channel='36'
set wireless.radio1.txpower='20'
set wireless.radio1.noscan='1'
set wireless.radio1.channel='1'
set wireless.radio1.beacon_int='200'
set wireless.radio1.legacy_rates='1'
set wireless.radio1.htmode='HT40'
set wireless.radio1.country='GB'
EOF
echo -e "Done" >&3
As shown in the example above you can see I enclose the echo commands around the UCI commands themselves. The problem I have is the script will always respond as "Done" no matter if the command has completed or not. For example, if I run the SQM UCI command on my stock OpenWrt firmware, the luci-sqm-app package is not installed by default therefore I will receive an error message when I run the command:
$ echo "SQM"
$ # Delete default SQM instance
$ echo -en "\tDeleting default SQM instance... "
$ uci del sqm.eth1
$ echo "Done"
uci: Entry not found
I've done some reading on the trap command but unfortunately it has gone straight over my head. From what I've seen it can capture error messages, print out precisely what line the error is at and print out a custom error message.
What I'm looking to do is place the trap command at the bottom of the UCI commands in place of the simple "Done" response and pickup whether an error can occurred since the first echo command (top of the UCI commands section). From here I would then use an IF ELSE statement to display success or error message dependent on the result.
For perspective here's a snippet of the script including the ANSI colour codes:
#!/bin/ash
#=======================================#
# Clear screen
clear
#=======================================#
# Hide all output by default
exec 3>&1 &>/dev/null
#=======================================#
# Define foreground and background colours
RED_FG="\033[0;31m" >&3
GREEN_FG="\033[0;32m" >&3
WHITE_FG="\033[1;97m" >&3 # Has bold styling
BLUE_BG="\033[44m" >&3
LIGHT_BLUE_BG="\033[104m" >&3
RC="\033[0;39m\033[0;49m" >&3 # Resets colour and style
# Define section styling
Section="${BLUE_BG}${WHITE_FG}" >&3
Config="${LIGHT_BLUE_BG}${WHITE_FG}" >&3
Success="${GREEN_FG}" >&3
Warning="${RED_FG}" >&3
#=======================================#
########################################
########## Clear/Set Defaults ##########
########################################
echo -e "${Section}# Clear/set defaults #${RC}" >&3
#==============================#
#---------- Wireless ----------#
#==============================#
echo -e "\t${Config}Wireless${RC}" >&3
# Set default radio settings
echo -en "\t> Configuring default radio settings... " >&3
uci batch <<-"EOF"
set wireless.radio0.beacon_int='400'
set wireless.radio0.txpower='23'
set wireless.radio0.legacy_rates='0'
set wireless.radio0.country='GB'
set wireless.radio0.noscan='1'
set wireless.radio0.channel='36'
set wireless.radio1.txpower='20'
set wireless.radio1.noscan='1'
set wireless.radio1.channel='1'
set wireless.radio1.beacon_int='200'
set wireless.radio1.legacy_rates='0'
set wireless.radio1.htmode='HT40'
set wireless.radio1.country='GB'
EOF
echo -e "${Success}Done${RC}" >&3
#============================#
#---------- System ----------#
#============================#
echo -e "\t${Config}System${RC}" >&3
# Set system settings
echo -en "\t> Configuring system settings... " >&3
uci batch <<-"EOF"
set system.@system[0].hostname='OpenWrt-AP1'
set system.@system[0].zonename='Europe/London'
set system.ntp.enable_server='1'
EOF
echo -e "${Success}Done${RC}" >&3
This will be useful for those who run this script or a variation of it and they forget to install a package and so a simple message like "Sorry this package doesn't seem be installed" could be displayed.
N.B Two things that spring to mind are, resetting the trap command so that it doesn't capture the entire script and display an error message for a different section of the script other than for the section it's supposed to be displaying for. Secondly I would have to allow stderr to come back through somewhere so the trap command can see it
2>&3
An example output could look like this:
Any ideas?