30d57c75e0b96fcd31fde4343e7f02e0584b9dec
[assignments.git] / assgn3 / process-pid.sh
1 #!/usr/bin/env bash
2
3 USAGE="usage: $0 <pid> [-h, -a] [-f, -e] [-s <regexp>]"
4 OPTIND=2
5
6 if [[ ! "$1" =~ ^-?[0-9]+$ ]]; then
7 echo $USAGE; exit 1
8
9 elif [[ ! -d /proc/$1 ]]; then
10 echo "invalid pid '$1'"; exit 1
11 fi
12
13 echo -e "pid '${1}' refers to a running process.\n"
14
15 while getopts ":s:hafe" opts; do
16 case "${opts}" in
17 s)
18 cat /proc/"$1"/status | grep "$OPTARG"
19 ;;
20 h)
21 echo "$USAGE"; DROPBOOL=1
22 ;;
23 a)
24 echo "written by ian sutton."; if [[ $DROPBOOL ]]; then exit 0; fi
25 ;;
26 f)
27 ls /proc/"$1"
28 ;;
29 e)
30 if [[ ! `readlink /proc/$1/exe` ]]; then
31 echo "no permissions to read pid '$1's executable path"; exit 1
32 fi
33
34 echo -n "executable path of pid '$1': "
35 readlink /proc/"$1"/exe
36 ;;
37 \?)
38 echo "bad option \"-$OPTARG\""; exit 1
39 ;;
40 :)
41 echo $USAGE; exit 1
42 ;;
43 esac
44 echo -ne "\n"
45 done
46
47 # -h case
48 #if [[ "$1" -eq "-h" ]]; then
49 # echo "$USAGE"; exit 0
50 #
51 # <pid> case
52 #elif [[ "$1" =~ ^-?[0-9]+$ ]]; then
53 # if `ps $1 > /dev/null 2>&1`; then
54 # echo "you win!"; exit 0
55 #
56 # else
57 # echo -ne "invalid pid\n$USAGE"; exit 1
58 # fi
59 #
60 # -e <pid> case
61 #elif [[ "$1" -eq "-e" && "$2" =~ ^-?[0-9]+$ ]]; then
62 # if `ps $2 > /dev/null 2>&1`; then
63 # echo "you win!"; exit 0;
64 #
65 # else
66 # echo -ne "invalid pid\n$USAGE"; exit 1
67 #
68 # -s <pid> <regex> case
69 #elif [[ "$1" -eq "-s" && "$2" =~ ^-?[0-9]+$ ]]; then
70 # if `ps $2 > /dev/null 2>&1`; then
71 # echo "you win!"; exit 0;
72 #
73 # else
74 # echo -ne "invalid pid\n$USAGE"; exit 1
75 #
76 #else
77 # echo "$USAGE"; exit 1
78 #fi