Add return code support to `pid`.
Test: pid nonexistent; echo $?
Test: pid --exact nonexistent; echo $?
Change-Id: If8efb1ca27ae3ed7b5be1c51179fae387c56e305
diff --git a/scripts/pid b/scripts/pid
index bb5d291..a9f55d3 100755
--- a/scripts/pid
+++ b/scripts/pid
@@ -14,22 +14,29 @@
# limitations under the License.
# Get the pid of processes matching a string.
-prepend=''
-append=''
+EXACT=0
if [ "$1" = "--exact" ]; then
- prepend=' '
- append='$'
+ EXACT=1
shift
fi
+
EXE="$1"
-if [ "$EXE" ] ; then
- PID=`adb shell ps \
- | tr -d '\r' \
- | \grep "$prepend$EXE$append" \
- | sed -e 's/^[^ ]* *\([0-9]*\).*$/\1/'`
- echo "$PID"
-else
+if ! [ "$EXE" ] ; then
echo "usage: pid [--exact] <process name>"
exit 255
fi
+if [ $EXACT == 1 ]; then
+ PIDS="$(adb shell pidof $EXE)"
+ RC=$?
+else
+ PIDS=$(adb shell "ps -o PID,NAME | tail -n +1 | grep $EXE | tr -s ' ' | cut -f2 -d' '")
+ [ -n "$PIDS" ]
+ RC=$?
+fi
+
+for PID in $PIDS; do
+ echo $PID
+done
+
+exit $RC