对于 BASH 脚本,我需要将另一个尚不存在的变量的值分配给脚本中较早的变量。但是,在为后面的变量赋值之前,我不需要调用前面的变量。
基本上,我需要第a
32 行(以及其他实例)中的变量 来获取分配给另一个变量的值,该变量t
直到第 132 行才存在(并且其值将随着循环而改变)。变量a
在t
被赋值之前不会被调用。我想我可以在第 135 行之后找到带有第二组 CASE 语句的替代解决方案,但如果我想要完成的事情是可能的,那将节省我的时间,这将是一种新技术,我也可以为未来的脚本添加我的工具带.
那么,我想做的是可能的吗?如果可以,我该如何完成。提前致谢。
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
虽然我仍然很想知道我最初的问题是否可以按照我描述的方式解决,但这是我的解决方案,以防任何偶然发现这个问题的人也想看到解决方案——即使不是我寻求的方式。
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
来自Bash 参考手册:
所以而不是
你分配
然后像这样检索值:
例子:
另一个例子:
还有更多:
This way you don't need
!
and you can even assign values indirectly. Example: