• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 and no >/dev/null &
15proc=$!
16#echo "# Process created with id: $proc"
17sleep .1
18session_id=0
19proc_parent=`cat /proc/${proc}/stat | cut -d' ' -f4`
20#echo "# Parent Process id of $proc is $proc_parent"
21
22# Testcases for pgrep command
23testing "pattern" "pgrep yes" "$proc\n" "" ""
24testing "wildCardPattern" "pgrep ^y.*s$" "$proc\n" "" ""
25testing "-l pattern" "pgrep -l yes" "$proc yes\n" "" ""
26testing "-f pattern" "pgrep -f yes" "$proc\n" "" ""
27testing "-a pattern" "pgrep -a yes" "$proc yes and no\n" "" ""
28testing "-la pattern" "pgrep -la yes" "$proc yes and no\n" "" ""
29testing "-fa pattern" "pgrep -fa yes" "$proc yes and no\n" "" ""
30testing "-lf pattern" "pgrep -lf yes" "$proc yes\n" "" ""
31testing "-fa pattern" "pgrep -fa yes" "$proc yes and no\n" "" ""
32testing "-lfa pattern" "pgrep -lfa yes" "$proc yes and no\n" "" ""
33testing "-n pattern" "pgrep -n yes" "$proc\n" "" ""
34testing "-o pattern" "pgrep -o yes" "$proc\n" "" ""
35testing "-s" "pgrep -s $session_id yes" "$proc\n" "" ""
36testing "-P" "pgrep -P $proc_parent yes" "$proc\n" "" ""
37
38testing "-f 'full command line'" "pgrep -f 'yes and no'" "$proc\n" "" ""
39testing "-l 'full command line nomatch'" "pgrep -l 'yes and no'" "" "" ""
40testing "-a 'full command line nomatch'" "pgrep -a 'yes and no'" "" "" ""
41
42testing "return success" "pgrep yes && echo success" "$proc\nsuccess\n" "" ""
43testing "return failure" "pgrep almost-certainly-not || echo failure" \
44    "failure\n" "" ""
45
46#Clean-up
47kill -9 $proc
48