xref: /illumos-gate/usr/src/test/util-tests/tests/awk/tests/T.getline (revision eb00b1c8a31c2253a353644606388dff5b0e0275)
1#!/bin/bash
2
3if [[ -z "$AWK" || -z "$WORKDIR" ]]; then
4    printf '$AWK and $WORKDIR must be set\n' >&2
5    exit 1
6fi
7
8TEMP1=$WORKDIR/test.temp.1
9TEMP2=$WORKDIR/test.temp.2
10
11RESULT=0
12
13fail() {
14	echo "$1" >&2
15	RESULT=1
16}
17
18echo T.getline: test getline function
19
20who > $TEMP1
21cat $TEMP1 | $AWK '
22BEGIN {
23	while (getline)
24		print
25	exit
26}
27' > $TEMP2
28cmp -s $TEMP1 $TEMP2 || fail 'BAD: T.getline (bare getline)'
29
30who > $TEMP1
31cat $TEMP1 | $AWK '
32BEGIN {
33	while (getline xxx)
34		print xxx
35	exit
36}
37' > $TEMP2
38cmp -s $TEMP1 $TEMP2 || fail 'BAD: T.getline (getline xxx)'
39
40$AWK '
41BEGIN {
42	while (getline <"/etc/passwd")
43		print
44	exit
45}
46' > $TEMP1
47cmp -s /etc/passwd $TEMP1 || fail 'BAD: T.getline (getline <file)'
48
49cat /etc/passwd | $AWK '
50BEGIN {
51	while (getline <"-")	# stdin
52		print
53	exit
54}
55' > $TEMP1
56cmp -s /etc/passwd $TEMP1 || fail 'BAD: T.getline (getline <"-")'
57
58$AWK '
59BEGIN {
60	while (getline <ARGV[1])
61		print
62	exit
63}
64' /etc/passwd > $TEMP1
65cmp -s /etc/passwd $TEMP1 || fail 'BAD: T.getline (getline <arg)'
66
67$AWK '
68BEGIN {
69	while (getline x <ARGV[1])
70		print x
71	exit
72}
73' /etc/passwd > $TEMP1
74cmp -s /etc/passwd $TEMP1 || fail 'BAD: T.getline (getline x <arg)'
75
76$AWK '
77BEGIN {
78	while (("cat " ARGV[1]) | getline)
79		print
80	exit
81}
82' /etc/passwd > $TEMP1
83cmp -s /etc/passwd $TEMP1 || fail 'BAD: T.getline (cat arg | getline)'
84
85$AWK '
86BEGIN {
87	while (("cat " ARGV[1]) | getline x)
88		print x
89	exit
90}
91' /etc/passwd > $TEMP1
92cmp -s /etc/passwd $TEMP1 || fail 'BAD: T.getline (cat arg | getline x)'
93
94$AWK ' BEGIN { print getline <"/glop/glop/glop" } ' > $TEMP1
95echo '-1' > $TEMP2
96cmp -s $TEMP1 $TEMP2 || fail 'BAD: T.getline (non-existent file)'
97
98echo 'false false equal' > $TEMP1
99$AWK 'BEGIN {
100	"echo 0" | getline
101	if ($0) printf "true "
102	else printf "false "
103	if ($1) printf "true "
104	else printf "false "
105	if ($0==$1) printf "equal\n"
106	else printf "not equal\n"
107}' > $TEMP2
108cmp -s $TEMP1 $TEMP2 || fail 'BAD: T.getline bad $0 type in cmd|getline'
109
110echo 'L1
111L2' | $AWK 'BEGIN { $0="old stuff"; $1="new"; getline x; print}' > $TEMP1
112echo 'new stuff' > $TEMP2
113cmp -s $TEMP1 $TEMP2 || fail 'BAD: T.getline bad update $0'
114
115exit $RESULT
116