4

I want to use the output of shred to make a progress bar with zenity.

My shred command looks like this.

sudo shred -vfn 1 /dev/sda

I am new to Linux. So I probably miss an obvious solution.

Raffa
  • 24,905
  • 3
  • 35
  • 79
BlackRed
  • 49
  • 2

1 Answers1

7

A progress bar is easy ... I guess what you are asking about is how to make it update and reflect the output status from shred ... Well, you can use something like this:

shred -vfn 1 file_toshred |& \
while read -r line; do awk '{print $NF+0}' <<<"$line"; done | \
zenity --progress

Notice: I changed sudo shred -vfn 1 /dev/sda to shred -vfn 1 file_toshred for safety reasons of folks testing the code, but that would work as well.

The |& will pipe stderr(needed for parsing shred's output) as well as stdout in Bash ... For other shells not supporting it you might use 2>&1 instead and probably change the herestring(<<<"$line") syntax to an echo "$line" | ... like so:

shred -vfn 1 file_toshred 2>&1 | \
while read -r line; do echo "$line" | \
awk '{print $NF+0}'; done | \
zenity --progress

To print the output text as well, you can add echo "# $line"; inside the while ... loop and you might want to expand the zenity window to accommodate the output by setting the --width= like so:

shred -vfn 1 file_toshred 2>&1 | \
while read -r line; do echo "$line" | \
awk '{print $NF+0}'; echo "# $line"; done | \
zenity --progress --width="500"
Raffa
  • 24,905
  • 3
  • 35
  • 79
  • A pipe continues to the next line, you don't need to escape the newline with ``\``. – muru Jul 13 '23 at 07:01
  • @muru True ... The escapes, however, are for compatibility and history ... Not all shells handle multi-line commands equally, I think ... See for example [multiline command chunks in bash history into multiple lines](https://askubuntu.com/q/1133015) – Raffa Jul 13 '23 at 07:19
  • Thank you. The second one did work I only had to put ( ) around everything before " | zenity --progress". I also deleted $NF+0 because the progress bar took the values to move the bar forward but I only wanted to get the output of shred in the text section of --progress. – BlackRed Jul 17 '23 at 08:07
  • @BlackRed You can show both ... I updated the answer for that ... And I don't see why you might need to use the sub-shell `( ... )` syntax for that – Raffa Jul 17 '23 at 09:51