Advertisement
bash-masters

bashfill

Oct 24th, 2012
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.80 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # synopsis:  bashfill FILE ... [-- TEMPLATEOPTIONS ... ]
  4.  
  5. function include() {
  6.     lastline=$(tail -n 1 $1; echo x);
  7.     lastline=${lastline%x};
  8.     sanitize='';
  9.     [[ "${lastline: -1}" != $'\n' ]] && sanitize=$'\n';
  10.     read; [[ "$REPLY" =~ ^'#!' ]] || {
  11.     cat <(echo "$REPLY") - <(echo -n "$sanitize");
  12.     } && cat - <(echo -n "$sanitize");
  13. }
  14.  
  15. declare -a BASHFILL;
  16.  
  17. while [[ $1 != '--' ]] && [[ $# -gt 0 ]]; do
  18.     BASHFILL[index++]=$1; shift;
  19. done
  20.  
  21. [[ $1 == '--' ]] && shift;
  22.  
  23. source <(echo 'cat <<ENDOFASSEMBLY';
  24. for (( order=0; order != index; ++order )); do
  25. [[ -e "${BASHFILL[order]}" ]] && {
  26. include "${BASHFILL[order]}" < "${BASHFILL[order]}";
  27. } || { [[ -n "${BASHFILL[order]}" ]] &&
  28. echo ${BASHFILL[0]}: error: unable to include $(pwd)/${BASHFILL[order]} >&2;
  29. }; done; echo 'ENDOFASSEMBLY');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement