Live data from Hacker News

Pure Sh Bible

github.com

11–20 of 138 posts

Re: Pure Sh Bible

#11

    # Remove all leading white-space.
    trim=${1#${1%%[![:space:]]*}}
    # Remove all trailing white-space.
    trim=${trim%${trim##*[![:space:]]}}
Your scientists were so preoccupied with whether they could, they didn't stop to think if they should

Re: Pure Sh Bible

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

It's well worth learning to use printf instead of echo to avoid all the various ways that echo can break or is not well defined.

https://mywiki.wooledge.org/BashPitfalls#echo_.24foo

Re: Pure Sh Bible

#14
Problems with this advice:

::looping over a file

The "loop over the contents of a file" entry does not protect stdin.

Use an alternate descriptor.

  while read -r line 
Particularly problematic is ssh, that will pass $line to any remote command that is able to consume it.

The looped commands will receive stdin of the loop when an alternate descriptor is used. I use 9 by habit after reading the flock manual page many years ago.

::EXIT trap

dash will only call the EXIT trap on a normal exit. Add INT (and any other signal that you want from "kill -l") if you also want to catch abnormal terminations.

Windows busybox sh/bash only catches regular exits, no matter what else you add.

Re: Pure Sh Bible

#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 Android terminal, and it works as advertised.

Re: Pure Sh Bible

#18

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.

Even as someone who grew up in a very religious household, I am still struggling to come up with a “steel-man” argument on your behalf.

Can you explain why you believe it is in poor taste?

Re: Pure Sh Bible

#19
post #8

If we're going to be purists, sh is a far cry from bash. /s tongue in cheek, please don't kill me. I love bash and I use it often. I love Greg Wooledge's bash guides and all the people in #bash@libera.

I like busybox bash (especially on Windows) which is very much not bash.

Busybox bash does not implement arrays, for starters, which is a deal breaker for many scripts.

The Windows kernel forks processes about 10x slower than Linux, so these tricks have real performance value for POSIX-family shells running there.

Re: Pure Sh Bible

#20

Before clicking, I really thought this was some kind of shell command front-end to spit out Bible verses or something. Woops.

For something like that, you’d want to use fortune(1) with a Bible fortune file, e.g.

- KJV: https://sourceforge.net/projects/fortunebible/

- NIV: https://github.com/optio50/Bible-NIV-Fortune

Post reply on HN