The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,867 other followers

Archive for the ‘bash’ Category

bash – How to add a progress bar to a shell script? – Stack Overflow

Posted by jpluimers on 2020/03/17

From [WayBackbash – How to add a progress bar to a shell script? – Stack Overflow (thanks Mitch Haile!):

You can implement this by overwriting a line. Use \r to go back to the beginning of the line without writing \n to the terminal.

Write \n when you’re done to advance the line.

Use echo -ne to:

  1. not print \n and
  2. to recognize escape sequences like \r.

Here’s a demo:

echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
echo -ne '#######################   (100%)\r'
echo -ne '\n'


Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, Software Development | Leave a Comment »

linux – Bash: Command grouping (&&, ||, …) – Stack Overflow

Posted by jpluimers on 2020/02/18

Excellent answer at [WayBack] linux – Bash: Command grouping (&&, ||, …) – Stack Overflow by Charles Duffy:

Operator precedence for && and || is strictly left-to-right.


pwd; (ls) || { cd .. && ls student/; }  && cd student || cd / && cd ;

…is equivalent to…

pwd; { { { (ls) || { cd .. && ls student/; }; } && cd student; } || cd /; } && cd ; }

…breaking that down graphically:

pwd; {                                      # 1
       {                                    # 2
         { (ls) ||                          # 3
                   { cd .. &&               # 4
                              ls student/;  # 5
                   };                       # 6
         } && cd student;                   # 7
       } || cd /;                           # 8
     } && cd ;                              # 9
  1. pwd happens unconditionally
  2. (Grouping only)
  3. ls happens (in a subshell) unconditionally.
  4. cd .. happens if (3) failed.
  5. ls student/ happens if (3) failed and (4) succeeded
  6. (Grouping only)
  7. cd student happens if either (3) succeeded or both (4) and (5) succeeded.
  8. cd / happens if either [both (3) and one of (4) or (5) failed], or [(7) failed].
  9. cd happens if (7) occurred and succeeded, or (7) occurred and succeeded.

Using explicit grouping operators is wise to avoid confusing yourself. Avoiding writing code as hard to read as this is even wiser.


Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, Software Development | Leave a Comment »

bash: converting numbers to human readable SI or IEC units

Posted by jpluimers on 2019/12/03

Many unix tools that report sizes in bytes can convert them to either IEC or SI readable formats.

For I wrote about last week, I also wanted that kind of behaviour. So I did some research and came up with the code and test cases below.

Note that depending on the bitness of your system, bash integer numeric values are limited in size; see [WayBack] What is the maximum value of a numeric bash shell variable? – Super User.

So I wrote a small bash script for that too, which needed also gave me the opportunity to show how a  perpetual while loop as explained by [WayBack] bash – “while :” vs. “while true” – Stack Overflow.

Two things that always bite me with these short scripts are expressions (done through [WayBack]Arithmetic Expansion) and comparisons (through[WayBack] Other Comparison Operators).

The IEC suffixes contain one extra i to indicate binary and – next to the ISO notation that were already ISO defined – made it into the ISO 80000 standard since 2008. Here is a comparison list from [WayBackBinary prefix – Wikipedia:

Prefixes for multiples of
bits (bit) or bytes (B)
Value SI
1000 k kilo
10002 M mega
10003 G giga
10004 T tera
10005 P peta
10006 E exa
10007 Z zetta
10008 Y yotta
1024 Ki kibi K kilo
10242 Mi mebi M mega
10243 Gi gibi G giga
10244 Ti tebi
10245 Pi pebi
10246 Ei exbi
10247 Zi zebi
10248 Yi yobi

Most tools nowadays default to binary IEC suffixes for byte sizes, though disk manufacturers still use SI suffixes because, well then they appear bigger but aren’t. Just for comparison, look at the numbers from [WayBack] File size – Wikipedia and [WayBack] IEC and SI Size Notations – AN!Wiki where I got the test cases from:

Read the rest of this entry »

Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, Software Development | Leave a Comment »

some links on bash and optional parameters

Posted by jpluimers on 2019/11/26

Hopefully I’ve been able to integrate some of the ideas in the links below in

One of the features I wanted there was to be able to add optional switches like --raw, --iec or --si to it similar to what as the btrfs qgroup show subcommand has.

It seems possible with bash, but it is not trivial, at least not for me as a non-frequent bash user, so here are some links to get me started:

In retrospect, other languages than bash might have been a better choice for a script like that (:


PS, some btrfs references:

Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, Software Development | Leave a Comment »

xargs compressing lots of files using xz

Posted by jpluimers on 2019/11/20

One day, on a legacy Linux system, logrotate managed to skip some files in the middle of a sequence in /var/log.

Since I didn’t have time to sort out the cause (the system was being phased out), I used this to compress the rest of the log-files (dated in 2017):

sudo -u bash
pushd /var/log
ls | grep -vw xz | grep "\-20......$" | xargs -L 1 ls -alh

After that you can execute this in the same directory:

ls | grep -vw xz | grep "\-20......$" | xargs -L 1 time xz

It skips any xz files and includes only files in the year 2017.

I occasionally tracked progress with this:

ls -alh /var/log/ | grep -v xz | less

That got back a few gigabytes of disk space, just enough to help me migrate the system away.


Posted in *nix, *nix-tools, bash, logrotate, Power User | Leave a Comment »

%d bloggers like this: