12.Shell test 命令
Shell test 命令
数值测试
1.num1=100
2.num2=100
3.if test $[num1] -eq $[num2]
4.then
5. echo '两个数相等!'
6.else
7. echo '两个数不相等!'
8.fi
字符串测试
文件测试
Last updated
Shell test 命令
数值测试
1.num1=100
2.num2=100
3.if test $[num1] -eq $[num2]
4.then
5. echo '两个数相等!'
6.else
7. echo '两个数不相等!'
8.fi
字符串测试
文件测试
Last updated
1.两个数相等!1.#!/bin/bash
2.
3.num1=1000
4.num2=1000
5.
6.if test $num1 -eq $num2
7.then
8. echo '两个数相等!'
9. echo $num1
10. echo $num2
11.else
12. echo '两个数不相等!'
13.fi1.[root@localhost lcr]# ./helloworld.sh
2.两个数相等!
3.1000
4.10001.#!/bin/bash
2.
3.a=5
4.b=6
5.
6.result=$[a+b] # 注意等号两边不能有空格
7.echo "result 为: $result"1.result 为: 111.#!/bin/bash
2.
3.a=5
4.b=6
5.
6.result=$[a+b]
7.echo "result = $result"1.[root@localhost lcr]# ./helloworld.sh
2.result = 111.num1="ru1noob"
2.num2="runoob"
3.if test $num1 = $num2
4.then
5. echo '两个字符串相等!'
6.else
7. echo '两个字符串不相等!'
8.fi1.两个字符串不相等!1.#!/bin/bash
2.
3.num1="runtoone"
4.num2="runtotwo"
5.
6.if test $num1 = num2
7.then
8. echo '两个字符串相同‘
9.else
10. echo '两个字符串不相同’1.#!/bin/bash
2.
3.num1="run to one"
4.num2="run to two"
5.
6.if test $num1 = $num2
7.then
8. echo '两个字符串相同!'
9.else
10. echo '两个字符串不相同!'
11.fi1.[root@localhost lcr]# ./helloworld.sh
2../helloworld.sh: 第 6 行:test: 参数太多
3.两个字符串不相同!1.cd /bin
2.if test -e ./bash
3.then
4. echo '文件已存在!'
5.else
6. echo '文件不存在!'
7.fi1.文件已存在!1.#!/bin/bash
2.cd
3.if test -e /home/lcr/mediawiki-1.29.0.tar.gz
4.then
5. echo '文件存在!'
6.else
7. echo '文件不存在!'
8.fi1.[root@localhost lcr]# ./helloworld.sh
2.文件存在!1.cd /bin
2.if test -e ./notFile -o -e ./bash
3.then
4. echo '有一个文件存在!'
5.else
6. echo '两个文件都不存在'
7.fi1.有一个文件存在!