1#!/bin/bash 2 3# Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com> 4# Copyright 2013 Robin Mittal <robinmittal.it@gmail.com> 5 6#cleaning 'yes' processes 7killall yes >/dev/null 2>&1 8 9[ -f testing.sh ] && . testing.sh 10 11#testing "name" "command" "result" "infile" "stdin" 12 13# Starting processes to test pgrep command 14yes >/dev/null & 15proc=$! 16echo "# Process created with id: $proc" 17sleep 1 18session_id=0 19proc_parent=`cat /proc/${proc}/stat | awk '{ print $4 }'` 20echo "# Parent Process id of $proc is $proc_parent" 21 22# Testcases for pgrep command 23testing "pgrep pattern" "pgrep yes" "$proc\n" "" "" 24testing "pgrep wildCardPattern" "pgrep ^y.*s$" "$proc\n" "" "" 25testing "pgrep -l pattern" "pgrep -l yes" "$proc yes\n" "" "" 26testing "pgrep -f pattern" "pgrep -f yes" "$proc\n" "" "" 27testing "pgrep -n pattern" "pgrep -n yes" "$proc\n" "" "" 28testing "pgrep -o pattern" "pgrep -o yes" "$proc\n" "" "" 29testing "pgrep -s" "pgrep -s $session_id yes" "$proc\n" "" "" 30testing "pgrep -P" "pgrep -P $proc_parent yes" "$proc\n" "" "" 31 32#Clean-up 33killall yes >/dev/null 2>&1 34 35# Testcases for pkill command 36 37yes >/dev/null & 38sleep 1 39testing "pkill pattern" "pkill yes && sleep 1 && (pgrep yes || echo 'yes')" \ 40 "yes\n" "" "" 41killall yes >/dev/null 2>&1 42 43yes >/dev/null & 44yes print1 >/dev/null & 45yes print2 >/dev/null & 46sleep 1 47testing "pkill pattern (multiple)" "pkill yes && sleep 1 && 48 (pgrep yes || echo 'yes')" "yes\n" "" "" 49killall yes >/dev/null 2>&1 50 51yes >/dev/null & 52sleep 1 53testing "pkill -f pattern (one)" "pkill -f yes && sleep 1 && 54 (pgrep yes || echo 'yes')" "yes\n" "" "" 55killall yes >/dev/null 2>&1 56 57yes print1 >/dev/null & 58sleep 1 59testing "pkill -f pattern args" "pkill -f \"yes print1\" && sleep 1 && 60 (pgrep yes || echo 'yes')" "yes\n" "" "" 61killall yes >/dev/null 2>&1 62 63yes >/dev/null & 64yes print1 >/dev/null & 65yes print2 >/dev/null & 66sleep 1 67testing "pkill -f pattern (multiple)" "pkill -f yes && sleep 1 && 68 (pgrep yes || echo 'yes')" "yes\n" "" "" 69killall yes >/dev/null 2>&1 70 71yes >/dev/null & 72sleep 1 73testing "pkill -s 0 -f pattern (regexp)" "pkill -s 0 -f ye* && sleep 1 && 74 (pgrep yes || echo 'yes')" "yes\n" "" "" 75killall yes >/dev/null 2>&1 76 77yes >/dev/null & 78proc1=$! 79yes >/dev/null & 80proc2=$! 81sleep 1 82testing "pkill -n pattern" "pkill -n yes && sleep 1 && pgrep yes" \ 83 "$proc1\n" "" "" 84killall yes >/dev/null 2>&1 85 86yes >/dev/null & 87proc1=$! 88yes >/dev/null & 89proc2=$! 90sleep 1 91testing "pkill -o pattern" "pkill -o yes && sleep 1 && pgrep yes" \ 92 "$proc2\n" "" "" 93killall yes >/dev/null 2>&1 94 95yes >/dev/null & 96sleep 1 97testing "pkill -s (blank) pattern" "pkill -s '' yes && sleep 1 && 98 (pgrep yes || echo 'yes')" "yes\n" "" "" 99killall yes >/dev/null 2>&1 100 101yes >/dev/null & 102sleep 1 103testing "pkill -s 0 pattern" "pkill -s 0 yes && sleep 1 && 104 (pgrep yes || echo 'yes')" "yes\n" "" "" 105killall yes >/dev/null 2>&1 106 107yes >/dev/null & 108proc=$! 109proc_p=`cat /proc/${proc}/stat | awk '{ print $4 }'` 110sleep 1 111testing "pkill -P parent_prodId pattern" "pkill -P $proc_p yes && sleep 1 && 112 (pgrep yes || echo 'yes')" "yes\n" "" "" 113killall yes >/dev/null 2>&1 114 115yes >/dev/null & 116proc=$! 117proc_parent=`cat /proc/${proc}/stat | awk '{ print $4 }'` 118sleep 1 119testing "pkill -9 pattern" "pkill -9 yes && sleep 1 && 120 (pgrep yes || echo 'yes')" "yes\n" "" "" 121killall yes >/dev/null 2>&1 122 123