-ne —比较两个参数是否不相等
-lt —参数1是否小于参数2
-le —参数1是否小于等于参数2
-gt —参数1是否大于参数2
-ge —参数1是否大于等于参数2
-f — 检查某文件是否存在(例如,if [ -f "filename" ])
-d — 检查目录是否存在
几乎所有的判断都可以用这些比较运算符实现。脚本中常用-f命令选项在执行某一文件之前检查它是否存在
if [空格 表达式 空格]
then 执行语句
elif[空格 表达式 空格]
then 执行语句
else 执行语句
fi
比如
a=1
b=2
if [ $a == $b ]
then echo "a==b"
elif [ $a -gt $b ]
then echo "a>b"
else echo "a<b"
fi
if ... else 语句也可以写成一行,以命令的方式来运行,像这样:
if [ $a == $b ];then echo "a==b";else echo "a!=b";
或者
if test $a -eq $b
then echo "a==b"
else echo "a!=b"
fi
test 命令用于检查某个条件是否成立,与方括号([ ])类似
二、
case 值 in 模式1) command1 command2 command3 ;; 模式2) command1 command2 command3 ;; *) command1 command2 command3 ;; esac比如
echo "input your number"
read num
case $num in
1) echo "you select 1"
;;
2) echo "you select 2"
;;
3) echo 'you select 3'
;;
4) echo 'you select 4'
;;
*) echo 'you select num not bt 1-4'
;;
esac
三、
for循环一般格式为:
for 变量 in 列表 do command1 command2 ... commandN done列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。
in 列表是可选的,如果不用它,for 循环使用命令行的位置参数。
比如
for x in this is phf
do
echo $x
done
输出
this
is
phf
比如
for str in "this is phf"
do
echo $x
done
输出 this is phf //当做一个字符串了 有双引号或者单引号
四、while
while command do Statement(s) to be executed if command is true done比如:
num=0
while [ $num -lt 5 ]
do
num=`expr $num + 1`
echo $num
done
比如 从键盘读取信息
echo "enter your most like movie:"
while read movie
do echo "the name is $movie"
done
五、 until
until 循环执行一系列命令直至条件为 true 时停止。until 循环与 while 循环在处理方式上刚好相反。一般while循环优于until循环,但在某些时候,也只是极少数情况下,until 循环更加有用
until 循环格式为:
until command do Statement(s) to be executed until command is true donecommand 一般为条件表达式,如果返回值为 false,则继续执行循环体内的语句,否则跳出循环
num=5
until [ $num -eq 2 ]
do num=`expr $num - 1`
echo "num=$num"
done
六、函数
- # Define your function here
- Hello () {
- echo "Url is http://see.xidian.edu.cn/cpp/shell/"
- }
- # Invoke your function
- Hello
Url is http://see.xidian.edu.cn/cpp/shell/
注意 调用函数时只需要给出函数名即可
来看一个带返回值的
#!/bin/bash funWithReturn(){ echo "The function is to get the sum of two numbers..." echo -n "Input first number: " read aNum echo -n "Input another number: " read anotherNum echo "The two numbers are $aNum and $anotherNum !" return $(($aNum+$anotherNum)) } funWithReturn # 使用$?捕获函数返回值 ret=$? echo "The sum of two numbers is $ret !"像删除变量一样,删除函数也可以使用 unset 命令,不过要加上 .f 选项,如下所示:
$unset .f function_name七、函数参数
在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数..
八、重定向
九、/dev/null http://blog.youkuaiyun.com/pi9nc/article/details/18257593
(1)、/dev/null : 在类Unix系统中,/dev/null,或称空设备,是一个特殊的设备文件,它丢弃一切写入其中的数据(但报告写入操作成功),读取它则会立即得到一个EOF。
在程序员行话,尤其是Unix行话中,/dev/null 被称为位桶(bit bucket)或者黑洞(black hole)。空设备通常被用于丢弃不需要的输出流,或作为用于输入流的空文件。
2、 /dev/null 的日常使用
把/dev/null看作"黑洞"。它等价于一个只写文件,并且所有写入它的内容都会永远丢失,而尝试从它那儿读取内容则什么也读不到。然而, /dev/null对命令行和脚本都非常的有用补充:我们知道cat filename会输出文件内容到标准输出,如果filename不存在会输出错误信息到标准错误输出,如果我们不想看到错误输出则可以
cat filename 2>/dev/null
如果想将stdout 和stderr都重定向 则 cat filename>/dev/null 2>&1
我们使用 echo $? 查看上条命令的退出码:0为命令正常执行,1-255为有出错。
3、/dev/zero
dev/zero : 在类UNIX 操作系统中, /dev/zero 是一个特殊的文件,当你读它的时候,它会提供无限的空字符(NULL, ASCII NUL, 0x00)。
备注:关于shell 的 看这篇文章 都写的很详细
http://c.biancheng.net/cpp/view/6994.html