checkboss.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/bin/bash
  2. # This script checks all arguments (or if no arguments are given, all files named *.dat in the directory),
  3. # with the exception of records.dat,
  4. # and prompts for cutting lines and characters in excess
  5. # i.e. cuts lines after the $MAX_LINES-th line and characters after the $MAX_CHARS-th character in each line
  6. # The script also adds as many blank spaces as needed to even the character count on each line
  7. # i.e. in the and each line will have $LONGEST_LINE characters in total
  8. # Old boss.dat file is copied to boss.dat.old to prevent data loss. I'm aware this is a coward's walkaround
  9. # to the fact this script has issues.
  10. # KNOWN BUGS:
  11. # - there is no return value checking from the functions called
  12. # - first bug implies that boss.dat file could be substituted with an empty file, or worse
  13. # Please report any bugs or suggestions to me .D
  14. #wc -L is the length of the longest line
  15. #wc -m is the character count for the file
  16. #wc -l is the line count for the file
  17. MAX_CHARS=10
  18. MAX_LINES=10
  19. FILES=""
  20. if [ $# -eq 1 -a $1 == "-h" ]; then echo Usage: $0 \<bossname.dat\>.; exit 1; fi
  21. if [ $# -gt 0 ]; then
  22. #check that file exists
  23. for k in $@; do
  24. if [ ! -e $k -a ! $k.dat ]; then
  25. echo $k does not exist!
  26. echo ignoring argument
  27. continue
  28. else
  29. #check that file has .dat extension
  30. if [[ "$k" =~ dat$ ]]; then
  31. FILES="$FILES $k"
  32. else
  33. if [[ "Sk.dat" =~ dat$ ]]; then
  34. echo File $k is not a .dat, but a file named $k.dat exists. Did you mean that file? \(Y/N\)
  35. read ans
  36. if [ $ans != "y" -a $ans != "Y" ]; then exit 1
  37. else
  38. FILES="$FILES $k.dat"
  39. continue
  40. fi
  41. else
  42. echo $k does not look like a .dat file
  43. echo ignoring argument
  44. continue
  45. fi
  46. fi
  47. fi
  48. done
  49. else
  50. FILES=`ls *.dat 2> /dev/null`
  51. fi
  52. echo $FILES > args.tmp
  53. #FOR EACH boss.dat FILE
  54. for i in `cat args.tmp`; do
  55. if [ $i == "records.dat" -o $i == "notes.dat" ]; then continue; fi
  56. #MAKE A BACKUP COPY
  57. cp $i $i.old
  58. #CUT LINES IN EXCESS
  59. N_LINES=`wc -l $i | cut -f1 -d' '`
  60. if [ $N_LINES -gt $MAX_LINES ]; then
  61. echo file $i has too many lines \($N_LINES\): cut lines in excess? Y\\N
  62. read ans
  63. while [ $ans != 'y' -a $ans != 'Y' -a $ans != 'n' -a $ans != 'N' ]; do
  64. echo cut lines in excess? Y\\N
  65. read ans
  66. done
  67. if [ $ans == 'y' -o $ans == 'Y' ]; then
  68. head -$MAX_LINES $i > output.tmp
  69. mv output.tmp $i
  70. N_LINES=`wc -l $i | cut -f1 -d' '`
  71. echo file has now $N_LINES lines
  72. fi
  73. echo
  74. fi
  75. #CUT CHARACTERS IN EXCESS
  76. LONGEST_LINE=`wc -L $i | cut -f1 -d' '`
  77. if [ $LONGEST_LINE -gt $MAX_CHARS ]; then
  78. echo file $i has at least one line that is longer than permitted. max length is $MAX_CHARS
  79. echo cut characters in excess? Y\\N
  80. read ans
  81. while [ $ans != 'y' -a $ans != 'Y' -a $ans != 'n' -a $ans != 'N' ]; do
  82. echo cut lines in excess? Y\\N
  83. read ans
  84. done
  85. if [ $ans == 'y' -o $ans == 'Y' ]; then
  86. cat $i | cut -c1-$MAX_CHARS > output.tmp2
  87. mv output.tmp2 $i
  88. LONGEST_LINE=`wc -L $i | cut -f1 -d' '`
  89. echo longest line has now $LONGEST_LINE characters
  90. fi
  91. echo
  92. fi
  93. #ADD BLANK SPACES AT THE END OF LINES
  94. for j in `seq 1 $MAX_CHARS`; do
  95. sed 's/.*/& /' $i > output.tmp3
  96. mv output.tmp3 $i
  97. done
  98. cat $i | cut -c1-$LONGEST_LINE > output.tmp4
  99. mv output.tmp4 $i
  100. done
  101. rm args.tmp