【求助】:这段代码是怎么回事?
-
- 帖子: 77
- 注册时间: 2011-04-23 5:28
- 来自: 汕尾遮浪
【求助】:这段代码是怎么回事?
这是一个傻瓜问题。
代码如下:
————————————————————
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'没有出现?是什么原因导致脚本从第七行直接退出?
代码如下:
————————————————————
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'没有出现?是什么原因导致脚本从第七行直接退出?
-
- 帖子: 77
- 注册时间: 2011-04-23 5:28
- 来自: 汕尾遮浪
Re: 【求助】:这段代码是怎么回事?
我想了很久,还是想不通。
真是傻死了。

真是傻死了。

- ptpt52
- 帖子: 717
- 注册时间: 2008-07-27 8:51
- 系统: Ubuntu/Windows
- 来自: 广西玉林|广东深圳
- 联系:
- ChenFengyuan
- 帖子: 770
- 注册时间: 2008-03-23 0:39
Re: 【求助】:这段代码是怎么回事?
代码: 全选
$ 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: 【求助】:这段代码是怎么回事?
echo "Next *if* is part of the comparison for the first *if*."
把这个当注释判断。
第2个if返回了第一if要判断的表达式
感觉很蛋疼。
把这个当注释判断。
第2个if返回了第一if要判断的表达式
感觉很蛋疼。
-
- 帖子: 77
- 注册时间: 2011-04-23 5:28
- 来自: 汕尾遮浪
Re: 【求助】:这段代码是怎么回事?
我尝试在第六行的后面加入一句echo “$a < $b“,则顺利地出现了第十行。真实郁闷死了。
why why why why why
why why why why why
-
- 帖子: 919
- 注册时间: 2009-07-01 22:04
Re: 【求助】:这段代码是怎么回事?
经典装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
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++强悍,不过可以用。
-
- 帖子: 919
- 注册时间: 2009-07-01 22:04
Re: 【求助】:这段代码是怎么回事?
想第十句出现,让then最近的布尔值为真即可:
6 [[ $a < $b ]] 改为 [[ a<b ]]
后面加上一句true也行,echo指令等效true.
6 [[ $a < $b ]] 改为 [[ a<b ]]
后面加上一句true也行,echo指令等效true.
bash不如perl精妙,学不到lisp的皮毛,远不够c++强悍,不过可以用。
-
- 帖子: 6548
- 注册时间: 2008-10-31 22:12
- 系统: 践兔
- 联系:
Re: 【求助】:这段代码是怎么回事?
首先,应该用bash执行脚本.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
1-7行都是if的条件.3-7就是说$a是不是小于$b.如果是,就显示then后边那echo的内容.
代码: 全选
] ls -ld //
-
- 帖子: 919
- 注册时间: 2009-07-01 22:04
Re: 【求助】:这段代码是怎么回事?
第十行只与第六行的条件有关,其它都不起作用。1-7行都是if的条件.3-7就是说$a是不是小于$b.如果是,就显示then后边那echo的内容.
bash不如perl精妙,学不到lisp的皮毛,远不够c++强悍,不过可以用。
-
- 帖子: 919
- 注册时间: 2009-07-01 22:04
Re: 【求助】:这段代码是怎么回事?
有关(( a<b ))表达式:
bash版本不同会有不同表现,新版不支持a<b的方式,但支持1<2的方式。
注意abs是bash版本很低时写的,有些细节现在不行。
(这个问题值得搞清楚,谁有疑问请问个清楚)
bash版本不同会有不同表现,新版不支持a<b的方式,但支持1<2的方式。
注意abs是bash版本很低时写的,有些细节现在不行。
(这个问题值得搞清楚,谁有疑问请问个清楚)
bash不如perl精妙,学不到lisp的皮毛,远不够c++强悍,不过可以用。
-
- 帖子: 919
- 注册时间: 2009-07-01 22:04
Re: 【求助】:这段代码是怎么回事?
想到一个排除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:~$
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++强悍,不过可以用。
-
- 帖子: 919
- 注册时间: 2009-07-01 22:04
- eexpress
- 帖子: 58428
- 注册时间: 2005-08-14 21:55
- 来自: 长沙
- cnkilior
- 论坛版主
- 帖子: 4984
- 注册时间: 2007-08-05 17:40
Re: 【求助】:这段代码是怎么回事?
bash -x