control-structure-as-command.sh 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/bin/sh
  2. setopt --verbose
  3. rm -rf shell-test 2> /dev/null
  4. mkdir shell-test
  5. cd shell-test
  6. touch a b c
  7. # Can we do logical stuff with control structures?
  8. ls && for $(seq 1) { echo yes > listing }
  9. test "$(cat listing)" = "yes" || echo for cannot appear as second part of '&&' && exit 1
  10. rm listing
  11. # FIXME: This should work!
  12. # for $(seq 1) { echo yes > listing } && echo HELLO!
  13. # test "$(cat listing)" = "yes" || echo for cannot appear as first part of '&&' && exit 1
  14. # rm listing
  15. # Can we pipe things into and from control structures?
  16. ls | if true { cat > listing }
  17. test "$(cat listing)" = "a b c" || echo if cannot be correctly redirected to && exit 1
  18. rm listing
  19. ls | for $(seq 1) { cat > listing }
  20. test "$(cat listing)" = "a b c" || echo for cannot be correctly redirected to && exit 1
  21. rm listing
  22. for $(seq 4) { echo $it } | cat > listing
  23. test "$(cat listing)" = "1 2 3 4" || echo for cannot be correctly redirected from && exit 1
  24. rm listing
  25. if true { echo TRUE! } | cat > listing
  26. test "$(cat listing)" = "TRUE!" || echo if cannot be correctly redirected from && exit 1
  27. rm listing
  28. cd ..
  29. rm -rf shell-test