News for hackers. Stuff that matters.
Re:Shell Script Best Practices, from a decade of scripting t... by HyperSane on Thursday October 27, 2022 @06:15AM (#33355503)
sh and bash feel pretty primitive after learning PowerShell.
Re:Shell Script Best Practices, from a decade of scripting t... by majkinetor on Thursday October 27, 2022 @06:17AM (#33355513)
They don't feel primitive, they are primitive.
Re:Shell Script Best Practices, from a decade of scripting t... by l3uwin on Thursday October 27, 2022 @06:54AM (#33355762)
Yep, just as a screwdriver is. For certain jobs, that's all you need.
Re:Shell Script Best Practices, from a decade of scripting t... by majkinetor on Thursday October 27, 2022 @10:08AM (#33357658)
Indeed, except then you have to maintain screwdriver.
Re:Shell Script Best Practices, from a decade of scripting t... by lupire on Thursday October 27, 2022 @10:01AM (#33357581)
I prefer "primal" for old great things.
Re:Shell Script Best Practices, from a decade of scripting t... by orwin on Thursday October 27, 2022 @06:38AM (#33355640)
I feel like powershell hides too much to be used regularly. I have a dozen of small shellscripts and aliases to do basically what PS help me to do when i work on windows (and some), but at least i know how it work behind.

I had to work with Sencha/ExtJS early 2010. It was the same feeling. Yes, it is powerfull, but too much magic happen for something without a clear orientation (at the time, now it is used for data loaded frontend i think). PS i don't understand what it wants me to do.

The language is fine. The interface is now fine, but in 2015 it was the shittiest tty available on modern computers. It's okay since at least early 2021 (when i restarted using windows). I know it should be reasonably better, but i wouldn't trust any PS script written before 2021 to run on my workstation. I run bash scripts i wrote when i started coding.

Still, if you're new to the gig and don't care about free software and commons, you should learn PS (unless you want to work on baremetal or on MC, in this case, bash will be enough).

Re:Shell Script Best Practices, from a decade of scripting t... by Beltalowda on Thursday October 27, 2022 @07:14AM (#33355911)
> I had to work with Sencha/ExtJS early 2010. It was the same feeling. Yes, it is powerfull, but too much magic happen for something without a clear orientation (at the time, now it is used for data loaded frontend i think). PS i don't understand what it wants me to do.

Bit off-topic, but I worked with ExtJS around the same time, and I found it one of the most confusing development experiences I ever had. "It's so easy, just add this one property to this deeply nested object you're passing to this function!" Thinking back on it, it's a really good example of "simple vs. easy". It didn't help it gave not a peep if you got one of those data structures wrong (capitalisation typo, wrong location, etc.)

Plus in hindsight the whole idea of "OS semantics in your browser!" was never a good one to start with, although that wasn't as obvious to me at the time.

Re:Shell Script Best Practices, from a decade of scripting t... by orwin on Thursday October 27, 2022 @05:09PM (#33364085)
Yeah, exactly, powershell is easy, but not simple!

ExtJS (on netbeans with windows server 2004) was always my worst development experience, it was my first internship too. That's probably the reason why it took so long (and a bag of money) for me to try JS, an IDE and developing on windows again (I only used Windows for CTFs and AndroidStudio).

Re:Shell Script Best Practices, from a decade of scripting t... by sharken on Thursday October 27, 2022 @07:17AM (#33355935)
I recently replaced a bit of code to look up locked files for a file share with SMB cmdlets to do the same.

The performance difference was night and day.

The biggest issue with PowerShell is that PowerShell Core is not yet default on Windows 10/11 and Windows Server. That should be Microsofts highest priority for PowerShell.

Re:Shell Script Best Practices, from a decade of scripting t... by sumtechguy on Thursday October 27, 2022 @10:14AM (#33357726)
Then not hobble who can run powershell scripts out of the box. Which makes it seem like a dangerous tool. Then no one wants to use it. Some form of powershell has been there since win7. Yet in one of the versions they decided 'oh only admins can use this unless you run this special command'. So it makes me have to revert to using CMD scripts for some simple things. Because I do not want to have to walk whoever it is thru enabling powershell.
Re:Shell Script Best Practices, from a decade of scripting t... by einpoklum on Thursday October 27, 2022 @06:48AM (#33355726)
sh and bash feel a lot more sane and reasonable after learning some PowerShell. PowerShell is mostly not-a-shell.
Re:Shell Script Best Practices, from a decade of scripting t... by partdavid on Thursday October 27, 2022 @04:19PM (#33363463)
Can you expand on this? What do you mean by it's "mostly not-a-shell."? I don't understand it.
Re:Shell Script Best Practices, from a decade of scripting t... by einpoklum on Friday October 28, 2022 @05:25AM (#33369326)
It's mostly a scripting language whose commands communicate with each other, which also let's you issue commands interactively. In an actual shell, programs and commands communicate with the _user_ via textual output that the user can read and understand; and other programs can take the place of the user, reading and parsing the textual output instead. That's not what happens in PowerShell, mostly.
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.
Re:Shell Script Best Practices, from a decade of scripting t... by heresie-dabord on Thursday October 27, 2022 @08:21AM (#33356461)
"Primitive" seems like the wrong word to apply. A modern Python programmer (a common example) might think that anything not OOP is archaic. But OOP is just one design pattern. And... it can easily lead to over-engineering and delight in the pattern per se.

Bash, awk, grep et alii don't do OOP. But they are close to the data and are powerful.

Compaints about the notation of these tools (e.g. "line noise") are becoming silly now that we've seen the appearance of some Powershell and Python statements. ^_^

Re:Shell Script Best Practices, from a decade of scripting t... by HyperSane on Thursday October 27, 2022 @11:13AM (#33358767)
""Primitive" seems like the wrong word to apply"

Painful, ugly, unpleasant?

Re:Shell Script Best Practices, from a decade of scripting t... by heresie-dabord on Friday October 28, 2022 @11:21AM (#33372518)
I guess you would admit that beauty is subjective. Beauty in tooling is an even more rarefied discussion.

Code that we might deem "beautiful" may not even compile. ^_^

Does the tool do what it needs to do without getting fussy, gobbling RAM, and requiring a small army of maintainers to feed the monster? There is most definitely a place in technology for that kind of sanity.

There is something to be said for a design that doesn't bring with it a brigade of Opinions-As-A-Service proponents.

Any non-trivial notation is going to require learning. There is absolute value in learning. I think of many flexible and effective notations in tooling that has literally been the foundation of modern computing.

Maybe we are in the age of the Mono-Notation Luxury Coder. ^_^

Re:Shell Script Best Practices, from a decade of scripting t... by sargstuff on Friday October 28, 2022 @12:14PM (#33373416)
grep is a domain specific language.

Bash and gawk don't have direct in-language support for OOP, but can be used/abused to do things in an oop manner. aka without in-language support (abstractions/api), takes much more effort to do/understand OOP approach.