xref: /freebsd/contrib/one-true-awk/testdir/T.argv (revision 2008043f386721d58158e37e0d7e50df8095942d)
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 3.345 >foo1
101$awk 'BEGIN { print ARGV[1] + ARGV[2]}' 1 2.345 >foo2
102diff foo1 foo2 || echo 'BAD: T.argv (ARGV[1] + ARGV[2])'
103
104echo 3.345 >foo1
105x1=1 x2=2.345 $awk 'BEGIN { print ENVIRON["x1"] + ENVIRON["x2"]}' 1 2.345 >foo2
106diff foo1 foo2 || echo 'BAD: T.argv (ENVIRON[x1] + ENVIRON[x2])'
107
108
109echo 'foo1' >foo1
110echo 'foo2' >foo2
111echo 'foo3' >foo3
112$awk 'BEGIN { ARGV[2] = "" }
113	{ print }' foo1 foo2 foo3 >foo4
114echo 'foo1
115foo3' >foo5
116diff foo4 foo5 || echo 'BAD: T.argv zap ARGV[2]'
117
118echo hi > foo1 ; mv foo1 foo2
119$awk 'BEGIN { ARGV[1] = "foo2" ; print FILENAME }
120             { print FILENAME }' foo1 >foo3
121echo '
122foo2' >foo4
123diff foo3 foo4 || echo 'BAD: T.argv startup FILENAME'
124	# assumes that startup FILENAME is ""
125
126
127# test data balanced on pinhead...
128echo 'ARGV[3] is /dev/null
129ARGV[0] is ../a.out
130ARGV[1] is /dev/null' >foo1
131
132$awk 'BEGIN {   # this is a variant of arnolds original example
133        ARGV[1] = "/dev/null"
134        ARGV[2] = "glotch"  # file open must skipped deleted argv
135        ARGV[3] = "/dev/null"
136        ARGC = 4
137        delete ARGV[2]
138}
139# note that input is read here
140END {
141        for (i in ARGV)
142                printf("ARGV[%d] is %s\n", i, ARGV[i])
143}' >foo2
144diff foo1 foo2 || echo 'BAD: T.argv delete ARGV[2]'
145