News for hackers. Stuff that matters.
Re:Shell Script Best Practices, from a decade of scripting t... by GuB-42 on Thursday October 27, 2022 @07:12AM (#33355895)
I tried PowerShell, hated it. The idea of manipulating objects instead of text streams is interesting, and avoids most of the footguns sh/bash have, but you also lose in flexibility.

One reason is that there are thousands of command line tools in the UNIX ecosystem that process text streams and are designed to work with shells like bash. You have much less options when you are processing PowerShell objects.

Note: I think the first thing I tried to do in PowerShell was a script that scanned a directory recursively for files containing a CRC in their name, and then check it, or something along these lines. After several hours of trying, I simply couldn't do it while it was relatively straightforward in bash, even with spaces in file names.

And that's not that I like UNIX shell scripting, in fact I hate it, so many footguns, that's why I wanted to try PowerShell, but it didn't fit my needs.

Re:Shell Script Best Practices, from a decade of scripting t... by papascrubs on Thursday October 27, 2022 @09:31AM (#33357193)
What things are you not able to do with PowerShell that you can with Bash + utilities? PowerShell literally gives you access to every way you can manipulate a string in C#. I get it, you're familiar with bash. But just because you know how to do something with something you're familiar with, doesn't mean PowerShell can't do what you wanted it to do.

Not trying to be combative, but learning any new language is going to require some dogfooding and digging in order to be as efficient as something you're already familiar with. I use bash and PowerShell daily. I can't think of any one thing I couldn't do in one or the other. Bash usually requires tying in another tool though. It's not strictly bash at that point. People saying stuff like jq. I rarely find that pre-installed. And if it's your machine, you can just as easily install something that doesn't require you to learn multiple languages (jq is a language).

I here you. I've been there. I'd lazily prod you to give POSH another shake. I've no horse in this race but I think you're missing out if you weren't able to accomplish in POSH what you were able to do in bash.

Re:Shell Script Best Practices, from a decade of scripting t... by HyperSane on Thursday October 27, 2022 @11:11AM (#33358746)
"You have much less options when you are processing PowerShell objects."

There is simply no need for the kind of extensive text processing common in Linux because every command returns an object whose fields can be directly referenced. Combined with the ConvertTo-Json command this is incredibly powerful. Honestly it seems like you are attempting to do things in PowerShell the bash way instead of the PowerShell way.

" I think the first thing I tried to do in PowerShell was a script that scanned a directory recursively for files containing a CRC in their name, and then check it"

I wrote a PowerShell script that recursively scanned every filed and folder on our fileshares and wrote the permissions to a file for later indexing.

Re:Shell Script Best Practices, from a decade of scripting t... by ndsipa_pomu on Thursday October 27, 2022 @12:15PM (#33359804)
I tried to write a PowerShell script to recursively scan and find files/folders older than a certain date, but kept hitting problems with the length of the path/filenames. As a complete PowerShell noob, I'm sure I was trying to do it the wrong way, but after a few attempts, I gave up and install cygwin instead.
Re:Shell Script Best Practices, from a decade of scripting t... by Macha on Thursday October 27, 2022 @12:52PM (#33360452)
Your problem here is with win32 not powershell I suspect. Your script would probably have worked with powershell on Linux, but windows you need to use UNC paths to get a 2^16 - ~20 character path limit rather than the 256 character path limit of regular paths.

Or there's some registry hacks to remove the limit from regular paths.

Re:Shell Script Best Practices, from a decade of scripting t... by ndsipa_pomu on Thursday October 27, 2022 @01:25PM (#33360930)
You're probably right. It was a few years ago and after trying a couple of alternatives, just went with what I know.
Re:Shell Script Best Practices, from a decade of scripting t... by HyperSane on Thursday October 27, 2022 @02:14PM (#33361632)
You can enable long path support in Windows to have paths up to 32,767 characters long

https://learn.microsoft.com/en-us/windows/win32/fileio/maxim...

Re:Shell Script Best Practices, from a decade of scripting t... by ndsipa_pomu on Thursday October 27, 2022 @02:23PM (#33361778)
It was a few years back, so probably before Windows included that ability (Windows 10 onwards?)
Re:Shell Script Best Practices, from a decade of scripting t... by majkinetor on Thursday October 27, 2022 @02:37PM (#33361990)

   ls -File | % { $fh = Get-FileHash $_ SHA1; if ($_.Name -match $fh.hash) {$_} }
Re:Shell Script Best Practices, from a decade of scripting t... by GuB-42 on Thursday October 27, 2022 @03:32PM (#33362823)
Doesn't work, it is not recursive and it was a CRC, not SHA1 but I probably could have found a solution starting from there.

Anyways, it was a long time ago, maybe I will give PowerShell a retry at some point.

Re:Shell Script Best Practices, from a decade of scripting t... by majkinetor on Thursday October 27, 2022 @03:46PM (#33363021)
Ofc. it works. If on linux, replace ls with `gci`. Recursion is done with ls -Recurse. Install-Package CRC. Its still single liner.