USAGE="usage: ./process-pid.sh {[-h, -a], [-f, -e] <pid>}, -s <regexp>"
-if [ -z "$1" ]; then
- echo "$USAGE"; exit 1
+# -h case
+if [[ "$1" -eq "-h" ]]; then
+ echo "$USAGE"; exit 0
-elif [[ "$1" =~ ^-?[0-9]+$ ]]; then # unportable :(
+# <pid> case
+elif [[ "$1" =~ ^-?[0-9]+$ ]]; then
if `ps $1 > /dev/null 2>&1`; then
echo "you win!"; exit 0
echo -ne "invalid pid\n$USAGE"; exit 1
fi
-elif [[ "$1" -eq "-h" ]]; then
- echo "$USAGE"; exit 0
+# -e <pid> case
+elif [[ "$1" -eq "-e" && "$2" =~ ^-?[0-9]+$ ]]; then
+ if `ps $2 > /dev/null 2>&1`; then
+ echo "you win!"; exit 0;
+
+ else
+ echo -ne "invalid pid\n$USAGE"; exit 1
+
+# -s <pid> <regex> case
+elif [[ "$1" -eq "-s" && "$2" =~ ^-?[0-9]+$ ]]; then
+ if `ps $2 > /dev/null 2>&1`; then
+ echo "you win!"; exit 0;
+
+ else
+ echo -ne "invalid pid\n$USAGE"; exit 1
+
+else
+ echo "$USAGE"; exit 1
fi