fork download
  1. #!/bin/bash
  2.  
  3. set -- -l . # for test purposes: set argument list to long-list current directory
  4.  
  5. usage() {
  6. printf >&2 'USAGE: %s [-l] path\n' "$0"
  7. exit 2
  8. }
  9.  
  10. long=0
  11.  
  12. while getopts 'l' option; do
  13. case "$option" in
  14. l) long=1 ;;
  15. *) usage ;;
  16. esac
  17. done
  18.  
  19. shiftval=$(( OPTIND - 1 ))
  20. if (( $# < shiftval )); then
  21. usage
  22. fi
  23. shift "$shiftval"
  24.  
  25. if (( $# != 1 )); then # 1 is number of required positional arguments
  26. usage
  27. fi
  28.  
  29. path="$1"
  30.  
  31. if (( long == 1 )); then
  32. ls -l -- "$path"
  33. else
  34. ls -- "$path"
  35. fi
Success #stdin #stdout 0.02s 5308KB
stdin
Standard input is empty
stdout
total 4
-rw-rw-r-- 1 nobody nobody 497 2014-11-03 21:55 prog.sh