1#!/bin/sh 2echo T.clv: check command-line variables 3 4awk=${awk-../a.out} 5 6rm -f core 7 8# stdin only, no cmdline asgn 9echo 'hello 10goodbye' | $awk ' 11BEGIN { x=0; print x; getline; print x, $0 } 12' >foo1 13echo '0 140 hello' >foo2 15diff foo1 foo2 || echo 'BAD: T.clv (stdin only)' 16 17# cmdline asgn then stdin 18echo 'hello 19goodbye' | $awk ' 20BEGIN { x=0; print x; getline; print x, $0 } 21' x=1 >foo1 22echo '0 231 hello' >foo2 24diff foo1 foo2 || echo 'BAD: T.clv (x=1 only)' 25 26# several cmdline asgn, then stdin 27echo 'hello 28goodbye' | $awk ' 29BEGIN { x=0; print x; getline; print x, $0 } 30' x=1 x=2 x=3 >foo1 31echo '0 323 hello' >foo2 33diff foo1 foo2 || echo 'BAD: T.clv (x=3 only)' 34 35# several cmdline asgn, then file 36echo 'hello 37goodbye' >foo 38$awk ' 39BEGIN { x=0; print x; getline; print x, $0 } 40' x=1 x=2 x=3 foo >foo1 41echo '0 423 hello' >foo2 43diff foo1 foo2 || echo 'BAD: T.clv (x=3 only)' 44 45# cmdline asgn then file 46echo 4 >foo1 47$awk 'BEGIN { getline; print x}' x=4 /dev/null >foo2 48diff foo1 foo2 || echo 'BAD: T.clv (x=4 /dev/null)' 49 50#cmdline asgn then file but no read of it 51echo 0 >foo1 52$awk 'BEGIN { x=0; getline <"/dev/null"; print x}' x=5 /dev/null >foo2 53diff foo1 foo2 || echo 'BAD: T.clv (x=5 /dev/null)' 54 55#cmdline asgn then file then read 56echo 'xxx 57yyy 58zzz' >foo 59echo '6 60end' >foo1 61$awk 'BEGIN { x=0; getline; print x} 62 END { print x }' x=6 foo x=end >foo2 63diff foo1 foo2 || echo 'BAD: T.clv (x=6 /dev/null)' 64 65#cmdline asgn then file then read 66echo '0 67end' >foo1 68$awk 'BEGIN { x=0; getline <"/dev/null"; print x} 69 END { print x }' x=7 /dev/null x=end >foo2 70diff foo1 foo2 || echo 'BAD: T.clv (x=7 /dev/null)' 71 72#cmdline asgn then file then read; _ in commandname 73echo '0 74end' >foo1 75$awk 'BEGIN { _=0; getline <"/dev/null"; print _} 76 END { print _ }' _=7A /dev/null _=end >foo2 77diff foo1 foo2 || echo 'BAD: T.clv (_=7A /dev/null)' 78 79# illegal varname in commandname 80$awk '{ print }' 99_=foo /dev/null >foo 2>foo2 81grep "can't open.*foo" foo2 >/dev/null 2>&1 || echo 'BAD: T.clv (7B: illegal varname)' 82 83# these test the new -v option: awk ... -v a=1 -v b=2 'prog' does before BEGIN 84 85echo 123 >foo1 86$awk -v x=123 'BEGIN { print x }' >foo2 87diff foo1 foo2 || echo 'BAD: T.clv (x=11)' 88 89echo 123 >foo1 90$awk -vx=123 'BEGIN { print x }' >foo2 91diff foo1 foo2 || echo 'BAD: T.clv (x=11a)' 92 93echo 123 abc 10.99 >foo1 94$awk -v x=123 -v y=abc -v z1=10.99 'BEGIN { print x, y, z1 }' >foo2 95diff foo1 foo2 || echo 'BAD: T.clv (x=12)' 96 97echo 123 abc 10.99 >foo1 98$awk -vx=123 -vy=abc -vz1=10.99 'BEGIN { print x, y, z1 }' >foo2 99diff foo1 foo2 || echo 'BAD: T.clv (x=12a)' 100 101echo 123 abc 10.99 >foo1 102$awk -v x=123 -v y=abc -v z1=10.99 -- 'BEGIN { print x, y, z1 }' >foo2 103diff foo1 foo2 || echo 'BAD: T.clv (x=12a)' 104 105echo 'BEGIN { print x, y, z1 }' >foo0 106echo 123 abc 10.99 >foo1 107$awk -v x=123 -v y=abc -f foo0 -v z1=10.99 >foo2 108diff foo1 foo2 || echo 'BAD: T.clv (x=13)' 109 110echo 'BEGIN { print x, y, z1 }' >foo0 111echo 123 abc 10.99 >foo1 112$awk -vx=123 -vy=abc -f foo0 -vz1=10.99 >foo2 113diff foo1 foo2 || echo 'BAD: T.clv (x=13a)' 114 115echo 'BEGIN { print x, y, z1 }' >foo0 116echo 123 abc 10.99 >foo1 117$awk -f foo0 -v x=123 -v y=abc -v z1=10.99 >foo2 118diff foo1 foo2 || echo 'BAD: T.clv (x=14)' 119 120echo 'BEGIN { print x, y, z1 }' >foo0 121echo 123 abc 10.99 >foo1 122$awk -f foo0 -vx=123 -vy=abc -vz1=10.99 >foo2 123diff foo1 foo2 || echo 'BAD: T.clv (x=14a)' 124 125echo 'BEGIN { print x, y, z1 } 126END { print x }' >foo0 127echo '123 abc 10.99 1284567' >foo1 129$awk -f foo0 -v x=123 -v y=abc -v z1=10.99 /dev/null x=4567 /dev/null >foo2 130diff foo1 foo2 || echo 'BAD: T.clv (x=15)' 131 132echo 'BEGIN { print x, y, z1 } 133END { print x }' >foo0 134echo '123 abc 10.99 1354567' >foo1 136$awk -f foo0 -vx=123 -vy=abc -vz1=10.99 /dev/null x=4567 /dev/null >foo2 137diff foo1 foo2 || echo 'BAD: T.clv (x=15a)' 138 139echo 'BEGIN { print x, y, z1 } 140NR==1 { print x }' >foo0 141echo '123 abc 10.99 1424567' >foo1 143$awk -v x=123 -v y=abc -v z1=10.99 -f foo0 x=4567 /etc/passwd >foo2 144diff foo1 foo2 || echo 'BAD: T.clv (x=16)' 145 146echo 'BEGIN { print x, y, z1 } 147NR==1 { print x }' >foo0 148echo '123 abc 10.99 1494567' >foo1 150$awk -vx=123 -vy=abc -vz1=10.99 -f foo0 x=4567 /etc/passwd >foo2 151diff foo1 foo2 || echo 'BAD: T.clv (x=16a)' 152 153 154 155# special chars in commandline assigned value; 156# have to use local echo to avoid quoting problems. 157 158./echo 'a\\b\z' >foo1 159./echo 'hello' | $awk '{print x}' x='\141\\\\\142\\z' >foo2 160diff foo1 foo2 || echo 'BAD: T.clv (x=17)' 161 162./echo "a 163z" >foo1 164./echo 'hello' | $awk '{print x}' x='a\nz' >foo2 165diff foo1 foo2 || echo 'BAD: T.clv (x=18)' 166 167# a bit circular here... 168$awk 'BEGIN { printf("a%c%c%cz\n", "\b", "\r", "\f") }' >foo1 169./echo 'hello' | $awk '{print x}' x='a\b\r\fz' >foo2 170diff foo1 foo2 || echo 'BAD: T.clv (x=19)' 171 172 173### newer -v tests 174 175 176$awk -vx 'BEGIN {print x}' >foo 2>&1 177grep 'invalid -v option argument: x' foo >/dev/null || echo 'BAD: T.clv (x=20)' 178 179$awk -v x 'BEGIN {print x}' >foo 2>&1 180grep 'invalid -v option argument: x' foo >/dev/null || echo 'BAD: T.clv (x=20a)' 181 182