Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. set -eo pipefail
  4.  
  5. red=$(printf '\033[1;31m')
  6. yellow=$(printf '\033[1;33m')
  7. purple=$(printf '\033[0;35m')
  8. nocolour=$(printf '\033[0m')
  9.  
  10. usage() {
  11. echo -e "${red}Error: $1${nocolour}\n" >&2
  12. echo -e "Usage:\n\t$0 PROG INFILE OUTFILE LIMIT [SIGNAL,]" >&2
  13. echo -e "Where:" >&2
  14. echo -e "\tPROG: The executable compiled from your shortLines.c program" >&2
  15. echo -e "\tINFILE: A sample text file to use for your program" >&2
  16. echo -e "\tOUTFILE: The file to write out the truncated lines to" >&2
  17. echo -e "\tLIMIT: The maximum line length before truncating" >&2
  18. echo -e "\tSIGNAL: One or more signals to send to PROG" >&2
  19. echo -e "\nEXAMPLE:\n\t$0 ./myProgram poem.txt out.txt 9 USR1 USR2 INT" >&2
  20. exit 1
  21. }
  22.  
  23. test $# -ge 4 ||\
  24. usage "Wrong number of arguments"
  25.  
  26. PROG=$1
  27. INFILE=$2
  28. OUTFILE=$3
  29. LIMIT=$4
  30. FIFO=sig_pipe.fifo
  31.  
  32. test -e "$PROG" ||\
  33. usage "PROG '$PROG' not found. Please provide your program's name as PROG"
  34.  
  35. test -x "$PROG" ||\
  36. usage "PROG '$PROG' is not executable"
  37.  
  38. test -f "$INFILE" ||\
  39. usage "INFILE not found: '$INFILE'. Please provide the path to a plain text file to use as input"
  40.  
  41. if [ -e "$OUTFILE" ] &&\
  42. [ ! -w $OUTFILE ] ; then
  43. usage "OUTFILE is not writable: '$OUTFILE'" ;
  44. fi
  45.  
  46. if [ -e "$FIFO" ] &&\
  47. [ ! -p "$FIFO" ] ; then
  48. usage "Refusing to clobber non-fifo file '$FIFO'. Remove or rename it and try again."
  49. fi
  50.  
  51. # if the fifo doesn't exist, create it
  52. test -e "$FIFO" || mkfifo "$FIFO"
  53.  
  54.  
  55. # Run the program with the fifo as the input file. The program just thinks it's
  56. # a file (woo unix!).
  57. # Reading the fifo blocks the program until we write data to it. This lets us
  58. # send signals in between program line reads in a reliable-ish way.
  59. "$PROG" "$FIFO" "$OUTFILE" "$LIMIT" &
  60. PROG_PID=$!
  61.  
  62.  
  63. # Alternate streaming lines and sending signals into the program
  64. # If there are fewer lines than signals, the extra signals are ignored
  65. SIG_INDEX=5
  66. while read -r line ; do
  67.  
  68. # if the program exited already, don't try to keep sending it lines
  69. if ! ps -p $PROG_PID > /dev/null ; then
  70. break
  71. fi
  72.  
  73. # Write out a line
  74. # It gets redirected to the fifo by piping the loop output through tee
  75. echo $line
  76.  
  77. # If there are more signals to send, send the next one
  78. if [ ! -z ${!SIG_INDEX} ] ; then
  79. sleep 0.1 # hack around race between writing a line and sending signal
  80. echo -e "${yellow} ⚡ ${!SIG_INDEX}${nocolour}" >&2
  81. kill -s ${!SIG_INDEX} $PROG_PID
  82. SIG_INDEX=$((SIG_INDEX+1))
  83. fi
  84.  
  85. done < $INFILE\
  86. | tee "$FIFO"\
  87. | sed "s/.*/${purple} ✎ &${nocolour}/"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement