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

sh/bash/dash/ksh/zsh等Shell脚本
mafn
帖子: 77
注册时间: 2011-04-23 5:28
来自: 汕尾遮浪

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

#1

帖子 mafn » 2011-07-28 23:35

这是一个傻瓜问题。
代码如下:
————————————————————
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'没有出现?是什么原因导致脚本从第七行直接退出?
mafn
帖子: 77
注册时间: 2011-04-23 5:28
来自: 汕尾遮浪

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

#2

帖子 mafn » 2011-07-28 23:37

我想了很久,还是想不通。
真是傻死了。
:em20
头像
ptpt52
帖子: 717
注册时间: 2008-07-27 8:51
系统: Ubuntu/Windows
来自: 广西玉林|广东深圳
联系:

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

#3

帖子 ptpt52 » 2011-07-28 23:42

这是个奇迹,至于你信不信,反正我是信了。 :em04
走过去了也便有了路
http://www.ptpt52.com/
头像
ChenFengyuan
帖子: 770
注册时间: 2008-03-23 0:39

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

#4

帖子 ChenFengyuan » 2011-07-28 23:47

代码: 全选

$ 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
头像
jarlyyn
帖子: 4671
注册时间: 2006-04-12 18:54
联系:

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

#5

帖子 jarlyyn » 2011-07-28 23:48

echo "Next *if* is part of the comparison for the first *if*."
把这个当注释判断。

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

感觉很蛋疼。
mafn
帖子: 77
注册时间: 2011-04-23 5:28
来自: 汕尾遮浪

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

#6

帖子 mafn » 2011-07-29 0:17

我尝试在第六行的后面加入一句echo “$a < $b“,则顺利地出现了第十行。真实郁闷死了。
why why why why why
fnan
帖子: 919
注册时间: 2009-07-01 22:04

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

#7

帖子 fnan » 2011-07-29 8:12

经典装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
bash不如perl精妙,学不到lisp的皮毛,远不够c++强悍,不过可以用。
fnan
帖子: 919
注册时间: 2009-07-01 22:04

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

#8

帖子 fnan » 2011-07-29 8:22

想第十句出现,让then最近的布尔值为真即可:
6 [[ $a < $b ]] 改为 [[ a<b ]]
后面加上一句true也行,echo指令等效true.
bash不如perl精妙,学不到lisp的皮毛,远不够c++强悍,不过可以用。
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

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

#9

帖子 tusooa » 2011-07-29 11:30

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的内容.

代码: 全选

] ls -ld //
fnan
帖子: 919
注册时间: 2009-07-01 22:04

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

#10

帖子 fnan » 2011-07-29 21:16

1-7行都是if的条件.3-7就是说$a是不是小于$b.如果是,就显示then后边那echo的内容.
第十行只与第六行的条件有关,其它都不起作用。
bash不如perl精妙,学不到lisp的皮毛,远不够c++强悍,不过可以用。
fnan
帖子: 919
注册时间: 2009-07-01 22:04

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

#11

帖子 fnan » 2011-07-29 21:35

有关(( a<b ))表达式:
bash版本不同会有不同表现,新版不支持a<b的方式,但支持1<2的方式。
注意abs是bash版本很低时写的,有些细节现在不行。
(这个问题值得搞清楚,谁有疑问请问个清楚)
bash不如perl精妙,学不到lisp的皮毛,远不够c++强悍,不过可以用。
fnan
帖子: 919
注册时间: 2009-07-01 22:04

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

#12

帖子 fnan » 2011-07-29 23:01

想到一个排除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:~$
bash不如perl精妙,学不到lisp的皮毛,远不够c++强悍,不过可以用。
fnan
帖子: 919
注册时间: 2009-07-01 22:04

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

#13

帖子 fnan » 2011-07-29 23:38

明白lz的问题了,没有先给变量赋值,却用变量测试,当然想不通。
bash不如perl精妙,学不到lisp的皮毛,远不够c++强悍,不过可以用。
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

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

#14

帖子 eexpress » 2011-07-29 23:53

if then 都对不上。说啥。
● 鸣学
头像
cnkilior
论坛版主
帖子: 4984
注册时间: 2007-08-05 17:40

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

#15

帖子 cnkilior » 2011-07-29 23:58

bash -x
回复