shell 脚本编程基础:=等号用来赋值与if判断的使用

1,变量的定义

变量的定义禁止等号=两边有空格。变量赋值时候,=号两边不需要空格。

[yxxun@tars ~/shell_l]$ ./thecmd
/home/yxxun/shell_l
[yxxun@tars ~/shell_l]$ cat thecmd
the_cmd="pwd"
$the_cmd
[yxxun@tars ~/shell_l]$

2,$the_cmd变量是一条命令,直接写可运行此命令

3,shell预定义的变量

$$:当前进程的进程号PID

$?:显示上一条命令的退出状态;0表示没有错误,其他任何值表明有错误

$! :后台运行的最后一个进程的进程号pid

4,shell相等比较

   条件判断使用语法 [ condition ],非空会返回 true

   条件判断也可以使用test 

[yxx@localhost yxx_shell]$ if test "as" = "sa";then echo "yes";else echo "no"; fi
no
[yxx@localhost yxx_shell]$ 

4.1使用=号进行比较

[yxx@localhost yxx_shell]$ [ "ok" = "2od" ]&& echo yes || echo no
no
[yxx@localhost yxx_shell]$ [ "ok" = "od" ]&& echo yes || echo no
no
[yxx@localhost yxx_shell]$ if [ 1 = 2 ];then echo "yes";else echo "no"; fi
no
[yxx@localhost yxx_shell]$ 

 注意:等号两边要有空格。注意 condition 前后有空格。

=号用于字符串比较适合,两边必须有空格

4.2 使用eq来比较数值大小

-eq 只能做整数值的等值比较,字符串等值比较不能用使用。

[yxx@localhost yxx_shell]$ if [ 1 = 2 ];then echo "yes";else echo "no"; fi
no
[yxx@localhost yxx_shell]$ if [ "ee" -eq "ed" ];then echo "yes";else echo "no"; fi
bash: [: ee: integer expression expected
no

4.3 使用==来比较

==可以用来比较数值和字符串的比较 

[yxx@localhost yxx_shell]$ if [ 1 == 2 ];then echo "yes";else echo "no"; fi
no
[yxx@localhost yxx_shell]$ if [ 1 == 1 ];then echo "yes";else echo "no"; fi
yes
[yxx@localhost yxx_shell]$ 
[yxx@localhost yxx_shell]$ if [ "as" == "asa" ];then echo "yes";else echo "no"; fi
no
[yxx@localhost yxx_shell]$ if [ "as" == "as" ];then echo "yes";else echo "no"; fi
yes
[yxx@localhost yxx_shell]$ 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值