« first day (5036 days earlier)      last day (176 days later) » 

13:43
@hymcode answering here so as not to clutter up the comments. Frankly, one of the best resources out there for shell scripting is this very site, Unix & Linux. Especially (although not exclusively) answers by our high rep users. I also find Grey Cat's wiki to be excellent.
@terdon might catch some crap for this but I think chatgpt is the best resource for all coding learning out there right now with the caveat that it does lie so answers need to be cross referenced on sites like this.
Ah true, could very well be a useful resource.
If asked to do something with files, does it handle whitespace?
For me to learn a language I need someone to be able to ask 100s of questions, often the same question a dozen times before it sticks. With powershell I had a friend that was patient enough to help me along but with chatgpt you don't have to feel bad every time you have to ask "How do I get this value out of this dict nested inside this list in python" for the 10th time
@terdon I can't think of a trick question to ask it right now, you have one?
@jesse_b "write a bash loop that copies the oldest three files of one directory to another"?
No trick, just basic file management stuff where most humans also would write for file in *; do cp $file dir/; done. I'm just wondering if chatGPT would at least get that much right. I suspect it would, there's enough info out there for that, I hope.
14:00
:/
#!/bin/bash

# Source and destination directories
source_directory="/path/to/source_directory"
destination_directory="/path/to/destination_directory"

# Ensure the destination directory exists
mkdir -p "$destination_directory"

# Find and copy the oldest three files
find "$source_directory" -type f -printf '%T+ %p\n' | sort | head -n 3 | cut -d' ' -f2- | while read file; do
    cp "$file" "$destination_directory"
    echo "Copied $file to $destination_directory"
done
it definitely does some strangely wrong stuff but it's also surprised me more than a few times by optimizing my code in ways I never would have thought to do myself
I can't think of the exact scenario that was most impressive but it solved a pretty big x-y problem for me without me even telling it what z was
14:26
that's cool, but yeah, the code there has some issues. Could have been worse though, at least it quotes.
better than most humans, for sure!
I asked it to handle filenames with newlines in it and it changed it to print 0
find "$source_directory" -type f -printf '%T+ %p\0' | sort -z | head -z -n 3 | cut -z -d' ' -f2- | tr '\0' '\n' | while IFS= read -r file; do
    cp "$file" "$destination_directory"
    echo "Copied $file to $destination_directory"
done
also explains everything and allows you to ask follow up questions in a way that is very useful:

Explanation of changes:

find "$source_directory" -type f -printf '%T+ %p\0' prints the modification time and file path, separated by a null character.
sort -z sorts the output using null characters as delimiters.
head -z -n 3 selects the first three entries, handling null characters.
cut -z -d' ' -f2- extracts the file paths, again using null characters.
tr '\0' '\n' converts null characters back to newlines for processing in the while loop.
Dammit, that does look damn near perfect.
The only improvement I can think of is to pull a Stéphane and make it cp -- "$file" "$dest" to handle file names starting with -.
The biggest difference though is that askers generally wont know the right questions to ask to get the correct answer where users on this site will know most of the pitfalls and point them out in answers
14:42
But the read loop means it doesn’t handle newlines in file names :-/
Yeah. And also picking up minor issues such as the fact that filenames containing - is fine, it's only when they _start with - that it can be a problem.
@StephenKitt ah duh, yes, no -d''
It does a whole lot of work to handle newlines and then throws all that work away /o\
hah
yeah I like the people who think gpt is going to start writing it's own code and become super AI
10 ms after we allow it to write it's own code it's going to mem overflow and shut down
it can write a lot of good code but it has also unequivocally demonstrated that it is incapable of only writing good code
Nevermind, they fixed that one
(which is larger, 3.9 or 3.11)
The explanation is bogus, though:
> Even though 3.11 might appear to be larger because of the digits after the decimal point (11 vs 9), in decimal notation, the number with more digits to the left of the decimal point is larger. Therefore, 3.9 (which can be read as "three point nine") is greater than 3.11 ("three point eleven").
But Linux 3.11 is newer than 3.9 :-P
14:57
Haha, got it:
I have spent a lot of time working on comparing firmware versions
This is what I came up with
compare_firmware_version () {
	local _curver=$1
	local _targetver=$2
	local _f=$(awk -F\. '{print NF}' <<<"$_curver")
	local _cn _tn
	for ((i=1;i<=_f;i++)); do
		unset _cn _tn
		_cn=$(awk -F\. -v f=$i '{print $f}' <<<"$_curver")
		_cn=$(printf '%05s' "$_cn")
		_tn=$(awk -F\. -v f=$i '{print $f}' <<<"$_targetver")
		_tn=$(printf '%05s' "$_tn")
		if [[ $_cn < $_tn ]]; then
			echo "behind"
			return 1
		elif [[ $_cn > $_tn ]]; then
			echo "ahead"
			return 1
		fi
	done
	echo "equal"
	return 0
}
15:12
GNU sort can sort versions but it didn't handle all my needs (I think it fails when versions contain letters)
Also just now realizing I could use printf within awk to 0 pad the numbers instead of the unnecessary additional printf
Why do you need to 0-pad the components?
I do lexicographic comparison to handle letters and it cares about padding
01 < 1
Ah right so you do
I mainly do firmware management for work so I have to compare firmware versions across dozens of vendors and components and there is very little standardization but so far that function handles anything I've encountered
@StephenKitt do you work directly with your cloud stuff?
@jesse_b what do you mean by work directly? I don’t manage clusters that use my stuff, if that’s what you’re asking, except for test purposes
15:25
@StephenKitt You use the infrastructure though?
I just commit directly in Kubernetes and the world tests for me
I guess you guys use all the cloud vendors though huh
@jesse_b all our CI runs on OpenShift, yes
and we test across a lot of vendors
I use on-premise clusters, AWS, and GCP
You all don't list us on your site though :(
15:33
I actually got one of those GPUs when we were decommissioning those servers
I've been meaning to setup a new plex server with it
15:49
well let me know if you ever need me to kick support for you.
16:15
@ChrisDavies I know very, very little about Kali, but its docs claim it "contains systemd hooks that disable network services by default.". If I understood you correctly, this is not the case in the default Kali WSL image (or whatever this is) and the WSL Kali does have networking enabled?
16:31
@jesse_b I've answered 8k+ questions here. I might be of service?
The zsh shell makes it so much easier to pick out the three oldest files beneath a directory: `files=( $source_directory/**/*(.Om[1,3]) )`

EDIT: `cp $source_directory/**/*(.Om[1,3]) $destination_directory/`
@Kusalananda You're a very big reason I know bash so thank you
I don't really know why people try do do it in bash...
and given how well I know bash you might not be so proud of that lol
:-)
You're doing well.
@Kusalananda I have yet to take the plunge and switch to zsh.
16:39
@terdon It's not too much of a "plunge" actually. It's just the same thing as bash, PLUS a whole lot of extra that you'll learn with time.
"same thing" = "similar thing"
I know, I know. But I need to transfer my tweaks and aliases and functions (most of which will be portable, but some won't) and take the time to learn it. I know bash well enough to not need to think about most things. I've been meaning to switch to zsh for ages but never find the time.
I know what you mean. I did a whole lot of switching between various shell a number of years back, and finally realized I kinda enjoyed the challenge of setting up my environment with as few shell-specific extras as possible. I have a total of 1 alias, which is alias ls='ls -F'.
Ah... um, no :) No way! There are some I use all the time, and loads of functions I use for work on a daily basis.
terdon@oregano ~ $ grep -c alias ~/.bashrc
65
including such gems as
alias gerp='grep'
I have
dad = !curl icanhazdadjoke.com && git add
in my ~/.gitconfig
@terdon Ho hum, the zsh shell corrects your misspelt commands for you! :-)
@StephenKitt :-)
@terdon I'd be seriously surprised if most of them did not work in zsh with no change.
16:54
oregano% echo aa | gerp a
zsh: command not found: gerp
@StephenKitt ha!
@Kusalananda oh, the aliases should all work. Even the functions, most likely. Frankly, I doubt it would take me more than half an hour tops to migrate.
I just need to do it, is all.
@terdon setopt CORRECT
eeyore% echo aa | gerp a
zsh: correct 'gerp' to 'grep' [nyae]? y
aa
yeah, yeah, tried it. I need to switch, I know.
:-P
You do whatever you feel is right terdon, as always.
16:57
Well not even! I feel it is right to switch! It's clearly the better shell, and all of the cool kids are using it, dammit!
that or fish, apparently
@terdon Looks like there's network enabled in WSL Kali by default, yes. No raw access to network interfaces, so I'm not entirely sure what use Kali is in this situation
I have a colleague who's using fish. I've tried a couple of time, but the syntax of everythingis so different that I find it a bit cumbersome to deal with.
@ChrisDavies Thanks. And yes, whatever Windows dev thought that Kali was a good choice has a lot to answer for. For one thing, if they ever show their face on U&L they will be tarred and feathered!
@Kusalananda yeah, that one's properly different. Unlike zsh/bash which are very close.
17:01
There's Debian and Ubuntu, so we do have alternatives to punt people towards
Oh, Debian as well? I thought it was only Ubuntu and Kali.
But still, WHY KALI?!
@terdon Yeah, that one is a bit odd. It would make more sense to provide Debian, Ubuntu, and something light, like Alpine.
You can use pretty much any distro in WSL apparently
@StephenKitt But there's a few that are "official"?
Whatever terdon might think, I don't have a Windows machine for testing...
Official - Arch, Debian, Ubuntu, Kali, Fedora, Alpine
17:08
Alpine Linux is currently my favourite Linux distribution, followed by Debian. We run it in VMs at work, for internal services, hosted on top of Debian.
@Kusalananda ha!
I haven't tried Alpine. Hmm, actually, I haven't switched distro in more than 8 years. Might be time.
 
1 hour later…
18:31
we use alpine for our preinstall and recovery environments, it's very lightweight

« first day (5036 days earlier)      last day (176 days later) »