7 ms·
It's funny how the article claims that treating everything like a string is a drawback, when it's often touted as a strength of bash. The author of this piece
by ghostpepper 6y ago
It's funny how the article claims that treating everything like a string is a drawback, when it's often touted as a strength of bash.
The author of this piece claims: "Powershell [...] offers a series of commands useful for developing tools and automatisms that are very difficult to implement with simple strings." but as far as I can tell, they don't go on to actually explain any of these cases where Powershell is a more appropriate tool than traditional string-based shells.
- 13of40 6y agoLet me channel Snover really quick: Imagine you need to get the MAC address of a network adapter in Bash. One way to do it would be... ifconfig eth0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' ...another... ifconfig eth0 | awk '/^[a-z]/ { iface=$1; mac=$NF; next } /inet addr:/ { print iface, mac }' Both of these are tied to the way the MAC address is printed in the output of the ifconfig command, and there's no contract that says that can't change. In fact, there are probably versions of it out there where these won't work. In PowerShell you would do this... Get-NetAdapter -Name Wi-Fi | select -ExpandProperty MacAddress This is far more readable if you pass this script on to someone else, it won't break if the way it Get-NetAdapter gets rendered to the screen changes, and best of all, since I know the discoverability tricks in PowerShell, even though I've never done this before, I didn't have to go to stack overflow to find it.
- rascul 6y agoYour example is relevant to show how powershell can handle some tasks better, but this task is better performed with ip than ifconfig, as it has an option for json output which jq can parse: ip --json link show eth0 | jq '.[0].address' I'll admit that the discovery of this is probably not very great. I wouldn't be surprised if a number of people learned about the json output, and possibly jq, from this post.
- majkinetor 6y agoCherry picking. Not a system feature. Good luck with that in general.
- lostmsu 6y agoNow parse the IP address
- rascul 6y agoip --json address show eth0 | jq '.[0].addr_info [].local'
- deleted 6y ago[deleted]
- WorldMaker 6y agoPowerShell is simply inverting the defaults: Linux/Bash scripts output formatted text by default, but sometimes have options to return objects (often as JSON text strings). PowerShell cmdlets by default return objects (in a custom binary/in-memory format, but convertible to/from JSON, among other options like CSV and XML), but might optionally return formatted strings. (PowerShell also offers tools out of the box similar to but different from jq for exploring/converting/navigating objects.) In general when working in PowerShell you don't have to look up if there is a command with a --json flag (or equivalent) that does what you need to do, you can assume that objects are the default response and move on to working with them with PowerShell's jq equivalents. (Obviously, that makes discovery generally easy.)
- taviso 6y agoRight, but the fact that you can parse out something that didn't have a contract is a strength. Someone had to write the Get-NetAdapter cmdlet to give you a contract, and Linux has those too, so a fairer comparison would be something like: addr=$(</sys/class/net/ens33/address) I think that's just as readable as your example. The question is how does powershell handle extracting something without a contract, and is that more readable than bash? I'm skeptical :)
- rascul 6y agoEven better than the ip | jq in my sibling comment.
- majkinetor 6y agoIts not like pwsh can't do strings ffs. It just can do objects on top of that. You can totally go bash-like-crazy in it and parse strings if you like that.
- taviso 6y agoSure, I'm saying that would be a fairer comparison - getting output from something that has no defined interface. So why not compare apples to apples, how would powershell extract the MAC from ifconfig output? If that's more flexible or powerful than bash with standard UNIX tools, then that might be impressive!
- majkinetor 6y ago> how would powershell extract the MAC from ifconfig output? It could go identically crazy as in bash, as it has its own grep (sls), can use grep itself etc. > (ipconfig /all | sls Physical) -replace ".+: " 00-FF-84-14-66-D7 00-FF-B7-06-19-0F 00-15-5D-00-1F-3C ...
- taviso 6y agoRight, that's a fairer comparison. So the question is, why is that better?
- bigger_cheese 6y agoI'm not on a Linux box at the moment but isn't there an eth0 entry in /sys (or maybe /proc) which can give you mac address in easier to parse way (I think you could just use cat). I don't know if they are still in vogue but /sys and /proc interfaces were originally intended to return clean output which is easy to parse in programs and scripts. I guess usage never really caught on.