loop.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/bin/sh
  2. singlecommand_ok=yes
  3. multicommand_ok=yes
  4. inlineexec_ok=yes
  5. implicit_ok=yes
  6. # Full form
  7. # Empty
  8. for x in () { }
  9. # Empty block but nonempty list
  10. for x in (1 2 3) { }
  11. # Single command in block
  12. for cmd in ((test 1 = 1) (test 2 = 2)) {
  13. $cmd || unset singlecommand_ok
  14. }
  15. # Multiple commands in block
  16. for cmd in ((test 1 = 1) (test 2 = 2)) {
  17. test -z "$cmd"
  18. test -z "$cmd" && unset multicommand_ok
  19. }
  20. # $(...) as iterable expression
  21. test_file=sh-test-1
  22. echo 1 > $test_file
  23. echo 2 >> $test_file
  24. echo 3 >> $test_file
  25. echo 4 >> $test_file
  26. lst=()
  27. for line in $(cat $test_file) {
  28. lst=($lst $line)
  29. }
  30. test "$lst" = "1 2 3 4" || unset inlineexec_ok
  31. rm $test_file
  32. # Implicit var
  33. for ((test 1 = 1) (test 2 = 2)) {
  34. $it || unset implicit_ok
  35. }
  36. test $singlecommand_ok || echo Fail: Single command inside for body
  37. test $multicommand_ok || echo Fail: Multiple commands inside for body
  38. test $inlineexec_ok || echo Fail: Inline Exec
  39. test $implicit_ok || echo Fail: implicit iter variable
  40. test "$singlecommand_ok $multicommand_ok $inlineexec_ok $implicit_ok" = "yes yes yes yes" || exit 1