Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #16557

Re: Return from function depending on number of parameters

From Chris Elvidge <celvidge001@gmail.com>
Newsgroups gnu.bash.bug
Subject Re: Return from function depending on number of parameters
Date 2020-07-10 10:14 +0100
Message-ID <mailman.37.1594372492.2306.bug-bash@gnu.org> (permalink)
References (1 earlier) <506AA493-0D79-4A9A-A53E-279FDA72CED5@larryv.me> <0b8f32e5-0644-526e-59a4-1bad5a474262@passchier.net> <ACF6AB75-5321-42EC-805E-D84048F539C9@larryv.me> <d4a5db68-0594-e540-c931-593469e73ce2@case.edu> <re9bi1$6q3$1@ciao.gmane.io>

Show all headers | View raw


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
>> https://lists.gnu.org/archive/html/bug-bash/2012-04/msg00034.html:
> 
>>>
>>> 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)"
done
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?

Thanks

-- 
Chris Elvidge
England

Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Re: Return from function depending on number of parameters Chris Elvidge <celvidge001@gmail.com> - 2020-07-10 10:14 +0100

csiph-web