📄 printf.tests
字号:
LC_ALL=CLC_NUMERIC=C# these should output error messages -- the format is requiredprintfprintf --# these should output nothingprintf ""printf -- ""# in the future this may mean to put the output into VAR, but for# now it is an error# 2005-03-15 no longer an errorunset varprintf -v var "%10d" $RANDOMecho ${#var}# this should expand escape sequences in the format string, nothing elseprintf "\tone\n"# this should not cut off output after the \cprintf "one\ctwo\n"# and unrecognized backslash escapes should have the backslash preserverdprintf "4\.2\n"printf "no newline " ; printf "now newline\n"# %% -> %printf "%%\n"# this was a bug caused by pre-processing the string for backslash escapes# before doing the `%' format processing -- all versions before bash-2.04printf "\045" ; echoprintf "\045d\n"# simple character outputprintf "%c\n" ABCD# test simple string outputprintf "%s\n" unquoted# test quoted string outputprintf "%s %q\n" unquoted quotedprintf "%s%10q\n" unquoted quotedprintf "%q\n" 'this&that'# make sure the format string is reused to use up argumentsprintf "%d " 1 2 3 4 5; printf "\n"# make sure that extra format characters get null argumentsprintf "%s %d %d %d\n" onestringprintf "%s %d %u %4.2f\n" onestringprintf -- "--%s %s--\n" 4.2 ''printf -- "--%s %s--\n" 4.2# test %b escapes# 8 is a non-octal digit, so the `81' should be outputprintf -- "--%b--\n" '\n\081'printf -- "--%b--\n" '\t\0101'printf -- "--%b--\n" '\t\101'# these should all display `A7'echo -e "\01017"echo -e "\x417"printf "%b\n" '\01017'printf "%b\n" '\1017'printf "%b\n" '\x417'printf -- "--%b--\n" '\"abcd\"'printf -- "--%b--\n" "\'abcd\'"printf -- "--%b--\n" 'a\\x'printf -- "--%b--\n" '\x'Z1=$(printf -- "%b\n" '\a\b\e\f\r\v')Z2=$'\a\b\e\f\r\v'if [ "$Z1" != "$Z2" ]; then echo "whoops: printf %b and $'' differ" >&2fiunset Z1 Z2printf -- "--%b--\n" ''printf -- "--%b--\n"# the stuff following the \c should be ignored, as well as the rest# of the format stringprintf -- "--%b--\n" '4.2\c5.4\n'; printf "\n"# unrecognized escape sequences should by displayed unchangedprintf -- "--%b--\n" '4\.2'# a bare \ should not be processed as an escape sequenceprintf -- "--%b--\n" '\'# make sure extra arguments are ignored if the format string doesn't# actually use themprintf "\n" 4.4 BSDprintf " " 4.4 BSD ; printf "\n"# make sure that a fieldwidth and precision of `*' are handled rightprintf "%10.8s\n" 4.4BSDprintf "%*.*s\n" 10 8 4.4BSDprintf "%10.8q\n" 4.4BSDprintf "%*.*q\n" 10 8 4.4BSDprintf "%6b\n" 4.4BSDprintf "%*b\n" 6 4.4BSD# we handle this crap with homemade code in printf.defprintf "%10b\n" 4.4BSDprintf -- "--%-10b--\n" 4.4BSDprintf "%4.2b\n" 4.4BSDprintf "%.3b\n" 4.4BSDprintf -- "--%-8b--\n" 4.4BSD# test numeric conversions -- these four lines should echo identicallyprintf "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255printf "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255printf "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255printf "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255printf "%10d\n" 42printf "%10d\n" -42printf "%*d\n" 10 42printf "%*d\n" 10 -42# test some simple floating point formatsprintf "%4.2f\n" 4.2printf "%#4.2f\n" 4.2printf "%#4.1f\n" 4.2printf "%*.*f\n" 4 2 4.2printf "%#*.*f\n" 4 2 4.2printf "%#*.*f\n" 4 1 4.2printf "%E\n" 4.2printf "%e\n" 4.2printf "%6.1E\n" 4.2printf "%6.1e\n" 4.2printf "%G\n" 4.2printf "%g\n" 4.2printf "%6.2G\n" 4.2printf "%6.2g\n" 4.2# test some of the more esoteric features of POSIX.1 printfprintf "%d\n" "'string'"printf "%d\n" '"string"'printf "%#o\n" "'string'"printf "%#o\n" '"string"'printf "%#x\n" "'string'"printf "%#X\n" '"string"'printf "%6.2f\n" "'string'"printf "%6.2f\n" '"string"'# output from these two lines had better be the sameprintf -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyzprintf -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz# and these two alsoprintf -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyzprintf -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz# tests for translating \' to ' and \\ to \# printf translates \' to ' in the format string...printf "\'abcd\'\n"# but not when the %b format specification is usedprintf "%b\n" \\\'abcd\\\'# but both translate \\ to \printf '\\abcd\\\n'printf "%b\n" '\\abcd\\'# this was reported as a bug in bash-2.03# these three lines should all echo `26'printf "%d\n" 0x1aprintf "%d\n" 032printf "%d\n" 26# error messages# this should be an overflow, but error messages vary between systems# printf "%lu\n" 4294967296# ...but we cannot use this because some systems (SunOS4, for example),# happily ignore overflow conditions in strtol(3)#printf "%ld\n" 4294967296printf "%10"printf "ab%Mcd\n"# this caused an infinite loop in older versions of printfprintf "%y" 0# these should print a warning and `0', according to POSIX.2printf "%d\n" GNUprintf "%o\n" GNU# failures in all bash versions through bash-2.05printf "%.0s" fooprintf "%.*s" 0 fooprintf '%.0b-%.0s\n' foo barprintf '(%*b)(%*s)\n' -4 foo -4 barformat='%'`printf '%0100384d' 0`'d\n' printf $format 0# failures in all bash versions through bash-3.0 - undercounted charactersunset vvprintf " %s %s %s \n%n" ab cd ef vvecho "$vv"# this doesn't work with printf(3) on all systems#printf "%'s\n" foo# test cases from an austin-group list discussion# prints ^G as an extensionprintf '%b\n' '\7'# prints ^Gprintf '%b\n' '\0007'# prints NUL then 7printf '\0007\n'# prints no more than two hex digitsprintf '\x07e\n'# additional backslash escapesprintf '\"\?\n'# failures with decimal precisions until after bash-3.1printf '%0.5d\n' 1printf '%05d\n' 1printf '%5d\n' 1printf '%0d\n' 1# failures with various floating point formats and 0 after bash-3.2printf "%G\n" 0printf "%g\n" 0printf "%4.2G\n" 0printf "%4.2g\n" 0printf "%G\n" 4printf "%g\n" 4printf "%4.2G\n" 4printf "%4.2g\n" 4printf "%F\n" 0printf "%f\n" 0printf "%4.2F\n" 0printf "%4.2f\n" 0printf "%F\n" 4printf "%f\n" 4printf "%4.2F\n" 4printf "%4.2f\n" 4printf "%E\n" 0printf "%e\n" 0printf "%4.2E\n" 0printf "%4.2e\n" 0printf "%E\n" 4printf "%e\n" 4printf "%4.2E\n" 4printf "%4.2e\n" 4printf "%08X\n" 2604292517# make sure these format specifiers all output '' for empty string argumentsecho qprintf "%q\n" ""printf "%q\n"echo sprintf "%s\n" ''printf "%s\n"echo bprintf "%b\n" ''printf "%b\n"# bug in bash versions up to and including bash-3.2v=yyyprintf -v var "%s" '/current/working/directory/*.@(m3|i3|ig|mg)'shopt -s nullglob extglobecho "x$(printf "%b" @(hugo))x"printf -v var "%b" @(hugo); echo "x${var}x"# tests variable assignment with -v${THIS_SH} ./printf1.sub${THIS_SH} ./printf2.sub${THIS_SH} ./printf3.sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -