0

I'm working on a script to cipher a bunch of folders through the use of tar, gzip and gpg, plus pv with du and awk to keep track of progress. Here is the line that causes problems

tar cf - "$f" | pv -s $(($(du -sk "$f" | awk '{print $1}') * 1024)) | gzip | gpg -e -o "$output/$(basename "$f").tar.gz.gpg"

This works well most of the time. However, if the output file already exists, gpg prompts the user, asking if we want to override the file or not. And in this case, when the script exits, the console kind of breaks: what I type does not appear anymore, pressing Enter does not create a new line, and so on.

The problem does not appear if the outfile does not exist yet, nor if the -s option of pv is missing or computed without du and awk (ex: $((500 * 500)). This won't break the console, but obviously the progress bar would be completely off)

The problem is reproducable even by using this command line outside of a script and replacing $f and $output with desired options.

Zone04
  • 21
  • 5

1 Answers1

1

Perhaps one or a combination of these changes will help.

  1. Change the gpg command to write to stdout, redirected to the file you want: gpg -e -o - > "$output/$(basename "$f").tar.gz.gpg".
  2. Calculate the file size with stat: stat -c "%s" "$f".

The whole line might then look like this:

tar cf - "$f" | pv -s $(stat -c "%s" "$f") | gzip | gpg -e -o - > "$output/$(basename "$f").tar.gz.gpg"
soith
  • 86
  • 3
  • The first option would certainly work, but then I would lose all possibility of interactively handling the case where output file exists. The second option could work if I was trying to cipher individual files, but I'm doing it with whole folders. Hence the use of du to get the combined size of the whole folder – Zone04 Dec 10 '21 at 15:13
  • 1
    What happens if you calculate the size prior to the tar-pipeline, e.g., `tar_bytes=$(du -sk $f | awk '{print $1*1024}')` (or `tar_bytes=$(du -sb $f | cut -f1)`)? – soith Dec 13 '21 at 09:48
  • This did the trick! I still don't understand why it was creating this weird glitch when I was trying to overwrite a file but hey, now it works! – Zone04 Dec 13 '21 at 22:31