might need to rewrite using gnu getopts instead of arg matching
[assignments.git] / assgn3 / process-pid.sh
index 0676b8b73b67cc44ade2cabcd119500641828ce0..38375037e84729c4fa81ee2fe432701839a12405 100755 (executable)
@@ -1,6 +1,36 @@
 #!/usr/bin/env bash
 
-if [ -z "$1" ] || [ "$1" = "-h" ] ; then
-       echo "usage: ./process-pid.sh [command] [pid]"
-       exit 1;
+USAGE="usage: ./process-pid.sh {[-h, -a], [-f, -e] <pid>}, -s <regexp>"
+
+# -h case
+if [[ "$1" -eq "-h" ]]; then
+       echo "$USAGE"; exit 0 
+
+# <pid> case
+elif [[ "$1" =~ ^-?[0-9]+$ ]]; then
+       if `ps $1 > /dev/null 2>&1`; then
+               echo "you win!"; exit 0
+
+       else
+               echo -ne "invalid pid\n$USAGE"; exit 1
+       fi
+
+# -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