Advertisement
spikeysnack

d2u (bashisms)

Aug 27th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.01 KB | None | 0 0
  1. #!/bin/bash
  2. # dos to unix converter
  3. # changes dos line endings to unix line endings
  4.  
  5. # control eol chars
  6. CR="\15"
  7. LF="\32"
  8. CRLF="\15\32"
  9.  
  10.  
  11. DEBUG_MODE=
  12.  
  13. function check_stdin
  14. {
  15.     [[ -t 0 ]] && \
  16.     echo 'STDIN is attached to TTY'
  17.  
  18.     [[ -p /dev/stdin ]] && \
  19.     echo 'STDIN is attached to a pipe'
  20.  
  21.     [[ ! -t 0 && ! -p /dev/stdin ]] && \
  22.     echo 'STDIN is attached to a redirection'
  23. }
  24.  
  25.  
  26. function check_stdout
  27. {
  28.     [[ -t 1 ]] && \
  29.     echo 'STDOUT is attached to TTY'
  30.  
  31.     [[ -p /dev/stdout ]] && \
  32.     echo 'STDOUT is attached to a pipe'
  33.  
  34.     [[ ! -t 1 && ! -p /dev/stdout ]] && \
  35.     echo 'STDOUT is attached to a redirection'
  36. }
  37.  
  38.  
  39. [[ $DEBUG_MODE ]] && echo "DEBUG MODE" && check_stdin  &&  check_stdin  &&  exit
  40.  
  41. DONE=
  42.  
  43. TR=$(which tr)
  44.  
  45. if [[ ! "${TR}" ]]
  46. then
  47.     echo -e "ERROR -- can;t find tr binary in executable path."
  48.     usage
  49.     exit -1
  50. fi
  51.  
  52. if [[ ! $TEMPDIR ]]
  53. then
  54.     TEMPDIR="/tmp"
  55. fi
  56.  
  57.  
  58.  
  59.  
  60. if [ -t 0 ]
  61. then
  62. #    echo "terminal input"
  63.     TERMIN=1
  64. fi
  65.  
  66. if [ -t 1 ]
  67. then
  68. #    echo "terminal output"
  69.     TERMOUT=1  
  70. fi
  71.  
  72.  
  73.  
  74. # don't automatically overwrite files
  75. check_overwrite()
  76. {
  77.     read -p "overwrite existing file ${1} ? [Y]" do_overwrite
  78.    
  79.     do_overwrite=${do_overwrite,,}    # tolower
  80.    
  81.     if [[ $do_overwrite =~ ^(yes|y)$ ]] ; then
  82.     return 1
  83.      else
  84.     return 0
  85.     fi
  86. }
  87.  
  88.  
  89. # help
  90. usage()
  91. {
  92.     cat <<EOF
  93. Usage:   d2u  <file>  
  94.                     if no outfile is specified,  
  95.                     <textfile> is overwritten.
  96.  
  97.          d2u  <inputfile> <outputfile>
  98.          
  99.          [file output] | d2u | [file input]
  100.                     use d2u as a text filter
  101.  
  102. EOF
  103.  
  104. }
  105.  
  106.  
  107.  
  108. if [ "X${1}" == "Xhelp" ] ; then
  109.     usage
  110.     exit 0
  111. fi
  112.  
  113.  
  114. if [ $# -gt 2 ] ; then
  115.     usage
  116.     exit -1
  117. fi
  118.  
  119.  
  120. if [ ! -f "${1}" ]
  121. then
  122.  
  123.     # filter just  tr <stdin> tr <stdout>
  124.     if [[ ! $DONE ]] && [[ $TERMIN ]] && [[ $TERMOUT ]]
  125.     then
  126.     ${TR} -d '\15\32'  
  127.     DONE=1
  128.     fi
  129.    
  130.     # translate a pipe
  131.     if [[ ! $DONE ]] && [[ $TERMOUT ]] && [[ ! $TERMIN ]]
  132.     then
  133.    
  134.     ${TR} -d '\15\32'
  135.    
  136.     DONE=1
  137.     fi
  138.    
  139. fi
  140.  
  141.  
  142.  
  143. # translate a file
  144. if [[ ! $DONE ]] && [[ $TERMIN ]] && [ -f "${1}" ]
  145. then
  146.     newfile="${TEMPDIR}/tr.temp"
  147.     ${TR} -d '\15\32' < "${1}" > "${newfile}"
  148.  
  149.     if [ -e "${newfile}" ]; then
  150.     mv "${1}"  "${1}.tr.old"
  151.     mv "${newfile}"  "${1}"
  152.     fi
  153.     DONE=1
  154. fi
  155.  
  156.  
  157.  
  158.  
  159.  
  160. # translate output from file
  161. if [[ ! $DONE ]] && [ -f "${1}" ] && [[ $TERMOUT ]]
  162. then
  163.     newfile=
  164.     ${TR} -d '\15\32' < "${1}"
  165.     DONE=1
  166. fi
  167.  
  168.  
  169. # make separate translated file from a file
  170. if [[ ! $DONE ]] && [ -f "${1}" ] && [ -f "${2}" ]
  171. then
  172.     newfile="${TEMPDIR}/tr.temp"
  173.     ${TR} -d '\15\32' < "${1}" > "${newfile}"
  174.  
  175.     if [ -e "${newfile}" ]; then
  176.     check_overwrite "${2}"
  177.     result=$?  #result of last function
  178.     if [[ $result -eq 1 ]] ; then
  179.         mv "${newfile}"  "${2}"    
  180.     else
  181.         echo "creating file ${2}.d2u"
  182.         mv "${newfile}"  "${2}.d2u"    
  183.     fi
  184.  
  185.     fi
  186.     DONE=1
  187. fi
  188.  
  189.  
  190. #END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement