新足迹

 找回密码
 注册

精华好帖回顾

· 重回 Brighton beach..................(17# 加两张) (2011-1-5) 星星星星 · 宝宝趣事-----何必舍近求远(更新) (2007-9-24) 飞儿
· 上班族,偷懒族的最爱,阑珊mm的方子做的柠檬百里香烤鸡以及完美的medium rare牛扒, (2012-11-16) chesecake · 多图详解!---自制福州鱼丸(包馅)!把握两浮是关键! (2014-4-5) 高歌
Advertisement
Advertisement
查看: 1409|回复: 5

[IT] Linux shell 請教 [复制链接]

发表于 2020-5-12 16:36 |显示全部楼层
此文章由 Bomsori 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 Bomsori 所有!转贴必须注明作者、出处和本声明,并保持内容完整
各位先進大家好:

if [ $? -eq 0 ]



if [$? = 0 ]



if [ $# = 0 ]

究竟有何異同? 感謝指導!!
Advertisement
Advertisement

发表于 2020-5-12 18:50 |显示全部楼层
此文章由 courage714 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 courage714 所有!转贴必须注明作者、出处和本声明,并保持内容完整
$#: Stores the number of command-line arguments that
      were passed to the shell program.
$?: Stores the exit value of the last command that was
      executed.
arg1 -eq arg2: like the set of "-eq, -ne, -lt, -le, -gt, or -ge", they are arithmetic binary operators. The
                        arguments may  be positive or negative integers.
str1 = str2:    POSIX compliant string test command, arguments will be interpreted as string
str1 == str2 : the same as "str1 = str2"

发表于 2020-5-12 19:07 |显示全部楼层
此文章由 Bomsori 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 Bomsori 所有!转贴必须注明作者、出处和本声明,并保持内容完整
courage714 发表于 2020-5-12 17:50
$#: Stores the number of command-line arguments that
      were passed to the shell program.
$?: St ...

ok, thanks.

So what is the difference when 0 is as string and argument?

发表于 2020-5-12 19:37 |显示全部楼层
此文章由 courage714 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 courage714 所有!转贴必须注明作者、出处和本声明,并保持内容完整
The part $? and 0 in the statement if [ $? = 0 ]  would be transferred to  "$?" and "0" and be stored as string  underneath some shell script interpreters like bash or csh. But unfortunately it cannot be supported by all shell script interpreters. So normally I would rather use either  if [ $? -eq 0 ] or if [  "$?" = "0" ]

发表于 2020-5-12 23:30 |显示全部楼层
此文章由 wyfme 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 wyfme 所有!转贴必须注明作者、出处和本声明,并保持内容完整
学习

发表于 2020-5-13 11:47 |显示全部楼层
此文章由 Bomsori 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 Bomsori 所有!转贴必须注明作者、出处和本声明,并保持内容完整
courage714 发表于 2020-5-12 18:37
The part $? and 0 in the statement if [ $? = 0 ]  would be transferred to  "$?" and "0" and be store ...

Hi ,

Thanks again for the explanation.
I think it is good enough to let me pass EX300 exam for RHCE.

Regards,
Bomsori
Advertisement
Advertisement

发表回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Advertisement
Advertisement
返回顶部