1 #!/bin/bash
2 #
3 # Usage:
4 # ./sh-usage.test.sh <function name>
5
6 #### sh -c
7 $SH -c 'echo hi'
8 ## stdout: hi
9 ## status: 0
10
11 #### empty -c input
12 # had a bug here
13 $SH -c ''
14 ## stdout-json: ""
15 ## status: 0
16
17 #### sh +c is accepted
18 $SH +c 'echo hi'
19 ## stdout: hi
20 ## status: 0
21 ## N-I mksh/yash stdout-json: ""
22 ## N-I mksh/yash status: 127
23
24 #### empty stdin
25 # had a bug here
26 echo -n '' | $SH
27 ## stdout-json: ""
28 ## status: 0
29
30 #### shell obeys --help (regression for OSH)
31 n=$($SH --help | wc -l)
32 if test $n -gt 0; then
33 echo yes
34 fi
35 ## STDOUT:
36 yes
37 ## END
38 ## N-I dash/mksh stdout-json: ""
39
40 #### args are passed
41 $SH -c 'argv.py "$@"' dummy a b
42 ## stdout: ['a', 'b']
43
44 #### args that look like flags are passed after script
45 script=$TMP/sh1.sh
46 echo 'argv.py "$@"' > $script
47 chmod +x $script
48 $SH $script --help --help -h
49 ## stdout: ['--help', '--help', '-h']
50
51 #### args that look like flags are passed after -c
52 $SH -c 'argv.py "$@"' --help --help -h
53 ## stdout: ['--help', '-h']
54
55 #### exit with explicit arg
56 exit 42
57 ## status: 42
58
59 #### exit with no args
60 false
61 exit
62 ## status: 1
63
64 #### --rcfile in non-interactive shell prints warnings
65 echo 'echo rc' > rc
66
67 $SH --rcfile rc -i </dev/null 2>interactive.txt
68 grep -q 'warning' interactive.txt
69 echo warned=$? >&2
70
71 $SH --rcfile rc </dev/null 2>non-interactive.txt
72 grep -q 'warning' non-interactive.txt
73 echo warned=$? >&2
74
75 head *interactive.txt
76
77 ## STDERR:
78 warned=1
79 warned=0
80 ## END
81 ## N-I bash/dash/mksh STDERR:
82 warned=1
83 warned=1
84 ## END