From aa363c849ccb2e3b5346eac9cc3631b0a0c13119 Mon Sep 17 00:00:00 2001 From: kremlin Date: Thu, 18 Sep 2014 16:42:35 -0400 Subject: [PATCH] rewrote with getopts, done --- assgn3/process-pid.sh | 96 +++++++++++++++++++++++++++++++------------ 1 file changed, 69 insertions(+), 27 deletions(-) diff --git a/assgn3/process-pid.sh b/assgn3/process-pid.sh index 3837503..30d57c7 100755 --- a/assgn3/process-pid.sh +++ b/assgn3/process-pid.sh @@ -1,36 +1,78 @@ #!/usr/bin/env bash -USAGE="usage: ./process-pid.sh {[-h, -a], [-f, -e] }, -s " +USAGE="usage: $0 [-h, -a] [-f, -e] [-s ]" +OPTIND=2 -# -h case -if [[ "$1" -eq "-h" ]]; then - echo "$USAGE"; exit 0 +if [[ ! "$1" =~ ^-?[0-9]+$ ]]; then + echo $USAGE; exit 1 -# case -elif [[ "$1" =~ ^-?[0-9]+$ ]]; then - if `ps $1 > /dev/null 2>&1`; then - echo "you win!"; exit 0 +elif [[ ! -d /proc/$1 ]]; then + echo "invalid pid '$1'"; exit 1 +fi - else - echo -ne "invalid pid\n$USAGE"; exit 1 - fi +echo -e "pid '${1}' refers to a running process.\n" -# -e case -elif [[ "$1" -eq "-e" && "$2" =~ ^-?[0-9]+$ ]]; then - if `ps $2 > /dev/null 2>&1`; then - echo "you win!"; exit 0; +while getopts ":s:hafe" opts; do + case "${opts}" in + s) + cat /proc/"$1"/status | grep "$OPTARG" + ;; + h) + echo "$USAGE"; DROPBOOL=1 + ;; + a) + echo "written by ian sutton."; if [[ $DROPBOOL ]]; then exit 0; fi + ;; + f) + ls /proc/"$1" + ;; + e) + if [[ ! `readlink /proc/$1/exe` ]]; then + echo "no permissions to read pid '$1's executable path"; exit 1 + fi - else - echo -ne "invalid pid\n$USAGE"; exit 1 + echo -n "executable path of pid '$1': " + readlink /proc/"$1"/exe + ;; + \?) + echo "bad option \"-$OPTARG\""; exit 1 + ;; + :) + echo $USAGE; exit 1 + ;; + esac + echo -ne "\n" +done +# -h case +#if [[ "$1" -eq "-h" ]]; then +# echo "$USAGE"; exit 0 +# +# 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 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 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 +#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 -- 2.41.0