Bash-4.3 distribution sources and documentation
[platform/upstream/bash.git] / examples / functions / notify.bash
1 #
2 #  Chet Ramey <chet.ramey@case.edu>
3 #
4 #  Copyright 1992 Chester Ramey
5 #
6 #   This program is free software; you can redistribute it and/or modify
7 #   it under the terms of the GNU General Public License as published by
8 #   the Free Software Foundation; either version 2, or (at your option)
9 #   any later version.
10 #
11 #   TThis program is distributed in the hope that it will be useful,
12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #   GNU General Public License for more details.
15 #
16 #   You should have received a copy of the GNU General Public License
17 #   along with this program; if not, write to the Free Software Foundation,
18 #   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 trap _notify CHLD
21 NOTIFY_ALL=false
22 unset NOTIFY_LIST
23 unalias false
24
25 false()
26 {
27         return 1
28 }
29
30 _notify ()
31 {
32         local i j
33         local newlist=
34
35         if $NOTIFY_ALL
36         then
37                 return          # let bash take care of this itself
38         elif [ -z "$NOTIFY_LIST" ]; then
39                 return
40         else
41                 set -- $NOTIFY_LIST
42                 for i in "$@"
43                 do
44                         j=$(jobs -n %$i)
45                         if [ -n "$j" ]; then
46                                 echo "$j"
47                                 jobs -n %$i >/dev/null
48                         else
49                                 newlist="newlist $i"
50                         fi
51                 done
52                 NOTIFY_LIST="$newlist"
53         fi
54 }
55
56 notify ()
57 {
58         local i j
59
60         if [ $# -eq 0 ]; then
61                 NOTIFY_ALL=:
62                 set -b
63                 return
64         else
65                 for i in "$@"
66                 do
67                         # turn a valid job spec into a job number
68                         j=$(jobs $i)
69                         case "$j" in
70                         [*)     j=${j%%]*}
71                                 j=${j#[}
72                                 NOTIFY_LIST="$NOTIFY_LIST $j"
73                                 ;;
74                         esac
75                 done
76         fi
77 }