7

I tried different variants

echo $0
echo $shell
echo $SHELL
ps -p $$

but none of them can give a distinctive output. I can do something like this but it's ugly and hackish:

if ls --help 2>&1 | grep BusyBox; then
    echo "it is BusyBox"
else
    echo "it is NOT BusyBox"
fi
svlasov
  • 9,923
  • 2
  • 38
  • 39
  • I find that there's no accurate way to do that. Your best luck is http://stackoverflow.com/questions/3327013/how-to-determine-the-current-shell-im-working-on. And @Vality's answer. Question: Is it only meant to run in Linux systems? – konsolebox Jul 22 '14 at 09:58
  • I have to give up then, at least now I know there is no better way. Ye, I need it for Linux only, appropriate tag added. – svlasov Jul 22 '14 at 10:01
  • If it's only for Linux I can give another suggestion. – konsolebox Jul 22 '14 at 10:02

3 Answers3

6

Another way requiring Linux and readlink:

#!/bin/ash
exe=`exec 2>/dev/null; readlink "/proc/$$/exe"`
case "$exe" in
*/busybox)
    echo "It's a busybox shell."
    ;;
esac
konsolebox
  • 72,135
  • 12
  • 99
  • 105
2

Personally I favour:

if ps ax -o pid,comm | grep `echo $$` | grep busybox ; then
    echo "it is BusyBox"
fi

Which is a fair check to ensure you are running busybox shell.

This works by having ps generate a list of pids vs program names then finding our pid and checking if the program name contains busybox.

Vality
  • 6,577
  • 3
  • 27
  • 48
  • 2
    BusyBox version of ps doesn't seem to support arguments. – svlasov Jul 22 '14 at 09:44
  • @svlasov Strange, my busybox seemed to work file with that, it supports only `-o` and `-T`. (I have tested the command on my own system running BusyBox v1.22.1.) Perhaps you need to update? Is your BusyBox very old? – Vality Jul 22 '14 at 09:47
  • I have v1.20.2 but I'm not sure if it's possible to install a newer version. – svlasov Jul 22 '14 at 09:54
  • @svlasov You could probably use /bin/ps instead of ps to force it to not use the built-in. You could then do an error check so if ps does not exist it will assume busybox. However what distro are you using? Many provide up to date busyboxes. – Vality Jul 22 '14 at 09:59
  • It is a Sitara board from TI. My /bin/ps is a symlink to /bin/busybox. By chance I can be running full bash or zsh, so relying on file check is not accurate. – svlasov Jul 22 '14 at 10:06
  • http://tinderbox.dev.gentoo.org/default-linux/arm/armv7a-hardfloat-linux-gnueabi/unstable/sys-apps/busybox-1.22.1.tbz2 Have a new busybox. (should run on your board.) If you like this should let you use the newer commands. – Vality Jul 22 '14 at 10:12
1

Might not be perfect but this is how I check:

test -h /bin/ls # busybox or other single binary system
test -h /bin/ls && test `readlink /bin/ls` = busybox # it is busybox