Archive for the ‘bash’ Category
Posted by jpluimers on 2020/06/03
I read [WayBack] Cursor Movement earlier than [WayBack] Colours and Cursor Movement With tput and [WayBack] The Floating Clock Prompt.
So in one of my scripts I’ve now used an escape sequence, but I might change it to tput in a future version:
## Move one line up, then write finished scripts:
echo -e "\033[1A$finished\r"
I would probably have started with put if I had read [WayBack] bash – Set or change vertical position of the cursor – Stack Overflow first.
–jeroen
Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2020/06/02
I hardly do this, so I tend to forget that unset -f functionname deletes a function and unset variablename or unset -v variablename deletes a variable.
From:
I have done this:bash $ z() { echo ‘hello world’; }How do I get rid of it?
Source: [WayBack] How do I delete a Bash function? – Stack Overflow
Reference: [WayBack] unset Man Page – Bash – SS64.com
–jeroen
Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2020/05/21
Almost 20 years old, but still a very nice read [Archive.is] David Korn Tells All – Slashdot.
Another funny story involving David Korn during the not-so open source times of Microsoft late last century: [WayBack] Korn Shell Story
–jeroen
Posted in *nix, *nix-tools, bash, bash, Development, History, Power User, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2020/03/17
From [WayBack] bash – 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:
- not print
\n and
- 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'
–jeroen
Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, Software Development | Leave a Comment »
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.
Thus:
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
pwd happens unconditionally
- (Grouping only)
ls happens (in a subshell) unconditionally.
cd .. happens if (3) failed.
ls student/ happens if (3) failed and (4) succeeded
- (Grouping only)
cd student happens if either (3) succeeded or both (4) and (5) succeeded.
cd / happens if either [both (3) and one of (4) or (5) failed], or [(7) failed].
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.
–jeroen
Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, Software Development | Leave a Comment »
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 github.com/jpluimers/btrfs-du/blob/master/btrfs-du 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 [WayBack] Binary prefix – Wikipedia:
Prefixes for multiples of
bits (bit) or bytes (B) |
|
|
|
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 »
Posted by jpluimers on 2019/11/26
Hopefully I’ve been able to integrate some of the ideas in the links below in github.com/jpluimers/btrfs-du/blob/master/btrfs-du
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 (:
–jeroen
PS, some btrfs references:
Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, Software Development | Leave a Comment »
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.
–jeroen
Posted in *nix, *nix-tools, bash, logrotate, Power User | Leave a Comment »
Posted by jpluimers on 2019/08/26
This looks smart
gem update `gem list | cut -d ' ' -f 1`
From: [WayBack] How to Update All Your Ruby Gems At Once | Life, the Universe, and Everything
Though on the bash prompt, it works fine on Mac OS X / OS X / macOS / …, it does not work nice as an alias.
You can get it to work with difficult escaping (or nesting).
But it is easier to escape this:
gem update $(gem list | cut -d ' ' -f 1)
Escaped, it comes down to:
alias "gem-update-all=gem update \$(gem list | cut -d ' ' -f 1)"
Based on:
–jeroen
Posted in Apple, bash, bash, Development, Mac OS X / OS X / MacOS, Power User, Scripting, Software Development | Leave a Comment »