0

For a BASH script, I need to assign the value of another variable that does not yet exist to a variable earlier in a script. I do not however need to call upon the earlier variable until after the later variable has been assigned a value.

Basically, I need the variable, a, in line 32 (among other instances) to take on the value assigned to another variable, t, that doesn't exist until line 132 (and whose value will change with the loop). The variable a is not called upon until after t is assigned a value. I think I could find an alternate solution with a second set of CASE statements after line 135 but if what I want to accomplish is possible, it would save me time and it would be a new technique I could add too my tool belt for future scripts.

So, is what I want to do possible and if so, how can I accomplish it. Thanks in advance.

1   IFS=$'\n'                               
2                                   
3   for i in {Gamma,Saturation,Brightness,Contrast}; do                             
4       for j in {Gamma,Saturation,Brightness,Contrast}; do                         
5           for k in {Gamma,Saturation,Brightness,Contrast}; do                     
6               for m in {Gamma,Saturation,Brightness,Contrast}; do                 
7                                   
8                   if [[ $i == $j || $i == $k || $i == $m || $j == $k || $j == $m || $k == $m ]]; then             
9                       continue            
10                  fi              
11                                  
12                  count=1             
13                                  
14                  s1=""               
15                  e1=""               
16                  i1=""               
17                  s2=""               
18                  e2=""               
19                  i2=""               
20                  s3=""               
21                  e3=""               
22                  i3=""               
23                  s4=""               
24                  e4=""               
25                  i4=""               
26                                  
27                  case $i in              
28                      Gamma)          
29                          s1=0.80     
30                          e1=1.20     
31                          i1=0.20     
32                          a=$t        
33                          ;;      
34                      Saturation)         
35                          s1=-20      
36                          e1=20       
37                          i1=20       
38                          b=$t        
39                          ;;      
40                      Brightness)         
41                          s1=-15      
42                          e1=15       
43                          i1=5        
44                          c=$t        
45                          ;;      
46                      Contrast)           
47                          s1=-30      
48                          e1=60       
49                          i1=10       
50                          d=$t        
51                          ;;      
52                  esac                
53                  case $j in              
54                      Gamma)          
55                          s2=0.80     
56                          e2=1.20     
57                          i2=0.20     
58                          a=$x        
59                          ;;      
60                      Saturation)         
61                          s2=-20      
62                          e2=20       
63                          i2=20       
64                          b=$x        
65                          ;;      
66                      Brightness)         
67                          s2=-15      
68                          e2=15       
69                          i2=5        
70                          c=$x        
71                          ;;      
72                      Contrast)           
73                          s2=-30      
74                          e2=60       
75                          i2=10       
76                          d=$x        
77                          ;;      
78                  esac                
79                  case $k in              
80                      Gamma)          
81                          s3=0.80     
82                          e3=1.20     
83                          i3=0.20     
84                          a=$y        
85                          ;;      
86                      Saturation)         
87                          s3=-20      
88                          e3=20       
89                          i3=20       
90                          b=$y        
91                          ;;      
92                      Brightness)         
93                          s3=-15      
94                          e3=15       
95                          i3=5        
96                          c=$y        
97                          ;;      
98                      Contrast)           
99                          s3=-30      
100                         e3=60       
101                         i3=10       
102                         d=$y        
103                         ;;      
104                 esac                
105                 case $m in              
106                     Gamma)          
107                         s4=0.80     
108                         e4=1.20     
109                         i4=0.20     
110                         a=$z        
111                         ;;      
112                     Saturation)         
113                         s4=-20      
114                         e4=20       
115                         i4=20       
116                         b=$z        
117                         ;;      
118                     Brightness)         
119                         s4=-15      
120                         e4=15       
121                         i4=5        
122                         c=$z        
123                         ;;      
124                     Contrast)           
125                         s4=-30      
126                         e4=60       
127                         i4=10       
128                         d=$z        
129                         ;;      
130                 esac                
131                                 
132                 for t in `seq $s1 $i1 $e1`; do              
133                     for x in `seq $s2 $i2 $e2`; do          
134                         for y in `seq $s3 $i3 $e3`; do      
135                             for z in `seq $s4 $i4 $e4`; do  
136                                 
137                                 if [[ $s1 == "0.80" ]]; then k=`printf "%0.2f\n" $t`; t=$k; fi
138                                 if [[ $s2 == "0.80" ]]; then k=`printf "%0.2f\n" $t`; x=$k; fi
139                                 if [[ $s3 == "0.80" ]]; then k=`printf "%0.2f\n" $t`; y=$k; fi
140                                 if [[ $s4 == "0.80" ]]; then k=`printf "%0.2f\n" $t`; z=$k; fi
141                                 
142                                 echo $a $b $c $d $t $x $y $z
143                                 echo "Color_""$a""_""$b""_""$c""_""$d"".tif"
144                                 echo "Color_Glossy_""$i""_""$j""_""$k""$m""/"
145                                 echo ${i:0:1}" "$t" "${j:0:1}" "$x" "${k:0:1}" "$y" "${m:0:1}" "$z".tif"
146                                 echo ""
147                                 sleep 0.1
148                             done    
149                         done        
150                     done            
151                 done                
152             done                    
153         done                        
154     done                            
155 done                                

While I would still love to know if my original question can be solved in the way I described, here is my work-around solution in case anyone who stumbles onto this question wants too see a solution - even if not in the way I sought.

IFS=$'\n'

for i in {Gamma,Saturation,Brightness,Contrast}; do
    for j in {Gamma,Saturation,Brightness,Contrast}; do
        for k in {Gamma,Saturation,Brightness,Contrast}; do
            for m in {Gamma,Saturation,Brightness,Contrast}; do

                if [[ $i == $j || $i == $k || $i == $m || $j == $k || $j == $m || $k == $m ]]; then
                    continue
                fi

                mkdir "Color_Glossy_""$i""_""$j""_""$k""_""$m"

                count=0001

                case $i in 
                    Gamma)
                        s1=0.80
                        e1=1.20
                        i1=0.20
                        ;;
                    Saturation)
                        s1=-20
                        e1=20
                        i1=20
                        ;;
                    Brightness)
                        s1=-15
                        e1=15
                        i1=5s
                        ;;
                    Contrast)
                        s1=-30
                        e1=60
                        i1=10
                        ;;
                esac
                case $j in 
                    Gamma)
                        s2=0.80
                        e2=1.20
                        i2=0.20
                        ;;
                    Saturation)
                        s2=-20
                        e2=20
                        i2=20
                        ;;
                    Brightness)
                        s2=-15
                        e2=15
                        i2=5
                        ;;
                    Contrast)
                        s2=-30
                        e2=60
                        i2=10
                        ;;
                esac
                case $k in 
                    Gamma)
                        s3=0.80
                        e3=1.20
                        i3=0.20
                        ;;
                    Saturation)
                        s3=-20
                        e3=20
                        i3=20
                        ;;
                    Brightness)
                        s3=-15
                        e3=15
                        i3=5
                        ;;
                    Contrast)
                        s3=-30
                        e3=60
                        i3=10
                        ;;
                esac
                case $m in 
                    Gamma)
                        s4=0.80
                        e4=1.20
                        i4=0.20
                        ;;
                    Saturation)
                        s4=-20
                        e4=20
                        i4=20
                        ;;
                    Brightness)
                        s4=-15
                        e4=15
                        i4=5
                        ;;
                    Contrast)
                        s4=-30
                        e4=60
                        i4=10
                        ;;
                esac

                for t in `seq $s1 $i1 $e1`; do
                    for x in `seq $s2 $i2 $e2`; do
                        for y in `seq $s3 $i3 $e3`; do
                            for z in `seq $s4 $i4 $e4`; do

                                if [[ $s1 == "0.80" ]]; then q=`printf "%0.2f\n" $t`; t=$q; fi
                                if [[ $s2 == "0.80" ]]; then q=`printf "%0.2f\n" $x`; x=$q; fi
                                if [[ $s3 == "0.80" ]]; then q=`printf "%0.2f\n" $y`; y=$q; fi
                                if [[ $s4 == "0.80" ]]; then q=`printf "%0.2f\n" $z`; z=$q; fi

                                case $i in 
                                    Gamma)
                                        a=$t
                                        ;;
                                    Saturation)
                                        b=$t
                                        ;;
                                    Brightness)
                                        c=$t
                                        ;;
                                    Contrast)
                                        d=$t
                                        ;;
                                esac
                                case $j in 
                                    Gamma)
                                        a=$x
                                        ;;
                                    Saturation)
                                        b=$x
                                        ;;
                                    Brightness)
                                        c=$x
                                        ;;
                                    Contrast)
                                        d=$x
                                        ;;
                                esac
                                case $k in 
                                    Gamma)
                                        a=$y
                                        ;;
                                    Saturation)
                                        b=$y
                                        ;;
                                    Brightness)
                                        c=$y
                                        ;;
                                    Contrast)
                                        d=$y
                                        ;;
                                esac
                                case $m in 
                                    Gamma)
                                        a=$z
                                        ;;
                                    Saturation)
                                        b=$z
                                        ;;
                                    Brightness)
                                        c=$z
                                        ;;
                                    Contrast)
                                        d=$z
                                        ;;
                                esac

                                old="Color_""$a""_""$b""_""$c""_""$d"".tif"
                                new="Color_Glossy_""$i""_""$j""_""$k""_""$m""/"$count" "${i:0:1}" "$t" "${j:0:1}" "$x" "${k:0:1}" "$y" "${m:0:1}" "$z".tif"

                                cp $old $new

                                count=$((10#$count+1))
                                countTmp=`printf "%04d\n" $count`
                                count=$countTmp
                            done
                        done
                    done
                done
            done
        done
    done
done
5
  • If $a is $t why use $a at all? Just use $t on lines 142 and 143... If you change $t, just copy it to $a right before modifying it... – Patrick Mevzek Aug 6 '19 at 23:37
  • If you look at all the CASE instances, a can equal the values of the variables t, x, y, or z (which each change based on their respective loops -- lines 132-135). In short, I have source files titled. "Color_A_B_C_D.tif" where A..D represent four unique variables of differing ranges. Each of these files will be copied into 24 unique subfolders than change the order of the variables A..D (e.g. B_C_D_A). The loops in lines 3-6 establish what a new subfolder will be named (as expressed by test output of line 144). Each of the original files (~600) will be copied and renamed ... – Brian Aug 7 '19 at 0:34
  • ...with the new file name being compromised of each of the variables A..D in the respective order of the folder (and represented by line 145). However, these new files must be copied in ascending order of each respective variables value (so they can be compared against one another in a systematic fashion). There will be a command added that prefixes the order of each file in the folder with an ascending counter (e.g. 001, 002...). This is why the order they are assembled is important. ... – Brian Aug 7 '19 at 0:40
  • ...So, while the second set of loops (lines 132-135 will correctly assemble the new file names, the variable a in lines 32, 58, 84, or 110 are used to extract information needed to identify what the original file name is that is being copied into the new folder. As I write this, I am not sure this explanation is helping. It would be ideal to reference values in the second set of loops (132-135) and limit my need to use CASE statements to only one instance. But, I am guessing I will need to use a second set of CASE statements in the second set of loops to accomplish my task. – Brian Aug 7 '19 at 0:43
  • It would be way better if you provided MCVE. I'm not completely sure if my answer helps. If it solves your problem then (as far as I'm concerned) you don't have to edit the question. But next time please put some effort in creating MCVE. – Kamil Maciorowski Aug 7 '19 at 6:54
0

From Bash Reference Manual:

The basic form of parameter expansion is ${parameter}. […]

If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion. The value is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion. […] The exclamation point must immediately follow the left brace in order to introduce indirection.

So instead of

a=$t

you assign

a=t

and then retrieve the value like this:

${!a}

Example:

$ a=t
$ echo "$a"
t
$ echo "$t"

$ echo "${!a}"

$ t=5
$ echo "${!a}"
5
$ t=foo
$ echo "${!a}"
foo
$

Another example:

$ x=1
$ y=2
$ z=3
$ a=x
$ echo "${!a}"
1
$ a=y
$ echo "${!a}"
2
$ a=z
$ echo "${!a}"
3
$ 

And more:

A variable can be assigned the nameref attribute using the -n option to the declare or local builtin commands […] to create a nameref, or a reference to another variable. This allows variables to be manipulated indirectly. Whenever the nameref variable is referenced, assigned to, unset, or has its attributes modified (other than using or changing the nameref attribute itself), the operation is actually performed on the variable specified by the nameref variable’s value.

This way you don't need ! and you can even assign values indirectly. Example:

$ a=t
$ echo "$a"
t
$ declare -n a
$ echo "$a"

$ t=5
$ echo "$a"
5
$ t=foo
$ echo "$a"
foo
$ a=bar
$ echo "$t"
bar
$ declare +n a
$ echo "$a"
t
$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.