Live data from Hacker News

Pure Sh Bible

github.com

81–90 of 138 posts

Re: Pure Sh Bible

#81
post #64
post #16

Earlier quoted context omitted.

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?

It's a general set of assignments that let you make any colors that you like.

That's only an example. Use them all, if you want.

Re: Pure Sh Bible

#83
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…

Actually, eval, much like global variables (which are maligned by "real programmers" everywhere) actually works fine for a janky shell script. Also, don't use echo, use printf. echo has different semantics and potential footguns depending on the platform.

Isn't the problem with eval that you are running whatever is in the var? Probably variable expansion but maybe not and that could be not ideal

Re: Pure Sh Bible

#84

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…

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.

Re: Pure Sh Bible

#85

"A collection of pure POSIX sh alternatives to external processes." "Ternary Tests" Can anyone point to where in the POSIX standard ternary test are described. (NB. Pure POSIX sh is less featureful than Bash.) What am I missing. https://web.archive.org/web/20201219013931/https://pubs.open... Also these operators from C that the author includes. Is he suggesting these are available in POSIX sh. Quoting from the Pure S…

Those operators are in "2.6.4 Arithmetic Expansion" in your linked doc. See the link to "Arithmetic Precision and Operations"[1].

[1]: https://web.archive.org/web/20201219013931/https://pubs.open...

Re: Pure Sh Bible

#86

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 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.

Re: Pure Sh Bible

#87

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.

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

Re: Pure Sh Bible

#88

Earlier quoted context omitted.

Actually, eval, much like global variables (which are maligned by "real programmers" everywhere) actually works fine for a janky shell script. Also, don't use echo, use printf. echo has different semantics and potential footguns depending on the platform.

Isn't the problem with eval that you are running whatever is in the var? Probably variable expansion but maybe not and that could be not ideal

Programmers do that without realizing it all the time. There's like five classes of exploits that are just programmers assuming whatever variable they are interpolating is safe because they assume their language handles all security problems for them.

Re: Pure Sh Bible

#89
Some nice stuff, but also some stuff, where things get easier if you just switch to a language with less caveats (like Python).

My rule of thumb is to use Bash to write simple scripts, if they fit on the command line and have just one level of loop or if-else (lines can get very long, though ...). However for more complicated stuff I use the alternative programs (find, sed, awk, ...), as they behave more predictable.

Furthermore I am not sure one should use such simplified versions, that are just wrong:

    is_float() {
        # Usage: is_float "number"

        # The test checks to see that the input contains
        # a '.'. This filters out whole numbers.
        [ -z "${1##*.*}" ] &&
            printf %f "$1" >/dev/null 2>&1
    }
I mean having a '.' in the string does make it a non-integer, but what about other floats like 2e3?

Re: Pure Sh Bible

#90
post #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)

Bash comes free. To get zsh I have to convince the sysadmins of the need for it. I'll stick with the (currently) lowest common denominator.
Post reply on HN