valid.sh 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/bin/sh
  2. # Are comments ignored?
  3. # Sanity check: can we do && and || ?
  4. true || exit 2
  5. false
  6. # Apply some useful aliases
  7. fail() {
  8. echo $*
  9. exit 1
  10. }
  11. # Can we chain &&'s?
  12. false && exit 2 && fail "can't chain &&'s"
  13. # Proper precedence between &&'s and ||'s
  14. false && exit 2 || true && false && fail Invalid precedence between '&&' and '||'
  15. # Sanity check: can we pass arguments to 'test'?
  16. test yes = yes || exit 2
  17. # Sanity check: can we use $(command)?
  18. test "$(echo yes)" = yes || exit 2
  19. # Redirections.
  20. test -z "$(echo foo > /dev/null)" || fail direct path redirection
  21. test -z "$(echo foo 2> /dev/null 1>&2)" || fail indirect redirection
  22. test -n "$(echo foo 2> /dev/null)" || fail fds interfere with each other
  23. # Argument unpack
  24. test "$(echo (yes))" = yes || fail arguments inside bare lists
  25. test "$(echo (no)() yes)" = yes || fail arguments inside juxtaposition: empty
  26. test "$(echo (y)(es))" = yes || fail arguments inside juxtaposition: list
  27. test "$(echo "y"es)" = yes || fail arguments inside juxtaposition: string
  28. # String substitution
  29. foo=yes
  30. test "$(echo $foo)" = yes || fail simple string var lookup
  31. test "$(echo "$foo")" = yes || fail stringified string var lookup
  32. # List substitution
  33. foo=(yes)
  34. # Static lookup, as list
  35. test "$(echo $foo)" = yes || fail simple list var lookup
  36. # Static lookup, stringified
  37. test "$(echo "$foo")" = yes || fail stringified list var lookup
  38. # Dynamic lookup through static expression
  39. test "$(echo $'foo')" = yes || fail dynamic lookup through static exp
  40. # Dynamic lookup through dynamic expression
  41. ref_to_foo=foo
  42. test "$(echo $"$ref_to_foo")" = yes || fail dynamic lookup through dynamic exp
  43. # More redirections
  44. echo test > /tmp/sh-test
  45. test "$(cat /tmp/sh-test)" = test || fail simple path redirect
  46. rm /tmp/sh-test
  47. # 'brace' expansions
  48. test "$(echo x(yes no))" = "xyes xno" || fail simple juxtaposition expansion
  49. test "$(echo (y n)(es o))" = "yes yo nes no" || fail list-list juxtaposition expansion
  50. test "$(echo ()(foo bar baz))" = "" || fail empty expansion
  51. # Variables inside commands
  52. to_devnull=(>/dev/null)
  53. test "$(echo hewwo $to_devnull)" = "" || fail variable containing simple command
  54. word_count=(() | wc -w)
  55. test "$(echo well hello friends $word_count)" -eq 3 || fail variable containing pipeline
  56. # Globs
  57. mkdir sh-test
  58. pushd sh-test
  59. touch (a b c)(d e f)
  60. test "$(echo a*)" = "ad ae af" || fail '*' glob expansion
  61. test "$(echo a?)" = "ad ae af" || fail '?' glob expansion
  62. glob_in_var='*'
  63. test "$(echo $glob_in_var)" = '*' || fail substituted string acts as glob
  64. test "$(echo (a*))" = "ad ae af" || fail globs in lists resolve wrong
  65. test "$(echo x(a*))" = "xad xae xaf" || fail globs in lists do not resolve to lists
  66. test "$(echo "foo"a*)" = "fooad fooae fooaf" || fail globs join to dquoted strings
  67. popd
  68. rm -fr sh-test
  69. # Setopt
  70. setopt --inline_exec_keep_empty_segments
  71. test "$(echo -n "a\n\nb")" = "a b" || fail inline_exec_keep_empty_segments has no effect
  72. setopt --no_inline_exec_keep_empty_segments
  73. test "$(echo -n "a\n\nb")" = "a b" || fail cannot unset inline_exec_keep_empty_segments