Ever burned a cake?
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

85 satır
2.1KB

  1. #!/bin/bash
  2. # Originally written by Anon, modified by Emil to better match Bake functionality
  3. # Issues: sloooow, fails to handle multi-line statements
  4. BLUE='\033[34m'
  5. GREEN='\033[32m'
  6. YELLOW='\033[93m'
  7. DIM='\033[2m'
  8. BOLD='\033[1m'
  9. NORMAL='\033[0m'
  10. MARKNAME="@BAKE"
  11. MARK="${MARKNAME} "
  12. MARKSTR="${GREEN}${MARKNAME}${NORMAL}"
  13. enable -n echo
  14. usage() {
  15. echo -e "$0: [option] ${BOLD}target-file${NORMAL} [${GREEN}arguments${NORMAL} ...]\n"
  16. echo -e "Use the format \`${BOLD}@BAKE${NORMAL} cmd ...' within the ${BOLD}target-file${NORMAL}."
  17. echo -e "This will execute until the end of line, or if existing, until the ${BOLD}@STOP${NORMAL} marker.\n"
  18. echo -e "Options [Must be first]"
  19. echo -e "\t${DIM}-h --help${NORMAL}, ${BOLD}-n --dry-run${NORMAL}\n"
  20. echo -e "Expansions\n"
  21. echo -e "\t${YELLOW}@FILENAME${NORMAL} returns target-file (abc.x.txt)"
  22. echo -e "\t${YELLOW}@SHORT${NORMAL} returns target-file without suffix (^-> abc.x)"
  23. echo -e "\t${YELLOW}@ARGS${NORMAL} returns ${GREEN}arguments${NORMAL}"
  24. }
  25. if [[ $# -lt 1 ]]; then
  26. usage
  27. exit 1
  28. fi
  29. if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
  30. usage
  31. exit 0
  32. fi
  33. run=1
  34. if [[ $1 == "-n" ]] || [[ $1 == "--dry-run" ]]; then
  35. if [[ $# -lt 2 ]]; then
  36. usage
  37. exit 1
  38. fi
  39. run=0
  40. shift 1
  41. fi
  42. input_file=$1
  43. shift 1
  44. if [[ ! -f $input_file ]]; then
  45. echo -e "Input file '$input_file' does not exist." >&2
  46. exit 1
  47. fi
  48. cd $(dirname "$(readlink -f "$input_file")")
  49. input_file=${input_file##*/}
  50. line=$(grep "$MARK" "$input_file" | head -1)
  51. if [[ -n $line ]]; then
  52. line=${line//\$@/$input_file}
  53. line=${line//\$\*/${input_file%.*}}
  54. line=${line//\$+/$@}
  55. line=${line//@FILENAME/$input_file}
  56. line=${line//@SHORT/${input_file%.*}}
  57. line=${line//@ARGS/$@}
  58. line=$(echo "$line" | sed 's/\W*@STOP.*//')
  59. command="${line#*${MARK}}"
  60. echo -e "${BOLD}${GREEN}$0${NORMAL}: $command"
  61. if [[ $run -eq 1 ]]; then
  62. echo -e "${BOLD}${GREEN}output${NORMAL}:"
  63. sh -c "$command"
  64. fi
  65. else
  66. echo -e "${MARKSTR} is not defined." >&2
  67. exit 1
  68. fi