Re:Advanced Shell Scripting Techniques: Automating Complex T...
by bee_rider on Tuesday June 18, 2024 @09:52PM
(#40724006)
Original is pretty readable. Efficient looks like what I get when I accidentally get shifted over to the right by one column.
Re:Advanced Shell Scripting Techniques: Automating Complex T...
by upon_drumhead on Tuesday June 18, 2024 @09:25PM
(#40723851)
FWIW, on ubuntu 22.04, your "Efficient" doesn't work
# df -h | grep "$partition" | awk '{print $5}' | sed 's/%//'
24
# df -h | grep -Po "\d+(?=%\s+$partition)"
#
Re:Advanced Shell Scripting Techniques: Automating Complex T...
by spicyusername on Tuesday June 18, 2024 @09:40PM
(#40723930)
Easiest to read is what I usually try optimize for, especially in a shell script.
I think the first one is much easier to read.
Re:Advanced Shell Scripting Techniques: Automating Complex T...
by viraptor on Tuesday June 18, 2024 @09:23PM
(#40723831)
To be fair, if I needed something optimal or it was used often enough to matter, I'd probably reach for the original data in a real language. For a one-off, I can tell what grep/awk/sed does immediately - but I need to stop and think for the efficient solution.
Re:Advanced Shell Scripting Techniques: Automating Complex T...
by metadat on Tuesday June 18, 2024 @09:22PM
(#40723827)
Shouldn't the first one be `grep -F/--fixed-strings "${partition}"? The second example will break in any case where $partition contains special characters.
Re:Advanced Shell Scripting Techniques: Automating Complex T...
by oguz-ismail on Tuesday June 18, 2024 @10:05PM
(#40724082)
Yes, it's an easy fix though:
df -h | grep -Po "\d+(?=%\s+\Q$partition\E)"
Re:Advanced Shell Scripting Techniques: Automating Complex T...
by metadat on Tuesday June 18, 2024 @10:19PM
(#40724131)
Oh cool, this is the first time I've heard of `grep \Q..\E'
https://stackoverflow.com/questions/54405892/are-q-and-e-sup...
Thanks!
At some point I should probably write an article about the 20k lines of bash and a little python that power my homelab and various automations. Bash isn't perfect but often 99% good enough is fine.
Re:Advanced Shell Scripting Techniques: Automating Complex T...
by learntopogrom on Wednesday June 19, 2024 @12:58AM
(#40725005)
is this a troll? your "efficient" is awful and you should feel bad
Original: df -h | grep "$partition" | awk '{print $5}' | sed 's/%//'
Efficient: df -h | grep -Po "\d+(?=%\s+$partition)"