0

I have a shell script that allows a user to add and/or delete files and directories. It's a file Manipulator. The script keeps track of all files that are added or deleted in a log file. I then read the log file line by line and place the files in one of 3 files: dev_deleted.txt, dev_duplicates.txt, or dev_final.txt.

  • If a user adds the same file(s) to a directory twice (duplicates) then the file or directory should be listed in dev_duplicates.txt file.
  • Any file/directory that is deleted should be listed in dev_deleted.txt
  • All other files should be listed in dev_final.txt including the filename, first and last char, and length of the file

  • As you can see in the code below, I read each line in the log file searching for the proper suffix, remove the suffix, and list in dev_deleted.txt, dev_duplicates.txt, or dev_final.txt

dev_deleted.txt works as it should but dev_duplicates.txt and dev_final.txt are not listing the proper files. I ran on my Mac without issues, but now I'm trying to run on Windows 10 OS (I did EOL Conversion to UNIX) and the last case is giving me issues. Can someone help?

I've tried searching for syntax errors

 6)
      echo "List filesystem directory artifacts"
      touch $basedir/dev_final.txt $basedir/dev_deleted.txt $basedir/dev_duplicates.txt
      while IFS= read -r line
      do

          #echo "$line"
          if [[ $line == *"-removed" ]]; then
              fileName=${line%-*}
              echo "$fileName" >> $basedir/dev_deleted.txt

          elif [[ $line == *"-new" ]]; then
               fileName=${line%-*}
               echo "$fileName" >> $basedir/dev_duplicates.txt
          else
               fileName=${line}
               first=${fileName:0:1}
               nameLength=${#fileName}
               last=${fileName:$nameLength-1}
               echo "$fileName, first char:$first, last char:$last, length:$nameLength" >> $basedir/dev_final.txt
          fi
      done < "$logFile"
      ;;
    esac
done

Files with the -new suffix should be listed in dev_duplicates.txt, files with a -removed suffix should be listed in dev_deleted.txt, and all other files (weren't deleted or duplicate) should be listed in dev_final.txt including the filename, first and last char, and length of the file

Toto
  • 89,455
  • 62
  • 89
  • 125
CadenJoe
  • 19
  • 5
  • use `set -x` near the top of your script so you can see what values are assigned to different variables. I don't think, for instance that `last=${fileName:$nameLength-1}` is doing what you think. Good luck. – shellter Sep 05 '19 at 02:41
  • The first line of your code is `6)`. If that is a typo, please remove, else please flesh out more of your script so there is a valid context for that. Good luck. – shellter Sep 05 '19 at 02:42

0 Answers0