scrub_username_from_file.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #/usr/bin/env bash
  2. : '
  3. This program can be used as a convenient way to remove a username
  4. from files that will be shared with the dev team. An example is
  5. pasting a large log file or stack trace. Usernames can appear here
  6. due to e.g. file paths in the home directory. This may not be
  7. desirable.
  8. In this case, you can run this program on the file you want to
  9. share and it will create a copy of it in the current working
  10. directory. The copy will have the extension ".scrubbed".
  11. e.g. running this program as user `rms` in /tmp/:
  12. ```
  13. sh no-dox.sh /etc/passwd
  14. ```
  15. creates the file `/tmp/passwd.scrubbed` with the string `rms`
  16. replaced with the string `user`.
  17. NOTE: It is recommended to verify the output for any mistakes.
  18. This script is simple and is likely to miss some edge-cases.
  19. '
  20. set -e
  21. if [ "$#" -ne 1 ]; then
  22. echo "Usage: $0 <file>"
  23. exit 1
  24. fi
  25. # Replace the current user's name.
  26. ME=$(whoami)
  27. # Uncomment the following line and change 'changeme' if the user
  28. # running the script is not the same user as the one you are
  29. # trying to conceal.
  30. #ME="changeme"
  31. # The string in $ME will be replaced with this string.
  32. REPLACE="user"
  33. FILE="$1"
  34. DST=$(basename $FILE)
  35. if ! [ -f $FILE ]; then
  36. echo "Target should be a file"
  37. exit 2
  38. else
  39. sed s/$ME/$REPLACE/g "$FILE" > "$(pwd)/$DST.scrubbed"
  40. fi