Sponsored Content
Top Forums Shell Programming and Scripting Bad substitution - ShellCheck says okay Post 303029937 by RudiC on Sunday 3rd of February 2019 05:57:09 AM
Old 02-03-2019
The problem in your approach in post #1 is bash's "indirection". man bash:
Quote:
Parameter Expansion
.
.
.
If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of variable indirection.
That indirection works (in fact supplies an empty string) if x holds a string that could be a valid variable name but fails on * or . (which can't be part of a valid variable name).
@Scrutinizer: that's why the -f option doesn't take effect - no pathname expansion attempted.



May I question the use of indirection in your script? What do you want to achieve by using it? Your simplified script, purged from indirection, looks like
Code:
IFS=':'
for i in $LS_COLORS
   do   echo -e "\e[${i#*=}m${i%=*}\e[m"
   done
[[0mrs[[m
[[01;34mdi[[m
[[01;36mln[[m
[[00mmh[[m
[[40;33mpi[[m
[[01;35mso[[m
[[01;35mdo[[m
[[40;33;01mbd[[m
[[40;33;01mcd[[m
[[40;31;01mor[[m
[[00mmi[[m
[[37;41msu[[m
[[30;43msg[[m
[[30;41mca[[m
[[30;42mtw[[m
[[34;42mow[[m
[[37;44mst[[m
[[01;32mex[[m
[[01;31m*.tar[[m
[[01;31m*.tgz[[m
 [[01;31m*.arc[[m

and works perfectly well. I showed the console codes as the colours don't translate to html too well.


EDIT: Added the IFS defnition to enable the script's success...

Last edited by RudiC; 02-03-2019 at 06:14 PM..
This User Gave Thanks to RudiC For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bad Substitution

Need Help... I am getting a bad substitution error on my script on a Solaris Server. However the script has been proven to work on HPUX and Solaris servers... #!/usr/bin/sh # # Set the location of the tzupdater.jar file # JAR=/tmp/tzupdater.jar # <<<<< UPDATE THIS LINE... (3 Replies)
Discussion started by: D_Redd74
3 Replies

2. Shell Programming and Scripting

Array reference - bad substitution

I've created a series of arrays named as follows: row1 row2 row3 . . . row10 Each has 4 elements. I'm trying to echo the array elements out in a for loop. Here's what I have: for ((i=1;i<=10;i++)) do for ((j=1;j<=4;j++)) do eval out=${row`echo $i`} echo -n $out (3 Replies)
Discussion started by: swankgd
3 Replies

3. Shell Programming and Scripting

bad substitution error in ksh

hi, i created a shell script having the following content: #! /usr/bin/ksh FROM="myemail@domain.com" MAILTO="someemail@domain" SUBJECT="TEST" BODY="/export/home/adshocker/body.txt" ATTACH="/export/home/adshocker/attach.prog" echo $ATTACH ATTACH_NAME="${ATTACH##*/}" echo $ATTACH_NAME... (5 Replies)
Discussion started by: adshocker
5 Replies

4. Shell Programming and Scripting

bad substitution error in ksh

Hello, In bash I can use the following: TMP=12345 MID=${TMP:1:1} the expected result is: 2 but when using KSH I'm getting a ''bad substitution" error. What is the correct syntaxin ksh? Thanks (2 Replies)
Discussion started by: LiorAmitai
2 Replies

5. UNIX for Dummies Questions & Answers

bad substitution

#!/bin/bash a1=( win 12,01,02,03,04 ) a2=( pre 04,05,06 ) a3=( msn 06,07,08,09 ) Given the above arrays, I want the script to return/echo the following in a loop; win 12,01,02,03,04 pre 04,05,06,07 msn 06,07,08,09 But I can't get it to do as such. I've tried; (2 Replies)
Discussion started by: Muhammad Rahiz
2 Replies

6. Shell Programming and Scripting

bad substitution error!

Hi All, I'm building a new shell script but i'm facing a problem with one line which is giving "bad substitution" error. Please assist script lines: #!/bin/sh printf "%s: " "Occurrence DATE (YYYYMMDD)"; read DATE shortdate=${DATE#??} o/p: ./test1: bad substitution This command is... (2 Replies)
Discussion started by: Dendany83
2 Replies

7. Programming

Make: Bad Substitution

Hi, I have a make file which I try to execute, but it failed when it arrived to the line: for r in ${PIPESTATUS }; do if (($r != 0)); then exit $r; fi;done; With the Error: ""make:/bin/sh: Bad substitution"" Or the Error: "make:${PIPESTATUS[...}: Bad substitution" (Depend on the operating... (3 Replies)
Discussion started by: nadne
3 Replies

8. Shell Programming and Scripting

Why I get bad bad substitution when using eval?

Why I get bad replace when using eval? $ map0=( "0" "0000" "0") $ i=0 $ eval echo \${map$i} 0000 $ a=`eval echo \${map$i}` !!!error happens!!! bash: ${map$i}: bad substitution How to resolve it ? Thanks! (5 Replies)
Discussion started by: 915086731
5 Replies

9. Shell Programming and Scripting

Bad substitution

Cant undestand :) why i have an error on line 2.it is working on my other boxes #!/bin/bash ret=$(echo Q | timeout 5 openssl s_client connect "${1`hostname`}:${2-443}" -ssl3 2> /dev/null) if echo "${ret}" | grep -q 'Protocol.*SSLv3'; then if echo "${ret}" | grep -q 'Cipher.*0000'; then ... (7 Replies)
Discussion started by: kenshinhimura
7 Replies

10. UNIX for Beginners Questions & Answers

Bad substitution issues.. but why?

i am trying to prepare a train and test dataset, for which i need to randomly split the data into corresponding folders (train,test).. I began on a simple script, but seem to get som weird error messages, that i cannot make sense of?.. what am I doing wrong? #!/bin/bash RED='\033] then... (13 Replies)
Discussion started by: kidi
13 Replies
subst(1T)						       Tcl Built-In Commands							 subst(1T)

__________________________________________________________________________________________________________________________________________________

NAME
subst - Perform backslash, command, and variable substitutions SYNOPSIS
subst ?-nobackslashes? ?-nocommands? ?-novariables? string _________________________________________________________________ DESCRIPTION
This command performs variable substitutions, command substitutions, and backslash substitutions on its string argument and returns the fully-substituted result. The substitutions are performed in exactly the same way as for Tcl commands. As a result, the string argument is actually substituted twice, once by the Tcl parser in the usual fashion for Tcl commands, and again by the subst command. If any of the -nobackslashes, -nocommands, or -novariables are specified, then the corresponding substitutions are not performed. For example, if -nocommands is specified, command substitution is not performed: open and close brackets are treated as ordinary characters with no special interpretation. Note that the substitution of one kind can include substitution of other kinds. For example, even when the -novariables option is speci- | fied, command substitution is performed without restriction. This means that any variable substitution necessary to complete the command | substitution will still take place. Likewise, any command substitution necessary to complete a variable substitution will take place, even | when -nocommands is specified. See the EXAMPLES below. | If an error occurs during substitution, then subst will return that error. If a break exception occurs during command or variable substi- | tution, the result of the whole substitution will be the string (as substituted) up to the start of the substitution that raised the excep- | tion. If a continue exception occurs during the evaluation of a command or variable substitution, an empty string will be substituted for | that entire command or variable substitution (as long as it is well-formed Tcl.) If a return exception occurs, or any other return code is | returned during command or variable substitution, then the returned value is substituted for that substitution. See the EXAMPLES below. | In this way, all exceptional return codes are ``caught'' by subst. The subst command itself will either return an error, or will complete | successfully. EXAMPLES
When it performs its substitutions, subst does not give any special treatment to double quotes or curly braces (except within command sub- stitutions) so the script set a 44 subst {xyz {$a}} returns ``xyz {44}'', not ``xyz {$a}'' and the script | set a "p} q {r" | subst {xyz {$a}} | return ``xyz {p} q {r}'', not ``xyz {p} q {r}''. | When command substitution is performed, it includes any variable substitution necessary to evaluate the script. | set a 44 | subst -novariables {$a [format $a]} | returns ``$a 44'', not ``$a $a''. Similarly, when variable substitution is performed, it includes any command substitution necessary to | retrieve the value of the variable. | proc b {} {return c} | array set a {c c [b] tricky} | subst -nocommands {[b] $a([b])} | returns ``[b] c'', not ``[b] tricky''. | The continue and break exceptions allow command substitutions to prevent substitution of the rest of the command substitution and the rest | of string respectively, giving script authors more options when processing text using subst. For example, the script | subst {abc,[break],def} | returns ``abc,'', not ``abc,,def'' and the script | subst {abc,[continue;expr 1+2],def} | returns ``abc,,def'', not ``abc,3,def''. | Other exceptional return codes substitute the returned value | subst {abc,[return foo;expr 1+2],def} | returns ``abc,foo,def'', not ``abc,3,def'' and | subst {abc,[return -code 10 foo;expr 1+2],def} | also returns ``abc,foo,def'', not ``abc,3,def''. SEE ALSO
Tcl(1T), eval(1T), break(1T), continue(1T) KEYWORDS
backslash substitution, command substitution, variable substitution ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +--------------------+-----------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +--------------------+-----------------+ |Availability | SUNWTcl | +--------------------+-----------------+ |Interface Stability | Uncommitted | +--------------------+-----------------+ NOTES
Source for Tcl is available on http://opensolaris.org. Tcl 7.4 subst(1T)
All times are GMT -4. The time now is 01:55 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy