Live data from Hacker News

Pure Sh Bible

github.com

61–70 of 138 posts

Re: Pure Sh Bible

#61

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 line use. Xonsh or something.

Re: Pure Sh Bible

#64
post #16
post #7

I have to take exception to some of this; it's "technically correct" but like much of shell, likely full of terrifying edge cases that are best avoided. For instance, using eval to have variables with variable names is madness. $ var="world" $ eval "hello_$var=value" $ eval printf '%s\n' "\$hello_$var" I suppose the fancy business with printf is flexing, but I suspect the majority of people scanning documents like th…

Here are my scary evals to use basic ANSI/vt220 color. N="$(printf '\033[')" x=30 for a in Bl R G Y B M C W # 4-bit Black Red Green Yellow Blue Magenta Cyan White do eval $a='$N'"'"$(( x))"m'" \ b$a='$N'"'"$((60+x))"m'" \ ${a}bg='$N'"'"$((10+x))"m'" \ b${a}bg='$N'"'"$((70+x))"m'" # bX=bright Xbg=background bXbg=brgt bgnd x=$((x+1)) done N="${N}0m" printf "$Y${Gbg}I am yellow on green.$N\n" I pasted this into my Andro…

I am curious yellow

on green is all that prints out? What's the rest of it for?

Re: Pure Sh Bible

#67
post #56

The Holy Grail of POSIX-portable shell scripts is irrelevant these days. Just use bash which is the default on most *nix systems.

I'll go a step further and say "just use zsh", which is clearly superior for scripting and installed so easily it hardly matters.

(aside: I don't think bash the "the default on most *nix systems"; it's not on BSD or macOS (which does ship with a very old bash), and while it's certainly common on Linux even there it's not universal)

Re: Pure Sh Bible

#68

What is everyone's current favorite one-liner for replacing a string in all files that match? In other words alternative implementations of this: ruby -pi -e "gsub(/Net::HTTPServerException/, 'Net::HTTPClientException')" {lib,knife}/**/*.rb

And the perl version from which ruby's kind of sprang.

    perl -pi -e 's/from/to/g' list_of_files

Re: Pure Sh Bible

#69

What is everyone's current favorite one-liner for replacing a string in all files that match? In other words alternative implementations of this: ruby -pi -e "gsub(/Net::HTTPServerException/, 'Net::HTTPClientException')" {lib,knife}/**/*.rb

sed -i? Not 100% portable across all implementations, but that usually doesn't matter.

Some languages also have nice tools for this, such as gofmt -r; I don't know if something like that exists for Ruby.

Re: Pure Sh Bible

#70

Mildly annoyed when people constantly refer to their article as the Bible of X. For those of us who are religious, it is in poor taste.

Ha ha - I had the opposite reaction ...

"Oh dear, some god botherer has wrapped their special text in a shell script. Why is this on hacker news? #$%$%$@!!"

Turns out I quite misdirected myself. Don't agree with all his tricks (some are very brittle) but impressive anyway

Post reply on HN