xref: /freebsd/contrib/one-true-awk/testdir/T.argv (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
1echo T.argv: misc tests of argc and argv
2
3awk=${awk-../a.out}
4
5echo >foo1
6echo >foo2
7$awk '
8BEGIN {
9	for (i = 1; i < ARGC-1; i++)
10		printf "%s ", ARGV[i]
11	if (ARGC > 1)
12		printf "%s", ARGV[i]
13	printf "\n"
14	exit
15}' * >foo1
16echo * >foo2
17diff foo1 foo2 || echo 'BAD: T.argv (echo1 *)'
18
19$awk '
20BEGIN {
21	for (i = 1; i < ARGC; i++) {
22		printf "%s", ARGV[i]
23		if (i < ARGC-1)
24			printf " "
25	}
26	printf "\n"
27	exit
28}' * >foo1
29echo * >foo2
30diff foo1 foo2 || echo 'BAD: T.argv (echo2 *)'
31
32$awk '
33BEGIN {
34	print ARGC
35	ARGV[ARGC-1] = ""
36	for (i=0; i < ARGC; i++)
37		print ARGV[i]
38	exit
39}
40' a bc def gh >foo1
41echo "5
42$awk
43a
44bc
45def
46" >foo2
47diff foo1 foo2 || echo 'BAD: T.argv (argc *)'
48
49echo '1
502
513' >foo0
52echo 'foo1
53foo2
54foo3' >foo1
55$awk '{print L $0}' L=foo <foo0 >foo2
56diff foo1 foo2 || echo 'BAD: T.argv (L=foo <foo1)'
57
58echo '1
592
603' >foo0
61echo 'foo1
62foo2
63foo3' >foo1
64$awk '{print L $0}' L=foo foo0 >foo2
65diff foo1 foo2 || echo 'BAD: T.argv (L=foo foo1)'
66
67echo '1
682
693' >foo0
70echo 'foo1
71foo2
72foo3' >foo1
73cat foo0 | $awk '{print L $0}' L=foo - >foo2
74diff foo1 foo2 || echo 'BAD: T.argv (L=foo -)'
75
76echo '1
772
783' >foo0
79echo 'foo1
80foo2
81foo3
82glop1
83glop2
84glop3' >foo1
85$awk '{print L $0}' L=foo foo0 L=glop foo0 >foo2
86diff foo1 foo2 || echo 'BAD: T.argv (L=foo L=glop)'
87
88echo '1
892
903' >foo0
91echo '111
92112
93113
94221
95222
96223' >foo1
97$awk '{print L $0}' L=11 foo0 L=22 foo0 >foo2
98diff foo1 foo2 || echo 'BAD: T.argv (L=11 L=22)'
99
100echo >foo0
101echo 'name=value
102name=value' >foo1
103$awk 'BEGIN { print ARGV[1] } { print ARGV[1] }' name=value foo0 >foo2
104diff foo1 foo2 || echo 'BAD: T.argv assignment operand modified'
105
106echo 3.345 >foo1
107$awk 'BEGIN { print ARGV[1] + ARGV[2]}' 1 2.345 >foo2
108diff foo1 foo2 || echo 'BAD: T.argv (ARGV[1] + ARGV[2])'
109
110echo 3.345 >foo1
111x1=1 x2=2.345 $awk 'BEGIN { print ENVIRON["x1"] + ENVIRON["x2"]}' 1 2.345 >foo2
112diff foo1 foo2 || echo 'BAD: T.argv (ENVIRON[x1] + ENVIRON[x2])'
113
114
115echo 'foo1' >foo1
116echo 'foo2' >foo2
117echo 'foo3' >foo3
118$awk 'BEGIN { ARGV[2] = "" }
119	{ print }' foo1 foo2 foo3 >foo4
120echo 'foo1
121foo3' >foo5
122diff foo4 foo5 || echo 'BAD: T.argv zap ARGV[2]'
123
124echo hi > foo1 ; mv foo1 foo2
125$awk 'BEGIN { ARGV[1] = "foo2" ; print FILENAME }
126             { print FILENAME }' foo1 >foo3
127echo '
128foo2' >foo4
129diff foo3 foo4 || echo 'BAD: T.argv startup FILENAME'
130	# assumes that startup FILENAME is ""
131
132
133# test data balanced on pinhead...
134echo 'ARGV[3] is /dev/null
135ARGV[0] is ../a.out
136ARGV[1] is /dev/null' >foo1
137
138$awk 'BEGIN {   # this is a variant of arnolds original example
139        ARGV[1] = "/dev/null"
140        ARGV[2] = "glotch"  # file open must skipped deleted argv
141        ARGV[3] = "/dev/null"
142        ARGC = 4
143        delete ARGV[2]
144}
145# note that input is read here
146END {
147        for (i in ARGV)
148                printf("ARGV[%d] is %s\n", i, ARGV[i])
149}' >foo2
150diff foo1 foo2 || echo 'BAD: T.argv delete ARGV[2]'
151