sh.snippets 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Shebang. Executing bash via /usr/bin/env makes scripts more portable.
  2. snippet #!
  3. #!/usr/bin/env bash
  4. snippet if
  5. if [[ ${1:condition} ]]; then
  6. ${2:#statements}
  7. fi
  8. snippet elif
  9. elif [[ ${1:condition} ]]; then
  10. ${2:#statements}
  11. snippet for
  12. for (( ${2:i} = 0; $2 < ${1:count}; $2++ )); do
  13. ${3:#statements}
  14. done
  15. snippet fori
  16. for ${1:needle} in ${2:haystack} ; do
  17. ${3:#statements}
  18. done
  19. snippet wh
  20. while [[ ${1:condition} ]]; do
  21. ${2:#statements}
  22. done
  23. snippet until
  24. until [[ ${1:condition} ]]; do
  25. ${2:#statements}
  26. done
  27. snippet case
  28. case ${1:word} in
  29. ${2:pattern})
  30. ${3};;
  31. esac
  32. snippet go
  33. while getopts '${1:o}' ${2:opts}
  34. do
  35. case $$2 in
  36. ${3:o0})
  37. ${4:#staments};;
  38. esac
  39. done
  40. # Set SCRIPT_DIR variable to directory script is located.
  41. snippet sdir
  42. SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  43. # getopt
  44. snippet getopt
  45. __ScriptVersion="${1:version}"
  46. #=== FUNCTION ================================================================
  47. # NAME: usage
  48. # DESCRIPTION: Display usage information.
  49. #===============================================================================
  50. function usage ()
  51. {
  52. cat <<- EOT
  53. Usage : $${0:0} [options] [--]
  54. Options:
  55. -h|help Display this message
  56. -v|version Display script version
  57. EOT
  58. } # ---------- end of function usage ----------
  59. #-----------------------------------------------------------------------
  60. # Handle command line arguments
  61. #-----------------------------------------------------------------------
  62. while getopts ":hv" opt
  63. do
  64. case $opt in
  65. h|help ) usage; exit 0 ;;
  66. v|version ) echo "$${0:0} -- Version $__ScriptVersion"; exit 0 ;;
  67. \? ) echo -e "\n Option does not exist : $OPTARG\n"
  68. usage; exit 1 ;;
  69. esac # --- end of case ---
  70. done
  71. shift $(($OPTIND-1))