Permalink
Please sign in to comment.
Browse files
Split out builtin-io.test.sh for echo and read.
Preparing to implement backslash escapes. Both echo and read know about backslashes. Also: - Begin tests that show off xtrace and PS4 - Add shell options and environment variables to the quick ref. Allow topics that are upper case. - Minor tweaks to comments.
- Loading branch information...
Showing
with
205 additions
and 83 deletions.
- +2 −2 build/quick_ref.py
- +2 −2 core/cmd_exec.py
- +13 −2 doc/osh-quick-ref-toc.txt
- +23 −0 gold/lineno.sh
- +78 −0 gold/xtrace1.sh
- +81 −0 spec/builtin-io.test.sh
- +0 −76 spec/builtins.test.sh
- +6 −1 test/spec.sh
| @@ -0,0 +1,23 @@ | ||
| #!/bin/bash | ||
| # | ||
| # Usage: | ||
| # ./lineno.sh <function name> | ||
| set -o nounset | ||
| set -o pipefail | ||
| set -o errexit | ||
| # https://unix.stackexchange.com/questions/355965/how-to-check-which-line-of-a-bash-script-is-being-executed | ||
| # This is different than LINENO, see gold/xtrace1.sh. | ||
| f(){ echo "${BASH_LINENO[-2]}"; } | ||
| echo next1 | ||
| f | ||
| echo next2 | ||
| f | ||
| echo next 3 | ||
| f |
| @@ -0,0 +1,78 @@ | ||
| #!/bin/bash | ||
| # | ||
| # Usage: | ||
| # ./xtrace1.sh <function name> | ||
| set -o nounset | ||
| set -o pipefail | ||
| set -o errexit | ||
| # Problem: | ||
| # - There is no indentation for function calls | ||
| # - There is no indication of the function call. It only traces simple | ||
| # commands. 'local' is also traced with the concrete value. | ||
| func() { | ||
| local arg=$1 | ||
| echo "{ func $arg" | ||
| echo "func $arg }" | ||
| } | ||
| main() { | ||
| set -o xtrace | ||
| echo '{ main' | ||
| func main | ||
| echo 'main }' | ||
| # No indentation or increase in + | ||
| ( func subshell) | ||
| # Now we change to ++ | ||
| foo=$(func commandsub) | ||
| echo $foo | ||
| # Still + | ||
| func pipeline | wc -l | ||
| # Increase to three | ||
| foo=$(echo $(func commandsub)) | ||
| echo $foo | ||
| # Call it recursively | ||
| $0 func dollar-zero | ||
| # Call it recursively with | ||
| export SHELLOPTS | ||
| $0 func dollar-zero-shellopts | ||
| echo | ||
| echo | ||
| echo | ||
| # OK this is useful. | ||
| # https://unix.stackexchange.com/questions/355965/how-to-check-which-line-of-a-bash-script-is-being-executed | ||
| PS4='+${LINENO}: ' | ||
| func ps4 | ||
| foo=$(func ps4-commandsub) | ||
| echo foo | ||
| } | ||
| my-ps4() { | ||
| for i in {1..3}; do | ||
| echo -n $i | ||
| done | ||
| } | ||
| # The problem with this is you don't want to fork the shell for every line! | ||
| call-func-in-ps4() { | ||
| set -x | ||
| PS4='[$(my-ps4)] ' | ||
| echo one | ||
| echo two | ||
| } | ||
| "$@" |
| @@ -0,0 +1,81 @@ | ||
| #!/bin/bash | ||
| # | ||
| # echo, read | ||
| # later: perhaps mapfile, etc. | ||
| ### echo dashes | ||
| echo - | ||
| echo -- | ||
| echo --- | ||
| # stdout-json: "-\n--\n---\n" | ||
| ### echo -en | ||
| echo -en 'abc\ndef\n' | ||
| # stdout-json: "abc\ndef\n" | ||
| # N-I dash stdout-json: "-en abc\ndef\n\n" | ||
| ### echo -ez (invalid flag) | ||
| # bash differs from the other two shells, but its behavior is possibly more | ||
| # sensible, if you're going to ignore the error. It doesn't make sense for the | ||
| # 'e' to mean 2 different things simultaneously: flag and literal to be | ||
| # printed. | ||
| echo -ez 'abc\n' | ||
| # stdout-json: "-ez abc\\n\n" | ||
| # BUG dash/mksh stdout-json: "-ez abc\n\n" | ||
| ### Read builtin | ||
| # NOTE: there are TABS below | ||
| read x <<EOF | ||
| A B C D E | ||
| FG | ||
| EOF | ||
| echo "[$x]" | ||
| # stdout: [A B C D E] | ||
| # status: 0 | ||
| ### Read builtin with no newline. | ||
| # This is odd because the variable is populated successfully. OSH/Oil might | ||
| # need a separate put reading feature that doesn't use IFS. | ||
| echo -n ZZZ | { read x; echo $?; echo $x; } | ||
| # stdout-json: "1\nZZZ\n" | ||
| # status: 0 | ||
| ### Read builtin with multiple variables | ||
| # NOTE: there are TABS below | ||
| read x y z <<EOF | ||
| A B C D E | ||
| FG | ||
| EOF | ||
| echo "$x/$y/$z" | ||
| # stdout: A/B/C D E | ||
| # status: 0 | ||
| ### Read builtin with not enough variables | ||
| set -o errexit | ||
| set -o nounset # hm this doesn't change it | ||
| read x y z <<EOF | ||
| A B | ||
| EOF | ||
| echo /$x/$y/$z/ | ||
| # stdout: /A/B// | ||
| # status: 0 | ||
| ### get umask | ||
| umask | grep '[0-9]\+' # check for digits | ||
| # status: 0 | ||
| ### Read -n (with $REPLY) | ||
| echo 12345 > $TMP/readn.txt | ||
| read -n 4 x < $TMP/readn.txt | ||
| read -n 2 < $TMP/readn.txt # Do it again with no variable | ||
| argv.py $x $REPLY | ||
| # stdout: ['1234', '12'] | ||
| # N-I dash stdout: [] | ||
| ### read -r ignores backslashes | ||
| echo 'one\ two' > $TMP/readr.txt | ||
| read escaped < $TMP/readr.txt | ||
| read -r raw < $TMP/readr.txt | ||
| argv "$escaped" "$raw" | ||
| # stdout: ['one two', 'one\\ two'] | ||
0 comments on commit
7fd097b