might need to rewrite using gnu getopts instead of arg matching
authorkremlin <ian@kremlin.cc>
Tue, 16 Sep 2014 21:26:28 +0000 (17:26 -0400)
committerkremlin <ian@kremlin.cc>
Tue, 16 Sep 2014 21:26:28 +0000 (17:26 -0400)
assgn3/process-pid.sh

index daa6624cfca31272c6b9f9160d132e93f86db834..38375037e84729c4fa81ee2fe432701839a12405 100755 (executable)
@@ -2,10 +2,12 @@
 
 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
 
@@ -13,6 +15,22 @@ elif [[ "$1" =~ ^-?[0-9]+$ ]]; then # unportable :(
                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