site  contact  subhomenews

ash and bash glob wildcard expansion gotcha

February 12, 2020 — BarryK

One of the scripts in EasyOS did not work as expected. It is code that I added today to /etc/rc.d/rc.services:

if [ "${baseSC}" == "messagebus" ];then
echo -n "started" > /tmp/pup_event_backend/dbus_ #permanent flag (see rc.services_ipc).
for aSC in /tmp/pup_event_ipc/dbus_* #rc.services_ipc should have already created this.
do
[ "$aSC" == "" ] && continue
[ ! -f $aSC ] && continue #200212b if no match for glob, ash uses literal filename.
echo -n "started" > ${aSC}
done
fi
fi

What this script did, as there were no files meeting the glob expansion of "/tmp/pup_event_backend/dbus_*", is fallback to using the literal name, "/tmp/pup_event_backend/dbus_*", and it wrote "started" to a file of that name, with a "*" in the actual filename!!!!

Oh man, that is awful. Having a file with a "*" in it is something you would never do, it is asking for trouble. Yet the above behaviour is the default for both bash and ash.

There is a fix, see the insert in red above. Test if the file exists.

Bash has another way to fix it, insert "shopt -s nullglob" at the beginning of the script, or in ~/.bashrc. Busybox ash does not have any way to modify this default behaviour.

I am gobsmacked that this is the default behaviour. 

Tags: easy