iPhone and Linux

Wednesday, March 3, 2010

whereis

Another simple script to replace the missing whereis command on the iPhone.
#! /bin/sh
if
ls /usr/bin/$1 2>/dev/null
then echo >/dev/null
elif
ls /usr/local/bin/$1 2>/dev/null
then echo >/dev/null
elif
ls /usr/sbin/$1 2>/dev/null
then echo >/dev/null
elif
ls /sbin/$1 2>/dev/null
then echo >/dev/null
elif
ls /bin/$1 2>/dev/null
then echo >/dev/null
else
echo "$1 not found in /bin /sbin /usr/bin /usr/sbin /usr/local/bin"
fi
Too bad an if statement needs a then statement to go with it. Since ls has it's own output, there's no reason to duplicate it so it's easier to use the ls output and do an empty then command. The result works, but it just looks dumb. Here's a shorter version. It will list all the files. If any are not found, the error goes to /dev/null.
#! /bin/sh
ls /usr/bin/$1 /usr/local/bin/$1 /usr/sbin/$1 /sbin/$1 /bin/$1 2>/dev/null
But that won't give you a "not found" message. So let's take that and put it in a variable. If the variable is not empty, it will echo the variable, which is the non-error output from the ls command. If it ends up empty, the -n test will fail so the && command is skipped and the || command executes.
#! /bin/sh
test=$(ls /usr/bin/$1 /usr/local/bin/$1 /usr/sbin/$1 /sbin/$1 /bin/$1 2>/dev/null)
[ -n "$test" ] && echo $test || echo "$1 not found in /bin /sbin /usr/bin /usr/sbin /usr/local/bin"

Blog Archive