分页: 1 / 2

【求助】:这段代码是怎么回事?

发表于 : 2011-07-28 23:35
mafn
这是一个傻瓜问题。
代码如下:
————————————————————
1 if echo "Next *if* is part of the comparison for the first *if*."
2
3 if [[ $comparison = "integer" ]]
4 then (( a < b )) #(())是数学计算,参见9.7
5 else
6 [[ $a < $b ]]
7 fi
8
9 then
10 echo '$a is less than $b'
11 fi
————————#出处:abs#——————
运行结果是:Next *if* is part of the comparison for the first *if*
我的问题是:为什么第十行的echo '$a is less than $b'没有出现?是什么原因导致脚本从第七行直接退出?

Re: 【求助】:这段代码是怎么回事?

发表于 : 2011-07-28 23:37
mafn
我想了很久,还是想不通。
真是傻死了。
:em20

Re: 【求助】:这段代码是怎么回事?

发表于 : 2011-07-28 23:42
ptpt52
这是个奇迹,至于你信不信,反正我是信了。 :em04

Re: 【求助】:这段代码是怎么回事?

发表于 : 2011-07-28 23:47
ChenFengyuan

代码: 全选

$ sh a.sh
Next *if* is part of the comparison for the first *if*.
a.sh: 11: [[: not found
a.sh: 11: cannot open : No such file
a.sh: 11: [[: not found

Re: 【求助】:这段代码是怎么回事?

发表于 : 2011-07-28 23:48
jarlyyn
echo "Next *if* is part of the comparison for the first *if*."
把这个当注释判断。

第2个if返回了第一if要判断的表达式

感觉很蛋疼。

Re: 【求助】:这段代码是怎么回事?

发表于 : 2011-07-29 0:17
mafn
我尝试在第六行的后面加入一句echo “$a < $b“,则顺利地出现了第十行。真实郁闷死了。
why why why why why

Re: 【求助】:这段代码是怎么回事?

发表于 : 2011-07-29 8:12
fnan
经典装B小技,有点深度,不过不难破解:
1 if echo "Next *if* is part of the comparison for the first *if*." #echo必定执行,即是: if true
2
3 if [[ $comparison = "integer" ]] #comparison未设置,为空,表达式为假,即是:if false。(then匹配最近的布尔值,即第一个if被架空了,语法上两个then需要两个if,但第一个if的布尔值无用了。)
4 then (( a < b )) #(())是数学计算,参见9.7 #无数字即零,数学表达式为假,即:then false .
5 else #if 为 false,即这里被触发。
6 [[ $a < $b ]] #空小于空?即假。
7 fi
8
9 then #最近布尔值为假,下句指令不能执行。(无false为真)
10 echo '$a is less than $b'
11 fi

Re: 【求助】:这段代码是怎么回事?

发表于 : 2011-07-29 8:22
fnan
想第十句出现,让then最近的布尔值为真即可:
6 [[ $a < $b ]] 改为 [[ a<b ]]
后面加上一句true也行,echo指令等效true.

Re: 【求助】:这段代码是怎么回事?

发表于 : 2011-07-29 11:30
tusooa
mafn 写了: 1 if echo "Next *if* is part of the comparison for the first *if*."
2
3 if [[ $comparison = "integer" ]]
4 then (( a < b )) #(())是数学计算,参见9.7
5 else
6 [[ $a < $b ]]
7 fi
8
9 then
10 echo '$a is less than $b'
11 fi
首先,应该用bash执行脚本.
1-7行都是if的条件.3-7就是说$a是不是小于$b.如果是,就显示then后边那echo的内容.

Re: 【求助】:这段代码是怎么回事?

发表于 : 2011-07-29 21:16
fnan
1-7行都是if的条件.3-7就是说$a是不是小于$b.如果是,就显示then后边那echo的内容.
第十行只与第六行的条件有关,其它都不起作用。

Re: 【求助】:这段代码是怎么回事?

发表于 : 2011-07-29 21:35
fnan
有关(( a<b ))表达式:
bash版本不同会有不同表现,新版不支持a<b的方式,但支持1<2的方式。
注意abs是bash版本很低时写的,有些细节现在不行。
(这个问题值得搞清楚,谁有疑问请问个清楚)

Re: 【求助】:这段代码是怎么回事?

发表于 : 2011-07-29 23:01
fnan
想到一个排除bash版本干扰的方法,直接用布尔值测试:
kose5@kose5-Aspire-4552:~$ if true; if false; then true; else false; fi; then echo '$a is less than $b'; fi #else false 起作用。
kose5@kose5-Aspire-4552:~$ if true; if true; then true; else false; fi; then echo '$a is less than $b'; fi #then true 起作用。
$a is less than $b
kose5@kose5-Aspire-4552:~$ if true; if true; then false; else true; fi; then echo '$a is less than $b'; fi #then false 起作用。
kose5@kose5-Aspire-4552:~$ if true; if false; then false; else true; fi; then echo '$a is less than $b'; fi #else true 起作用。
$a is less than $b
kose5@kose5-Aspire-4552:~$ if false; if false; then false; else true; fi; then echo '$a is less than $b'; fi # 第一个if爱怎么写就怎么写,没作用。
$a is less than $b
kose5@kose5-Aspire-4552:~$ if false; if true; then false; else true; fi; then echo '$a is less than $b'; fi #第一个if没作用。
kose5@kose5-Aspire-4552:~$
(证明then确实只匹配最后出现的布尔值)
kose5@kose5-Aspire-4552:~$ if false; if false; then false; fi; then echo '$a is less than $b'; fi #就算没出现true,无false被触发也是true,第一个if的false没影响。
$a is less than $b
kose5@kose5-Aspire-4552:~$

Re: 【求助】:这段代码是怎么回事?

发表于 : 2011-07-29 23:38
fnan
明白lz的问题了,没有先给变量赋值,却用变量测试,当然想不通。

Re: 【求助】:这段代码是怎么回事?

发表于 : 2011-07-29 23:53
eexpress
if then 都对不上。说啥。

Re: 【求助】:这段代码是怎么回事?

发表于 : 2011-07-29 23:58
cnkilior
bash -x