quoting2.sh revision da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin########################################################################
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# This software is part of the ast package #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# Copyright (c) 1982-2007 AT&T Knowledge Ventures #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# and is licensed under the #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# Common Public License, Version 1.0 #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# by AT&T Knowledge Ventures #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# A copy of the License is available at #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# http://www.opensource.org/licenses/cpl1.0.txt #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# Information and Software Systems Research #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# AT&T Research #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# Florham Park NJ #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# David Korn <dgk@research.att.com> #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# #
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin########################################################################
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfunction err_exit
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin print -u2 -n "\t"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin print -u2 -r ${Command}[$1]: "${@:2}"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin let Errors+=1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinalias err_exit='err_exit $LINENO'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinCommand=${0##*/}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininteger Errors=0
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinset -o noglob
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ 'hi there' != "hi there" ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit "single quotes not the same as double quotes"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinx='hi there'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $x != 'hi there' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit "$x not the same as 'hi there'"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $x != "hi there" ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit "$x not the same as \"hi there \""
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ \a\b\c\*\|\"\ \\ != 'abc*|" \' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit " \\ differs from '' "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ "ab\'\"\$(" != 'ab\'\''"$(' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit " \"\" differs from '' "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $(print -r - 'abc*|" \') != 'abc*|" \' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit "\$(print -r - '') differs from ''"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $(print -r - "abc*|\" \\") != 'abc*|" \' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit "\$(print -r - '') differs from ''"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ "$(print -r - 'abc*|" \')" != 'abc*|" \' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit "\"\$(print -r - '')\" differs from ''"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ "$(print -r - "abc*|\" \\")" != 'abc*|" \' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit "\"\$(print -r - "")\" differs from ''"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $(print -r - "$(print -r - 'abc*|" \')") != 'abc*|" \' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit "nested \$(print -r - '') differs from ''"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ "$(print -r - $(print -r - 'abc*|" \'))" != 'abc*|" \' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit "\"nested \$(print -r - '')\" differs from ''"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $(print -r - "$(print -r - 'abc*|" \')") != 'abc*|" \' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit "nested \"\$(print -r - '')\" differs from ''"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinunset x
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ ${x-$(print -r - "abc*|\" \\")} != 'abc*|" \' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit "\${x-\$(print -r - '')} differs from ''"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ ${x-$(print -r - "a}c*|\" \\")} != 'a}c*|" \' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit "\${x-\$(print -r - '}')} differs from ''"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinx=$((echo foo)|(cat))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $x != foo ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit "((cmd)|(cmd)) failed"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinx=$(print -r -- "\"$HOME\"")
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $x != '"'$HOME'"' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit "nested double quotes failed"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin: ${z="a{b}c"}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $z != 'a{b}c' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '${z="a{b}c"} not correct'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinunset z
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin: "${z="a{b}c"}"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $z != 'a{b}c' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '"${z="a{b}c"}" not correct'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $(print -r -- "a\*b") != 'a\*b' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '$(print -r -- "a\*b") differs from a\*b'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinunset x
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $(print -r -- "a\*b$x") != 'a\*b' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '$(print -r -- "a\*b$x") differs from a\*b'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinx=hello
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinset -- ${x+foo bar bam}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif (( $# !=3 ))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '${x+foo bar bam} does not yield three arguments'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinset -- ${x+foo "bar bam"}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif (( $# !=2 ))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '${x+foo "bar bam"} does not yield two arguments'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinset -- ${x+foo 'bar bam'}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif (( $# !=2 ))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '${x+foo '\''bar bam'\''} does not yield two arguments'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinset -- ${x+foo $x bam}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif (( $# !=3 ))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '${x+foo $x bam} does not yield three arguments'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinset -- ${x+foo "$x" bam}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif (( $# !=3 ))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '${x+foo "$x" bam} does not yield three arguments'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinset -- ${x+"foo $x bam"}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif (( $# !=1 ))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '${x+"foo $x bam"} does not yield one argument'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinset -- "${x+foo $x bam}"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif (( $# !=1 ))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '"${x+foo $x bam}" does not yield one argument'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinset -- ${x+foo "$x "bam}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif (( $# !=2 ))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '${x+foo "$x "bam} does not yield two arguments'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinx="ab$'cd"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $x != 'ab$'"'cd" ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '$'"' inside double quotes not working"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinx=`print 'ab$'`
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $x != 'ab$' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '$'"' inside `` quotes not working"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinunset a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinx=$(print -r -- "'\
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin")
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $x != "'" ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit 'line continuation in double strings not working'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinx=$(print -r -- "'\
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin$a\
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin")
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $x != "'" ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit 'line continuation in expanded double strings not working'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinx='\*'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $(print -r -- $x) != '\*' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit 'x="\\*";$x != \*'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $(print -r -- "\}" ) != '\}' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '(print -r -- "\}"' not working
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ $(print -r -- "\{" ) != '\{' ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit 'print -r -- "\{"' not working
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# The following caused a syntax error on earlier versions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfoo=foo x=-
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ `eval print \\${foo$x}` != foo* ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '`eval print \\${foo$x}`' not working
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif [[ "`eval print \\${foo$x}`" != foo* ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '"`eval print \\${foo$x}`"' not working
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif ( [[ $() != '' ]] )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit '$() not working'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinx=a:b:c
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinset -- $( IFS=:; print $x)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif (( $# != 3))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen err_exit 'IFS not working correctly with command substitution'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin$SHELL -n 2> /dev/null << \! || err_exit '$(...) bug with ( in comment'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chiny=$(
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin # ( this line is a bug fix
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin print hi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin!
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinx=
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor j in glob noglob
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindo for i in 'a\*b' 'a\ b' 'a\bc' 'a\*b' 'a\"b'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin do eval [[ '$('print -r -- \'$i\'\$x')' != "'$i'" ]] && err_exit "quoting of $i\$x with $j enabled failed"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin eval [[ '$('print -r -- \'$i\'\${x%*}')' != "'$i'" ]] && err_exit "quoting of $i\${x%*} with $j enabled failed"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if [[ $j == noglob ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin then eval [[ '$('print -r -- \'$i\'\${x:-*}')' != "'$i''*'" ]] && err_exit "quoting of $i\${x:-*} with $j enabled failed"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin done
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin set -f
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindone
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfoo=foo
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin[[ "$" == '$' ]] || err_exit '"$" != $'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin[[ "${foo}$" == 'foo$' ]] || err_exit 'foo=foo;"${foo}$" != foo$'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin[[ "${foo}${foo}$" == 'foofoo$' ]] || err_exit 'foo=foo;"${foo}${foo}$" != foofoo$'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinexit $((Errors))