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]$