RHEL 6 Linux中的shell变量总结
整数值比较 格式:[ 整数1 操作符整数2 ] 常用的操作测试符 -eq:等于(Equal) -ne:不等于(Not Equal) -gt:大于(Greater Than) -lt:小于(Lesser Than) -le:小于或等于(Lessser or Equal) -ge:大于或等于(Greater or Equal) 案例: 如果登录用户数小于或等于10则输出YES [root@localhost 桌面]# who|wc -l 2 [root@localhost 桌面]# [ `who|wc -l` -le 10 ] && echo "YES" YES 变量的加减法运算: 变量的加法运算: [root@localhost ~]# a=1000 [root@localhost ~]# echo $a 1000 [root@localhost ~]# expr $a + $a 2000 [root@localhost ~]# echo $a + $a //这是2中错误的操作,运算必须使用expr 本栏目更多精彩内容:http://www.bianceng.cn/OS/Linux/ 1000 + 1000 [root@localhost ~]# expr $a+$a //$a + $a 要有空格否则也是错误的 1000+1000 变量的乘法运算: [root@localhost ~]# expr $a * $a //* 在操作系统中视为特殊字符,屏蔽字符的特殊意义 expr: syntax error [root@localhost ~]# expr $a * $a 1000000 [root@localhost ~]# expr 2 / 1 //除法 2 [root@localhost ~]# expr 2 – 1 //减法 1 [root@localhost ~]# expr 2 % 1 //取余 0 位置变量: 0-9 常用的为1-9 通过脚本来看些位置变量: [root@localhost shellscripts]# cat a.sh #!/bin/bash echo $1 echo $2 echo $3 echo $0 [root@localhost shellscripts]# a.sh a b c d e a b c /shellscripts/a.sh //$0打印的是脚本笨的名字 [root@localhost shellscripts]# a.sh 1 2 3 [root@localhost shellscripts]# a.sh 2 3 5 [root@localhost shellscripts]# cat a.sh #!/bin/bash echo `expr $1 + $2` //$1和$2代表了2个位置变量 [root@localhost shellscripts]# 预定义变量 $# : 命令行中位置参数的个数 $* : 所有位置参数的内容 $? : 上一条命令执行后返回的状态,当状态为0时表示正常,非0表示执行异常或出错。 $$ : 当前所在进程的进程号 $! : 后台运行的最后一个进程号 $0 : 当前执行的进程/进程名 (编辑:常州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |