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
$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:37a
can equal the values of the variablest
,x
,y
, orz
(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:34a
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