Tizen 2.0 Release
[external/tizen-coreutils.git] / tests / misc / nohup
1 #!/bin/sh
2 # test nohup
3
4 # Copyright (C) 2003, 2006 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 2 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, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21 if test "$VERBOSE" = yes; then
22   set -x
23   nohup --version
24 fi
25
26 . $srcdir/../envvar-check
27 . $srcdir/../lang-default
28
29 pwd=`pwd`
30 t0=`echo "$0"|sed 's,.*/,,'`.tmp; tmp=$t0/$$
31 trap 'status=$?; cd "$pwd" && chmod -R u+rwx $t0 && rm -rf $t0 && exit $status' 0
32 trap '(exit $?); exit $?' 1 2 13 15
33
34 framework_failure=0
35 mkdir -p $tmp || framework_failure=1
36 cd $tmp || framework_failure=1
37
38 if test $framework_failure = 1; then
39   echo "$0: failure in testing framework" 1>&2
40   (exit 1); exit 1
41 fi
42
43 fail=0
44
45 nohup sh -c 'echo stdout; echo stderr 1>&2' 2>err || fail=1
46
47 # Be careful.  The results of the above nohup command
48 # change depending on whether stdin and stdout are redirected.
49 if test -t 1; then
50   test "`cat nohup.out`" = stdout || fail=1
51   if test -t 0; then
52     echo 'nohup: ignoring input and appending output to `nohup.out'\'
53   else
54     echo 'nohup: appending output to `nohup.out'\'
55   fi >exp || fail=1
56 else
57   # Here it should not even exist.
58   test -f nohup.out && fail=1
59   if test -t 0; then
60     echo 'nohup: ignoring input' >exp
61   else
62     rm -f exp
63   fi || fail=1
64 fi
65 echo 'stderr' >> exp || fail=1
66
67 cmp exp err || fail=1
68 test $fail = 1 && diff exp err 2> /dev/null
69 rm -f nohup.out err exp
70 # ----------------------
71
72 # Be careful.  The results of the following nohup command
73 # change depending on whether stderr is redirected.
74 nohup sh -c 'echo stdout; echo stderr 1>&2' >out || fail=1
75 if test -t 2; then
76   test "`cat out|tr '\n' -`" = stdout-stderr- || fail=1
77 else
78   test "`cat out|tr '\n' -`" = stdout- || fail=1
79 fi
80 # It must *not* exist.
81 test -f nohup.out && fail=1
82 rm -f nohup.out err
83 # ----------------------
84
85 nohup no-such-command 2> err
86 errno=$?
87 if test -t 1; then
88   test $errno = 127 || fail=1
89   # It must exist.
90   test -f nohup.out || fail=1
91   # It must be empty.
92   test -s nohup.out && fail=1
93 fi
94
95 cat <<\EOF > exp || fail=1
96 nohup: appending output to `nohup.out'
97 nohup: cannot run command `no-such-command': No such file or directory
98 EOF
99 # Disable these comparisons.  Too much variation in 2nd line.
100 # cmp exp err || fail=1
101 # test $fail = 1 && diff exp err 2> /dev/null
102 rm -f nohup.out err exp
103 # ----------------------
104
105 touch k; chmod 0 k
106 nohup ./k 2> err
107 errno=$?
108 test $errno = 126 || fail=1
109 if test -t 1; then
110   # It must exist.
111   test -f nohup.out || fail=1
112   # It must be empty.
113   test -s nohup.out && fail=1
114 fi
115
116 cat <<\EOF > exp || fail=1
117 nohup: appending output to `nohup.out'
118 nohup: cannot run command `./k': Permission denied
119 EOF
120 # Disable these comparisons.  Too much variation in 2nd line.
121 # cmp exp err || fail=1
122 # test $fail = 1 && diff exp err 2> /dev/null
123
124 # Make sure it fails with exit status of 127 when given too few arguments.
125 nohup >/dev/null 2>&1
126 test $? = 127 || fail=1
127
128 (exit $fail); exit $fail