Live data from Hacker News

Pure Sh Bible

github.com

101–110 of 138 posts

Re: Pure Sh Bible

#101

Earlier quoted context omitted.

Shell syntax isn't "wacky", it's extremely error prone. Using shell script instead of a sane language is like opening beer with your teeth because you can't be bothered to get a bottle opener.

Oh, right, because it's not easy to cause errors in Python.

Correct. Python code is unlikely to contain the kind of trivial variable manipulation errors that plague Bash scripts.

Re: Pure Sh Bible

#103

If you are at this level of required complexity as in those examples, you should use a proper programming language, not shell. Half those snippets fail with spaces in the wrong place, newlines, control characters, etc. I think all such shell "magic" should come with a huge disclaimer pointing the user at better alternatives such as perl or python. And all snippets should have the necessary caveats on horribly broken…

I enjoy using bash, and throw together little scripts now and then. It is convenient to be able to wrap up some bash commands and turn them into a script, when I realize I’ve been using them repeatedly. But, every time I see examples of how to write sh scripts properly, it makes me wonder if this is just the wrong way to look at the world. Maybe it would be easier to extend Python down to make it better for command l…

What would make Python better for command line use? Better alternatives to argparse in the standard library?

Re: Pure Sh Bible

#104

Earlier quoted context omitted.

Shell is full of ridiculous footguns though. It's like saying if the store is only 60m away (across an Indiana Jones-tier trap gauntlet) then just walk there. Remember that time bumblebee accidentally deleted everyone's /usr? Or that time steam deleted everyone's homedir? Both because of the easiest to avoid bash footguns - spaces and empty variables. My hard and fast rule that I've never regretted is - if you use a…

Have you not used Python or Perl much? Because both are full of footguns. And I don't see any advantage to for loops in a HLL. These are identical: for i in `seq 1 10` ; do echo i $i done for i in range(1,10): print("i %i\n") You might find problems with the shell code, and I'll find problems with the Python. But both will print 1 to 10.

Come on, they aren't full of footguns in basic things like string comparison or variable assignment.

Most of the things on this list couldn't happen in Python:

https://mywiki.wooledge.org/BashPitfalls

Re: Pure Sh Bible

#105
post #59

Earlier quoted context omitted.

Same. Shell and glue-scripts in Python. The activation energy required is now close to zero.

I hope I never have to be the one to read those scripts.

Why? They're generally much clearer and well-commented than what a rushed harried sysadmin would write.

Re: Pure Sh Bible

#106

Earlier quoted context omitted.

Using a more advanced language just because you find shell syntax to be wacky is like using a car to get groceries because you find panniers or a backpack to be wacky. It's the use case that matters; if you're 60 meters from the store, just use your bike, or walk. There are plenty of cases in which Perl or Python will make things much more complicated than 5 lines of spooky-looking shell script. Sometimes a little my…

Shell syntax isn't "wacky", it's extremely error prone. Using shell script instead of a sane language is like opening beer with your teeth because you can't be bothered to get a bottle opener.

I have written my large share of bash scripts at this point in my life. However, I recently started a new project. I opened up a file and started noodling out a sh script. I stopped exactly because of what you are saying. I then installed powershell. I have not decided if I am going to use powershell, python or ansible yet for this. But as it is gluing a bunch of commands together with some string manipulation and some very simple math calculations powershell seems better for the job in this case.

Bash is also good at these things but it feels like you are using weird archaic tools to get things done. They work very well but the syntax on some of the commands you end up having to use are entire oriley books by themselves. It has its own odd way of doing things. Bash is nice for when you know you can not really manipulate the environment. As it is fully complete and usually 'table stakes' for what is installed. It is just kind of odd the way it works. In this case I decided to start with something that is more akin to what I am used to writing.

Re: Pure Sh Bible

#107

POSIX shell scripting is just broken. Don't get me wrong. I love doing it, but from a language design point it is abysmal. Just this week I tried to write a script to apply a custom function to a list of files. But the amount of time you spend just to make sure it works with special cases like spaces or new line characters in file names is not healthy. After all, it is probably one of the most standard scenarios. Fir…

You can xargs a bash function That function must be exported before, for instance:

my_function(){

...

}

export -f my_function

find | xargs my_function

Re: Pure Sh Bible

#108

Earlier quoted context omitted.

I enjoy using bash, and throw together little scripts now and then. It is convenient to be able to wrap up some bash commands and turn them into a script, when I realize I’ve been using them repeatedly. But, every time I see examples of how to write sh scripts properly, it makes me wonder if this is just the wrong way to look at the world. Maybe it would be easier to extend Python down to make it better for command l…

What would make Python better for command line use? Better alternatives to argparse in the standard library?

Sorry, I was sloppy. I meant using it as the system shell. So, processing arguments, I guess, would be less of a big deal.

Convenience features, like ls and other classic shell commands being run without parentheses would have to be handled… I’m not breaking any new ground here, actually this has gotten me to look into xonsh and it looks pretty decent.

Re: Pure Sh Bible

#109
I feel the same way about "advanced" shell scripting techniques as I do about "cutting-edge" accounting techniques: someone's probably going to jail.

I have written some big complex systems in shell and regretted it. My rule of thumb is that if a script needs error handling, it's time to write it in a real programming language. Tricky shell programs running as root are a real danger.

It's nice to know that some of these techniques exist, but better not to use them. Many of them have to do with string manipulation, which is a sign that a proper language with data structures would be a win.

Many people don't realize that perl is part of the Debian base system. If you are going to go crazy with one-liners, then that's a better tool. I would generally recommend Python, though.

Avoiding bash-isms is useful so that you can run under busybox shell, reducing the size and dependencies of containers.

Embedded systems often use shell. If you are tempted to install bash for more power, a better solution is probably lua. It's smaller and saner.

Re: Pure Sh Bible

#110

Earlier quoted context omitted.

Have you not used Python or Perl much? Because both are full of footguns. And I don't see any advantage to for loops in a HLL. These are identical: for i in `seq 1 10` ; do echo i $i done for i in range(1,10): print("i %i\n") You might find problems with the shell code, and I'll find problems with the Python. But both will print 1 to 10.

Personally I've found Python to have significantly fewer foot-guns than bash. The biggest reason why I don't use it all of the time is that calling / piping commands takes a lot more typing, so it's easier to use bash for very simple shell scripts. And while there are libraries that simplify shell scripting, that adds external dependencies to your scripts. > for i in range(1,10): > print("i %i\n") This outputs "i %i\…

> This outputs "i %i\n\n" 9 times.

damn, ya got me there. to be fair, I was drunk when I wrote that xD

Post reply on HN