1#! /bin/sh 2# posix-2.sh - Simple identification tests for POSIX.2 features 3# commonly missing or incorrectly implemented. 4# Time-stamp: <96/04/10 16:43:48 gildea> 5# By Stephen Gildea <gildea@x.org> March 1995 6# 7# Copyright (c) 1995 Stephen Gildea 8# Permission is hereby granted to deal in this Software without restriction. 9# THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. 10# 11# MODIFIED BY chet@po.cwru.edu to make part of the bash test suite. 12# last change: Wed Jun 19 12:24:24 EDT 1996 13# 14# some of the tests: 15# 16# shell functions (do we care?) 17# var=${var:-val} 18# unset 19# set -- 20# IFS parsing 21## not exiting with -e and failed "if", the way Ultrix does (Ultrix 4.2?) 22# "$@" expands to zero arguments if passed zero arguments 23# $SHELL -c 'echo $1' bad good 24# test -x 25# positional parameters greater than 9 26# arithmetic expansion $(( ... )) 27# getopts 28 29# For some tests we must run a sub-shell; $TESTSHELL says what to use. 30# If set, TESTSHELL must be an absolute pathname. 31# For example, on HP-UX 9, /bin/posix/sh is the supposedly-compliant shell. 32TESTSHELL=${THIS_SH:-$PWD/../bash} 33 34# these tests create temp files with names $TMPDIR/conf* 35: ${TMPDIR:=/tmp} 36 37exitval=0 38numtests=0 39 40echo "Testing for POSIX.2 conformance" 41 42newtest() 43{ 44 numtests=$(($numtests + 1)) 45} 46 47testfail() 48{ 49 echo "$1 test failed" 50 exitval=$(($exitval + 1)) 51} 52 53newtest 54empty="" 55test "${empty:-ok}" = ok || testfail "empty var colon" 56newtest 57test "${empty-bad}" = "" || testfail "got \"${empty-bad}\": empty var nocolon" 58newtest 59test "${unsetvar-ok}" = ok || testfail "unset var" 60newtest 61unset empty 62test "${empty-ok}" = ok || testfail "unset" 63 64newtest 65set -- -Z 66test "x$1" = x-Z || testfail '\"set -- arg\"' 67# this should empty the argument list 68newtest 69set -- 70test $# = 0 || testfail "still $# args: \"set --\"" 71 72# IFS parsing: 73newtest 74names=one/good/three 75saved_ifs="$IFS" 76IFS=/ 77set $names lose 78test "$2" = good || testfail "got \"$2\": IFS parsing" 79IFS="$saved_ifs" 80 81# "$@" with 0 arguments should expand to 0 arguments 82newtest 83cat > $TMPDIR/conftest1 << EOF 84$TMPDIR/conftest2 "\$@" 85EOF 86cat > $TMPDIR/conftest2 << "EOF" 87#! /bin/sh 88echo $# 89EOF 90chmod +x $TMPDIR/conftest1 $TMPDIR/conftest2 91numargs=$($TESTSHELL $TMPDIR/conftest1) 92if [ "$?" != 0 ]; then 93 testfail 'running $@' 94else 95 test "$numargs" = 0 || testfail '"$@" got '"$numargs args: expansion w 0 args" 96fi 97rm -f $TMPDIR/conftest1 $TMPDIR/conftest2 98 99newtest 100val=$("$TESTSHELL" -c 'echo $1' csh good) 101test "$val" = good || testfail "got \"$val\": sh -c" 102 103newtest 104# do these tests in a sub-shell because failure will exit 105val=$("$TESTSHELL" -c 'echo ${10}' 0 1 2 3 4 5 6 7 8 9 ten 11 2> /dev/null) 106test "$val" = ten || testfail "accessing more than 9 positional params" 107 108a=abc_def_ghi 109export a 110newtest; val=`"$TESTSHELL" -c 'echo "${a%_*}"' 2> /dev/null` 111test "$val" = abc_def || testfail "parameter % op" 112newtest; val=`"$TESTSHELL" -c 'echo "${a%%_*}"' 2> /dev/null` 113test "$val" = abc || testfail "parameter %% op" 114newtest; val=`"$TESTSHELL" -c 'echo "${a#*_}"' 2> /dev/null` 115test "$val" = def_ghi || testfail "parameter # op" 116newtest; val=`"$TESTSHELL" -c 'echo "${a##*_}"' 2> /dev/null` 117test "$val" = ghi || testfail "parameter ## op" 118 119newtest 120"$TESTSHELL" -c 'export a=value' 2> /dev/null || testfail "export with value" 121 122newtest 123a=5; test "$(( ($a+1)/2 ))" = 3 || testfail "arithmetic expansion" 124 125# does "test" support the -x switch? 126newtest 127touch $TMPDIR/conftest 128chmod -x $TMPDIR/conftest 129test -x $TMPDIR/conftest && testfail "negative test -x" 130chmod +x $TMPDIR/conftest 131test -x $TMPDIR/conftest || testfail "positive test -x" 132rm -f $TMPDIR/conftest 133 134newtest 135test "$OPTIND" = 1 || testfail "OPTIND initial value" 136 137newtest 138getopts a: store -a aoptval 139if [ "$OPTIND" != 3 ] || [ "$store" != a ] || [ "$OPTARG" != aoptval ]; then 140 testfail "getopts" 141fi 142 143# if I change the default quoting style for variable values, these 144# next four must change 145 146newtest 147SQUOTE="'" 148val1=$(set | sed -n 's:^SQUOTE=::p') 149if [ "$val1" != "''\\'''" ]; then 150 testfail "variable quoting 1" 151fi 152 153newtest 154VTILDE='~' 155val1=$(set | sed -n 's:^VTILDE=::p') 156if [ "$val1" != "'~'" ]; then 157 testfail "variable quoting 2" 158fi 159 160newtest 161VHASH=ab#cd 162val1=$(set | sed -n 's:^VHASH=::p') 163if [ "$val1" != "ab#cd" ]; then 164 testfail "variable quoting 3" 165fi 166 167newtest 168VHASH2=#abcd 169val1=$(set | sed -n 's:^VHASH2=::p') 170if [ "$val1" != "'#abcd'" ]; then 171 testfail "variable quoting 4" 172fi 173 174if [ $exitval = 0 ]; then 175 echo "All tests passed" 176else 177 echo "$exitval of $numtests tests failed" 178fi 179exit $exitval 180