function.sh 707 B

1234567891011121314151617181920212223242526
  1. #!/bin/sh
  2. # Syntax ok?
  3. fn() { echo $* }
  4. # Can we invoke that?
  5. test "$(fn 1)" = 1 || echo cannot invoke "'fn 1'" && exit 1
  6. test "$(fn 1 2)" = "1 2" || echo cannot invoke "'fn 1 2'" && exit 1
  7. # With explicit argument names?
  8. fn(a) { echo $a }
  9. # Can we invoke that?
  10. test "$(fn 1)" = 1 || echo cannot invoke "'fn 1'" with explicit names && exit 1
  11. test "$(fn 1 2)" = 1 || echo cannot invoke "'fn 1 2'" with explicit names and extra arguments && exit 1
  12. # Can it fail?
  13. if fn 2>/dev/null {
  14. echo "'fn'" with an explicit argument is not failing with not enough args
  15. exit 1
  16. }
  17. # $0 in function should be its name
  18. fn() { echo $0 }
  19. test "$(fn)" = fn || echo '$0' in function not equal to its name && exit 1