3

In my case, I want to see if busybox has "md5sum" builtin.

I am currently doing this:

$ echo | busybox md5sum &>/dev/null && echo yes || echo no

I have not been able to find any information as to whether there is anything built into busybox to query what features are available programatically.

Yes, I can list the available applets by running it with no arguments, but trying to grep the output would be error prone and there is no guarantee as to whether grep will be available.

Zhro
  • 957

3 Answers3

3

Thanks for the push, Micah. It got my creative juices flowing.

Updated:

Tested on Bash 3/4, all builtins, no depedencies:

Portability: 100% compatible with Bash 3 and Bash 4 only

function _busybox_has() {
   builtin command -v busybox >/dev/null ||
      return 1

Sanitize searches for '[' and '[['

a=$1 a=${a//[/\[}

[[ $(busybox) =~ [:space:]([,]|$) ]] || return 1 }

No bashisms, tested on Dash:

Portability: Portable on all sh with sed/egrep

_busybox_has() {
   busybox >/dev/null 2>&1 ||
      return 1

Sanitize searches for '[' and '[['

a=$(echo "$1" | sed 's/[[]/\[/g')

busybox | egrep -oq "[:space:]([,]|$)" || return 1 }

No bashisms, grep -e instead of egrep (more portable), tested on Dash:

Portability: Portable on all sh with sed/grep -e

_busybox_has() {
   busybox >/dev/null 2>&1 ||
      return 1

Sanitize searches for '[' and '[['

a=$(echo "$1" | sed 's/[[]/\[/g')

busybox | grep -oqe "[[:space:]]($a)([,]|$)" || return 1 }

To test:

_busybox_has md5sum && echo yes || echo no
Zhro
  • 957
2

If I type # busybox with no parameters I get a list of configured commands that are possible.

Depending on your environment you could then parse this string. Grep is mentioned, but lacking that option, I would approach it via the string parsing tools of my environment:

bash:

options=$('busybox');

if [[ $options == *command* ]]
then
  echo "It's there!";
fi

if you're using another language there's usually something appropriate.

Micah
  • 46
0
busybox --list | busybox grep -qF md5sum && echo yes || echo no

(Tested with Busybox v1.35.0 in Debian 12.)

A shell function that implements the above solution:

_busybox_has () { busybox --list | busybox grep -qxF "$1"; }

Usage:

_busybox_has md5sum && echo yes || echo no
_busybox_has kitchensink && echo yes || echo no

there is no guarantee as to whether grep will be available

grep is so useful, it would be a peculiarity not to have it. Just in case, the following shell function implements _busybox_has using just busybox --list and features of the POSIX sh (not even [):

_busybox_has () {
busybox --list | ( while IFS= read -r line; do
   case "$line" in
      "$1") return 0
         ;;
   esac
done
return 1 )
}