1*826ac02aSGarrett D'Amore#! /usr/bin/ksh 2*826ac02aSGarrett D'Amore# 3*826ac02aSGarrett D'Amore# 4*826ac02aSGarrett D'Amore# This file and its contents are supplied under the terms of the 5*826ac02aSGarrett D'Amore# Common Development and Distribution License ("CDDL"), version 1.0. 6*826ac02aSGarrett D'Amore# You may only use this file in accordance with the terms of version 7*826ac02aSGarrett D'Amore# 1.0 of the CDDL. 8*826ac02aSGarrett D'Amore# 9*826ac02aSGarrett D'Amore# A full copy of the text of the CDDL should have accompanied this 10*826ac02aSGarrett D'Amore# source. A copy of the CDDL is also available via the Internet at 11*826ac02aSGarrett D'Amore# http://www.illumos.org/license/CDDL. 12*826ac02aSGarrett D'Amore# 13*826ac02aSGarrett D'Amore 14*826ac02aSGarrett D'Amore# 15*826ac02aSGarrett D'Amore# Copyright 2014 Garrett D'Amore <garrett@damore.org> 16*826ac02aSGarrett D'Amore# 17*826ac02aSGarrett D'Amore 18*826ac02aSGarrett D'AmoreXARGS=${XARGS:=/usr/bin/xargs} 19*826ac02aSGarrett D'Amore 20*826ac02aSGarrett D'Amoretest_start() { 21*826ac02aSGarrett D'Amore print "TEST STARTING ${1}: ${2}" 22*826ac02aSGarrett D'Amore} 23*826ac02aSGarrett D'Amore 24*826ac02aSGarrett D'Amoretest_pass() { 25*826ac02aSGarrett D'Amore print "TEST PASS: ${1}" 26*826ac02aSGarrett D'Amore} 27*826ac02aSGarrett D'Amore 28*826ac02aSGarrett D'Amoretest_fail() { 29*826ac02aSGarrett D'Amore print "TEST FAIL: ${1}: ${2}" 30*826ac02aSGarrett D'Amore exit -1 31*826ac02aSGarrett D'Amore} 32*826ac02aSGarrett D'Amore 33*826ac02aSGarrett D'Amorecheckrv() { 34*826ac02aSGarrett D'Amore if [[ $? -ne 0 ]]; then 35*826ac02aSGarrett D'Amore test_fail $1 "exit failure" 36*826ac02aSGarrett D'Amore fi 37*826ac02aSGarrett D'Amore} 38*826ac02aSGarrett D'Amore 39*826ac02aSGarrett D'Amorecompare() { 40*826ac02aSGarrett D'Amore if [[ "$2" != "$3" ]]; then 41*826ac02aSGarrett D'Amore test_fail $1 "compare mismatch, got [$2] expected [$3]" 42*826ac02aSGarrett D'Amore fi 43*826ac02aSGarrett D'Amore} 44*826ac02aSGarrett D'Amore 45*826ac02aSGarrett D'Amoretest1() { 46*826ac02aSGarrett D'Amore t=test1 47*826ac02aSGarrett D'Amore test_start $t "-I handling" 48*826ac02aSGarrett D'Amore comp=$(echo foo bar baz other | $XARGS -I THING echo '** THING **') 49*826ac02aSGarrett D'Amore checkrv $t 50*826ac02aSGarrett D'Amore good='** foo bar baz other **' 51*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 52*826ac02aSGarrett D'Amore test_pass $t 53*826ac02aSGarrett D'Amore} 54*826ac02aSGarrett D'Amore 55*826ac02aSGarrett D'Amoretest2() { 56*826ac02aSGarrett D'Amore t=test2 57*826ac02aSGarrett D'Amore test_start $t "-n 1 handling" 58*826ac02aSGarrett D'Amore comp=$(echo foo bar baz other | $XARGS -n 1 echo '***') 59*826ac02aSGarrett D'Amore checkrv $t 60*826ac02aSGarrett D'Amore good='*** foo 61*826ac02aSGarrett D'Amore*** bar 62*826ac02aSGarrett D'Amore*** baz 63*826ac02aSGarrett D'Amore*** other' 64*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 65*826ac02aSGarrett D'Amore test_pass $t 66*826ac02aSGarrett D'Amore} 67*826ac02aSGarrett D'Amore 68*826ac02aSGarrett D'Amoretest3() { 69*826ac02aSGarrett D'Amore t=test3 70*826ac02aSGarrett D'Amore test_start $t "-I before -n 1" 71*826ac02aSGarrett D'Amore comp=$(echo foo bar baz other | $XARGS -I THING -n1 echo '** THING **') 72*826ac02aSGarrett D'Amore checkrv $t 73*826ac02aSGarrett D'Amore good='** THING ** foo 74*826ac02aSGarrett D'Amore** THING ** bar 75*826ac02aSGarrett D'Amore** THING ** baz 76*826ac02aSGarrett D'Amore** THING ** other' 77*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 78*826ac02aSGarrett D'Amore test_pass $t 79*826ac02aSGarrett D'Amore} 80*826ac02aSGarrett D'Amore 81*826ac02aSGarrett D'Amoretest4() { 82*826ac02aSGarrett D'Amore t=test4 83*826ac02aSGarrett D'Amore test_start $t "-n 1 before -I" 84*826ac02aSGarrett D'Amore comp=$(echo foo bar baz other | $XARGS -n 1 -I THING echo '** THING **') 85*826ac02aSGarrett D'Amore checkrv $t 86*826ac02aSGarrett D'Amore good='** foo bar baz other **' 87*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 88*826ac02aSGarrett D'Amore test_pass $t 89*826ac02aSGarrett D'Amore} 90*826ac02aSGarrett D'Amore 91*826ac02aSGarrett D'Amoretest5() { 92*826ac02aSGarrett D'Amore t=test5 93*826ac02aSGarrett D'Amore test_start $t "-i multiple lines handling" 94*826ac02aSGarrett D'Amore comp=$(printf "abc def\nxyz\n123" | $XARGS -n1 -i echo '[{}]') 95*826ac02aSGarrett D'Amore checkrv $t 96*826ac02aSGarrett D'Amore good='[abc def] 97*826ac02aSGarrett D'Amore[xyz] 98*826ac02aSGarrett D'Amore[123]' 99*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 100*826ac02aSGarrett D'Amore test_pass $t 101*826ac02aSGarrett D'Amore} 102*826ac02aSGarrett D'Amore 103*826ac02aSGarrett D'Amoretest6() { 104*826ac02aSGarrett D'Amore t=test6 105*826ac02aSGarrett D'Amore test_start $t "-E handling" 106*826ac02aSGarrett D'Amore comp=$(printf "abc def xyx\n_\n123\nnope" | $XARGS -edef echo) 107*826ac02aSGarrett D'Amore checkrv $t 108*826ac02aSGarrett D'Amore good='abc' 109*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 110*826ac02aSGarrett D'Amore test_pass $t 111*826ac02aSGarrett D'Amore} 112*826ac02aSGarrett D'Amore 113*826ac02aSGarrett D'Amoretest7() { 114*826ac02aSGarrett D'Amore t=test7 115*826ac02aSGarrett D'Amore test_start $t "newlines in arguments" 116*826ac02aSGarrett D'Amore comp=$(printf "abc def\nxyz\n\n123 456\n789" | $XARGS echo) 117*826ac02aSGarrett D'Amore checkrv $t 118*826ac02aSGarrett D'Amore good='abc def xyz 123 456 789' 119*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 120*826ac02aSGarrett D'Amore test_pass $t 121*826ac02aSGarrett D'Amore} 122*826ac02aSGarrett D'Amore 123*826ac02aSGarrett D'Amoretest8() { 124*826ac02aSGarrett D'Amore t=test8 125*826ac02aSGarrett D'Amore test_start $t "limited counts via -n3" 126*826ac02aSGarrett D'Amore comp=$(printf "abc def ghi jkl mno 123 456 789" | $XARGS -n 3 echo '**' ) 127*826ac02aSGarrett D'Amore checkrv $t 128*826ac02aSGarrett D'Amore good='** abc def ghi 129*826ac02aSGarrett D'Amore** jkl mno 123 130*826ac02aSGarrett D'Amore** 456 789' 131*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 132*826ac02aSGarrett D'Amore test_pass $t 133*826ac02aSGarrett D'Amore} 134*826ac02aSGarrett D'Amore 135*826ac02aSGarrett D'Amoretest9() { 136*826ac02aSGarrett D'Amore t=test9 137*826ac02aSGarrett D'Amore test_start $t "multiple lines via -L2" 138*826ac02aSGarrett D'Amore comp=$(printf "abc def\n123 456\npeterpiper" | $XARGS -L2 echo '**') 139*826ac02aSGarrett D'Amore checkrv $t 140*826ac02aSGarrett D'Amore good='** abc def 123 456 141*826ac02aSGarrett D'Amore** peterpiper' 142*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 143*826ac02aSGarrett D'Amore test_pass $t 144*826ac02aSGarrett D'Amore} 145*826ac02aSGarrett D'Amore 146*826ac02aSGarrett D'Amoretest10() { 147*826ac02aSGarrett D'Amore t=test10 148*826ac02aSGarrett D'Amore test_start $t "argument sizes" 149*826ac02aSGarrett D'Amore comp=$(printf "abc def 123 456 peter alpha\n" | $XARGS -s15 echo) 150*826ac02aSGarrett D'Amore checkrv $t 151*826ac02aSGarrett D'Amore good='abc def 152*826ac02aSGarrett D'Amore123 456 153*826ac02aSGarrett D'Amorepeter 154*826ac02aSGarrett D'Amorealpha' 155*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 156*826ac02aSGarrett D'Amore test_pass $t 157*826ac02aSGarrett D'Amore} 158*826ac02aSGarrett D'Amore 159*826ac02aSGarrett D'Amoretest11() { 160*826ac02aSGarrett D'Amore t=test11 161*826ac02aSGarrett D'Amore test_start $t "bare -e" 162*826ac02aSGarrett D'Amore comp=$(printf "abc def _ end of string" | $XARGS -e echo '**') 163*826ac02aSGarrett D'Amore checkrv $t 164*826ac02aSGarrett D'Amore good='** abc def _ end of string' 165*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 166*826ac02aSGarrett D'Amore test_pass $t 167*826ac02aSGarrett D'Amore} 168*826ac02aSGarrett D'Amore 169*826ac02aSGarrett D'Amoretest12() { 170*826ac02aSGarrett D'Amore t=test12 171*826ac02aSGarrett D'Amore test_start $t "-E ''" 172*826ac02aSGarrett D'Amore comp=$(printf "abc def _ end of string" | $XARGS -E '' echo '**') 173*826ac02aSGarrett D'Amore checkrv $t 174*826ac02aSGarrett D'Amore good='** abc def _ end of string' 175*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 176*826ac02aSGarrett D'Amore test_pass $t 177*826ac02aSGarrett D'Amore} 178*826ac02aSGarrett D'Amore 179*826ac02aSGarrett D'Amoretest13() { 180*826ac02aSGarrett D'Amore t=test13 181*826ac02aSGarrett D'Amore test_start $t "end of string (no -E or -e)" 182*826ac02aSGarrett D'Amore comp=$(printf "abc def _ end of string" | $XARGS echo '**') 183*826ac02aSGarrett D'Amore checkrv $t 184*826ac02aSGarrett D'Amore good='** abc def' 185*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 186*826ac02aSGarrett D'Amore test_pass $t 187*826ac02aSGarrett D'Amore} 188*826ac02aSGarrett D'Amore 189*826ac02aSGarrett D'Amoretest14() { 190*826ac02aSGarrett D'Amore t=test14 191*826ac02aSGarrett D'Amore test_start $t "trailing blank with -L" 192*826ac02aSGarrett D'Amore comp=$(printf "abc def \n123 456\npeter\nbogus" | $XARGS -L2 echo '**') 193*826ac02aSGarrett D'Amore checkrv $t 194*826ac02aSGarrett D'Amore good='** abc def 123 456 peter 195*826ac02aSGarrett D'Amore** bogus' 196*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 197*826ac02aSGarrett D'Amore test_pass $t 198*826ac02aSGarrett D'Amore} 199*826ac02aSGarrett D'Amore 200*826ac02aSGarrett D'Amoretest15() { 201*826ac02aSGarrett D'Amore t=test15 202*826ac02aSGarrett D'Amore test_start $t "leading and embedded blanks with -i" 203*826ac02aSGarrett D'Amore comp=$(printf "abc def\n xyz bogus\nnext" | $XARGS -i echo '** {}') 204*826ac02aSGarrett D'Amore checkrv $t 205*826ac02aSGarrett D'Amore good='** abc def 206*826ac02aSGarrett D'Amore** xyz bogus 207*826ac02aSGarrett D'Amore** next' 208*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 209*826ac02aSGarrett D'Amore test_pass $t 210*826ac02aSGarrett D'Amore} 211*826ac02aSGarrett D'Amore 212*826ac02aSGarrett D'Amoretest16() { 213*826ac02aSGarrett D'Amore t=test16 214*826ac02aSGarrett D'Amore test_start $t "single character replstring" 215*826ac02aSGarrett D'Amore comp=$(echo foo bar baz other | $XARGS -I X echo '** X **') 216*826ac02aSGarrett D'Amore checkrv $t 217*826ac02aSGarrett D'Amore good='** foo bar baz other **' 218*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 219*826ac02aSGarrett D'Amore test_pass $t 220*826ac02aSGarrett D'Amore} 221*826ac02aSGarrett D'Amore 222*826ac02aSGarrett D'Amoretest17() { 223*826ac02aSGarrett D'Amore t=test17 224*826ac02aSGarrett D'Amore test_start $t "null byte separators" 225*826ac02aSGarrett D'Amore comp=$(print 'foo bar baz\000more data' | $XARGS -n1 -0 echo '**') 226*826ac02aSGarrett D'Amore checkrv $t 227*826ac02aSGarrett D'Amore good='** foo bar baz 228*826ac02aSGarrett D'Amore** more data' 229*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 230*826ac02aSGarrett D'Amore test_pass $t 231*826ac02aSGarrett D'Amore} 232*826ac02aSGarrett D'Amore 233*826ac02aSGarrett D'Amoretest18() { 234*826ac02aSGarrett D'Amore t=test18 235*826ac02aSGarrett D'Amore test_start $t "escape characters" 236*826ac02aSGarrett D'Amore comp=$(printf 'foo\\ bar second" "arg third' | $XARGS -n1 echo '**') 237*826ac02aSGarrett D'Amore checkrv $t 238*826ac02aSGarrett D'Amore good='** foo bar 239*826ac02aSGarrett D'Amore** second arg 240*826ac02aSGarrett D'Amore** third' 241*826ac02aSGarrett D'Amore compare $t "$comp" "$good" 242*826ac02aSGarrett D'Amore test_pass $t 243*826ac02aSGarrett D'Amore} 244*826ac02aSGarrett D'Amore 245*826ac02aSGarrett D'Amoretest1 246*826ac02aSGarrett D'Amoretest2 247*826ac02aSGarrett D'Amoretest3 248*826ac02aSGarrett D'Amoretest4 249*826ac02aSGarrett D'Amoretest5 250*826ac02aSGarrett D'Amoretest6 251*826ac02aSGarrett D'Amoretest7 252*826ac02aSGarrett D'Amoretest8 253*826ac02aSGarrett D'Amoretest9 254*826ac02aSGarrett D'Amoretest10 255*826ac02aSGarrett D'Amoretest11 256*826ac02aSGarrett D'Amoretest12 257*826ac02aSGarrett D'Amoretest13 258*826ac02aSGarrett D'Amoretest14 259*826ac02aSGarrett D'Amoretest15 260*826ac02aSGarrett D'Amoretest16 261*826ac02aSGarrett D'Amoretest17 262*826ac02aSGarrett D'Amoretest18 263