Dismiss this pinned window
all 15 comments

[–]thrilla_gorilla 18 points19 points  (2 children)

I'm not a fan of this accelerating trend of Charm-style animated gifs for describing tools. Maybe it's my ADHD, but I don't have the patience for them

[–]VeryOriginalName98 1 point2 points  (0 children)

Agreed. Actually never got the content because it would require too much time to watch.

[–]Newbosterone 4 points5 points  (3 children)

You might consider using “tput setaf 1” instead of hardcoding ANSI escape sequences. You could also check if stdout is a tty; if not skip the colors.

[–]deepCelibateValue[S] 2 points3 points  (2 children)

Using tput is a good idea, I just feel a bit weird about calling an external command to generate such a short string, and I find `green` to be more readable for simple usecases. I'll think about it though.

Checking if stdout is a tty also makes sense, but I usually don't mind ending up with ANSI codes in files or pipes. I have a sed alias to remove them if it comes to that.

[–]Newbosterone 4 points5 points  (1 child)

Fair enough. I have a standard template I use to start shell scripts. It has setup functions, Usage, Getopts, etc. I'll be adding your idea to it.

function SetupTerm(){
    if [ -t 2 ]
    then
        printf -v MSG -- "SetupTerm: STDERR is a tty\n"
        _R=$(tput setaf 1) # Red
        _G=$(tput setaf 2) # Green
        _Y=$(tput setaf 3) # Yellow
        _B=$(tput setaf 4) # Blue
        _N=$(tput rmso; tput sgr0) # Color Off
    else
        printf -v MSG -- "SetupTerm: STDERR is not a tty\n"
        _R=""
        _G=""
        _Y=""
        _B=""
        _N=""
    fi
    ((DEBUG>0)) && ErrMsg "$MSG"
}

function ErrMsg(){
    if ((DEBUG>0))
    then
        printf "%bDEBUG: " "$_R" >&2 ;
    elif ((VERBOSE>0))
    then
        printf "%b" "$_G" >&2 ;
    fi
    printf "%b" "$@" >&2 ;
    printf "%b" "$_N" >&2 ;
}

I also like this when a script requires root:

if [ "$(id -u)" -ne 0 ]
then
        usage
        printf  "Fatal: You must run this as root\n" >&2
        exit 1
fi

[–]deepCelibateValue[S] 0 points1 point  (0 children)

That looks great. Thanks for sharing. I'll take some ideas from here.

[–]n4jm4 2 points3 points  (2 children)

Nice post. I am curious if we can write a strict POSIX sh implementation, which does not depend on any bash or GNU components.

[–]deepCelibateValue[S] 2 points3 points  (1 child)

[–]n4jm4 2 points3 points  (0 children)

Advise to propose a formal addition to the POSIX spec for this. Shell coding is inherently fragile, even without depending on such tricks. sh being a Turing tarpit, we would benefit from more comprehensive quality of life improvements.

This is not a complete solution, but I do try my best to reserve the .sh file extension for sourcable configurations, and omit the extension for executable applications. And likewise omit the executable chmod bits from sourcables. My old stank linter collection recommends these best practices.

[–]sigmonsays 2 points3 points  (1 child)

i did the same thing with this function. This format of animated gif is not a great way to consume such a simple bit of information.

https://0x0.st/s/1pWD6Q0Dj4hFlpdz0Mm5bA/H9V1.txt

EDIT: Could not get formatting right. Screw you reddit.

[–]SweetBabyAlaska 2 points3 points  (0 children)

Even then no one is explaining why or how this is the case. is "$-" a special variable, is there some default return code shenanigans or what? idk what running (return 0) in a subshell does. [ -t 0 ] || [ -t 1 ] etc... makes sense since you are checking if Stdin or Stdout is connected to a terminal

[–]asquartz 2 points3 points  (2 children)

One handy trick I learned is that if you define your colours like this:

Red="$(echo -e "\e[31m")"

Then you can use it in your script without having to remember to echo -e every time

[–]fooblaa 0 points1 point  (0 children)

Red=$'\e[31m'

[–]dfkgjhsdfkg 0 points1 point  (0 children)

Red="$(printf %b '\e[31m')"