Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: Chris Elvidge Newsgroups: gnu.bash.bug Subject: Re: Return from function depending on number of parameters Date: Fri, 10 Jul 2020 10:14:38 +0100 Lines: 69 Approved: bug-bash@gnu.org Message-ID: References: <506AA493-0D79-4A9A-A53E-279FDA72CED5@larryv.me> <0b8f32e5-0644-526e-59a4-1bad5a474262@passchier.net> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: usenet.stanford.edu 1594372492 32501 209.51.188.17 (10 Jul 2020 09:14:52 GMT) X-Complaints-To: action@cs.stanford.edu To: bug-bash@gnu.org Envelope-to: bug-bash@gnu.org X-Injected-Via-Gmane: http://gmane.org/ User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 In-Reply-To: Content-Language: en-GB Received-SPF: pass client-ip=116.202.254.214; envelope-from=gnu-bug-bash-3@m.gmane-mx.org; helo=ciao.gmane.io X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/10 02:24:21 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: 44 X-Spam_score: 4.4 X-Spam_bar: ++++ X-Spam_report: (4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_ADSP_CUSTOM_MED=0.001, FORGED_GMAIL_RCVD=1, FORGED_MUA_MOZILLA=2.309, FREEMAIL_FORGED_FROMDOMAIN=1, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=1, NML_ADSP_CUSTOM_MED=0.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001, URIBL_SBL_A=0.1 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <506AA493-0D79-4A9A-A53E-279FDA72CED5@larryv.me> <0b8f32e5-0644-526e-59a4-1bad5a474262@passchier.net> Xref: csiph.com gnu.bash.bug:16557 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