might need to rewrite using gnu getopts instead of arg matching
[assignments.git] / assgn3 / process-pid.sh
CommitLineData
bfcad051 1#!/usr/bin/env bash
2
8fbe044c 3USAGE="usage: ./process-pid.sh {[-h, -a], [-f, -e] <pid>}, -s <regexp>"
4
259095d5 5# -h case
6if [[ "$1" -eq "-h" ]]; then
7 echo "$USAGE"; exit 0
8fbe044c 8
259095d5 9# <pid> case
10elif [[ "$1" =~ ^-?[0-9]+$ ]]; then
8fbe044c 11 if `ps $1 > /dev/null 2>&1`; then
12 echo "you win!"; exit 0
13
14 else
15 echo -ne "invalid pid\n$USAGE"; exit 1
16 fi
17
259095d5 18# -e <pid> case
19elif [[ "$1" -eq "-e" && "$2" =~ ^-?[0-9]+$ ]]; then
20 if `ps $2 > /dev/null 2>&1`; then
21 echo "you win!"; exit 0;
22
23 else
24 echo -ne "invalid pid\n$USAGE"; exit 1
25
26# -s <pid> <regex> case
27elif [[ "$1" -eq "-s" && "$2" =~ ^-?[0-9]+$ ]]; then
28 if `ps $2 > /dev/null 2>&1`; then
29 echo "you win!"; exit 0;
30
31 else
32 echo -ne "invalid pid\n$USAGE"; exit 1
33
34else
35 echo "$USAGE"; exit 1
bfcad051 36fi