fix renames
[assignments.git] / homework / assgn3 / process-pid.sh
CommitLineData
bfcad051 1#!/usr/bin/env bash
2
aa363c84 3USAGE="usage: $0 <pid> [-h, -a] [-f, -e] [-s <regexp>]"
4OPTIND=2
8fbe044c 5
aa363c84 6if [[ ! "$1" =~ ^-?[0-9]+$ ]]; then
7 echo $USAGE; exit 1
8fbe044c 8
aa363c84 9elif [[ ! -d /proc/$1 ]]; then
10 echo "invalid pid '$1'"; exit 1
11fi
8fbe044c 12
aa363c84 13echo -e "pid '${1}' refers to a running process.\n"
8fbe044c 14
aa363c84 15while 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
259095d5 33
aa363c84 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"
45done
259095d5 46
aa363c84 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#
259095d5 68# -s <pid> <regex> case
aa363c84 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