[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Return from function depending on number of parameters

From: Chris Elvidge
Subject: Re: Return from function depending on number of parameters
Date: Fri, 10 Jul 2020 10:14:38 +0100
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1

On 05/07/2020 06:24 pm, Chet Ramey wrote:
On 7/4/20 12:23 PM, Lawrence Velázquez wrote:

Quoting Chet liberally from

It dates from Posix circa 1990 (1003.2d9, of which I've lost my paper
copy).  I implemented it after the Berkeley guys, mostly Marc
Teitelbaum, put it into Posix.  It ended up getting dropped in favor
of the ksh $((...)) expansion, at which point everyone deprecated the
old $[...].  I removed it from the manual sometime later, but it still
works as it always has.

This all remains true. I probably won't remove it, but nobody should use it
in new code.

The reason I tried $[...] stems from some code I found re. autosetting the colour of the return code from the previous command, and autosetting the colour of the prompt depending on user id (PS1). Viz:

# see: http://linuxcommand.org/lc3_adv_tput.php
# blacK Red Green Blue Yellow Magenta Cyan White bolD Normal
unset fcol bcol col
declare -A bcol
declare -A fcol
declare -a col
col=( k r g y b m c w d n )
for i in {0..7}; do
        bcol[${col[i]}]="$(tput setab $i)"
        fcol[${col[i]}]="$(tput setaf $i)"
fcol[d]="$(tput bold)"
fcol[n]="$(tput sgr0)"
# user colour: red for root; green for others (>0)
ucol=( "$(tput setaf 1)" "$(tput setaf 2)" )
# exit colour: green for 0; red for errors (>0)
ecol=( "$(tput setaf 2)" "$(tput setaf 1)" )

# I don't know where I found this.
# Probably somewhere in Greg Wooledge's wiki
# I can't find it again, now!!

# note use of [!!$?]
# !!$? in an arithmetic context does a logical negation
# of the exit status twice, which maps 0 to 0, and non-zero to 1.
# also user id [!!$(id -u)]

export PS1='\[${fcol[d]}${ecol[!!$?]}\][$?] \[${ucol[!!$(id -u)]}\]\h\[${fcol[b]}\]!\[${ucol[!!$(id -u)]}\]\u\[${fcol[w]}\]:(`tty|cut -d/ -f3-`):\D{%a %d %b %Y %I:%M %P %Z}:`pwd`\n\$\[${fcol[n]}\] '

I somehow thought that [...] forced 'arithmetic context', so I tried it.

Are the two uses (array subscript and arithmetic context) of [...] connected/related? Or am I (stupidly) seeing a connection where none really exists?


Chris Elvidge

reply via email to

[Prev in Thread] Current Thread [Next in Thread]