tests: factor 350 fail=0 initializations into test-lib.sh
[platform/upstream/coreutils.git] / tests / misc / nohup
1 #!/bin/sh
2 # test nohup
3
4 # Copyright (C) 2003, 2006-2009 Free Software Foundation, Inc.
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 3 of the License, or
9 # (at your option) any later version.
10
11 # This 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, see <http://www.gnu.org/licenses/>.
18
19 if test "$VERBOSE" = yes; then
20   set -x
21   nohup --version
22 fi
23
24 . $srcdir/test-lib.sh
25
26
27 nohup sh -c 'echo stdout; echo stderr 1>&2' 2>err || fail=1
28
29 # Be careful.  The results of the above nohup command
30 # change depending on whether stdin and stdout are redirected.
31 if test -t 1; then
32   test "`cat nohup.out`" = stdout || fail=1
33   if test -t 0; then
34     echo 'nohup: ignoring input and appending output to `nohup.out'\'
35   else
36     echo 'nohup: appending output to `nohup.out'\'
37   fi >exp || fail=1
38 else
39   # Here it should not even exist.
40   test -f nohup.out && fail=1
41   if test -t 0; then
42     echo 'nohup: ignoring input' >exp
43   else
44     rm -f exp
45   fi || fail=1
46 fi
47 echo 'stderr' >> exp || fail=1
48
49 compare exp err || fail=1
50 rm -f nohup.out err exp
51 # ----------------------
52
53 # Be careful.  The results of the following nohup command
54 # change depending on whether stderr is redirected.
55 nohup sh -c 'echo stdout; echo stderr 1>&2' >out || fail=1
56 if test -t 2; then
57   test "`cat out|tr '\n' -`" = stdout-stderr- || fail=1
58 else
59   test "`cat out|tr '\n' -`" = stdout- || fail=1
60 fi
61 # It must *not* exist.
62 test -f nohup.out && fail=1
63 rm -f nohup.out err
64 # ----------------------
65
66 # Bug present through coreutils 8.0: failure to print advisory message
67 # to stderr must be fatal.  Requires stdout to be terminal.
68 if test -w /dev/full && test -c /dev/full; then
69 (
70   exec >/dev/tty
71   test -t 1 || exit 0
72   nohup echo hi 2> /dev/full
73   test $? = 125 || fail=1
74   test -f nohup.out || fail=1
75   test -s nohup.out && fail=1
76   rm -f nohup.out
77   exit $fail
78 ) || fail=1
79 fi
80
81 nohup no-such-command 2> err
82 errno=$?
83 if test -t 1; then
84   test $errno = 127 || fail=1
85   # It must exist.
86   test -f nohup.out || fail=1
87   # It must be empty.
88   test -s nohup.out && fail=1
89 fi
90
91 cat <<\EOF > exp || fail=1
92 nohup: appending output to `nohup.out'
93 nohup: cannot run command `no-such-command': No such file or directory
94 EOF
95 # Disable these comparisons.  Too much variation in 2nd line.
96 # compare exp err || fail=1
97 rm -f nohup.out err exp
98 # ----------------------
99
100 touch k; chmod 0 k
101 nohup ./k 2> err
102 errno=$?
103 test $errno = 126 || fail=1
104 if test -t 1; then
105   # It must exist.
106   test -f nohup.out || fail=1
107   # It must be empty.
108   test -s nohup.out && fail=1
109 fi
110
111 cat <<\EOF > exp || fail=1
112 nohup: appending output to `nohup.out'
113 nohup: cannot run command `./k': Permission denied
114 EOF
115 # Disable these comparisons.  Too much variation in 2nd line.
116 # compare exp err || fail=1
117
118 # Make sure it fails with exit status of 125 when given too few arguments,
119 # except that POSIX requires 127 in this case.
120 nohup >/dev/null 2>&1
121 test $? = 125 || fail=1
122 POSIXLY_CORRECT=1 nohup >/dev/null 2>&1
123 test $? = 127 || fail=1
124
125 Exit $fail