基本:
以#!/bin/sh开头通知shell使用系统的Bourne shell解释器。
程序名为*.sh
条件测试:
test condition 或 [ conditon ] 注意,[]中有两个空格
-d 目录 -s非空 -r可读 -w可写 -x可执行
-f正规文件 -l 符合连接 -u 文件有suid位设置
eg:test -w file.txt 等价于[ -w file.txt ]也等价于ls -l file.txt 返回0表示真即可写,返回1表示假。
eg:[ -d bin ] 查看bin目录是否存在。
逻辑操作符:
-a 与 -o或 !非
字符串测试: test "string"
test string_operator "string" [ string_operator string ]
test "strig" string_operator "string" [ string string_operator string ]
string_operator:= != -z空串 -n非空串
eg: if [ "$name" = "" ] #判断输入的内容是否为空。
数值测试: "number" numeric_operator "number" [ "number" numeric_operator "number" ]
numeric_operator :-eq -ne不等 -gt大于 -lt小于 -le小于等于 -ge大于等于
eg:if [ "15" -lt "12" ] #判断15<12?
控制流程:
fi 条件 case 值 in for 变量名 [in 列表]
then 命令 模式) do
elif 条件 命令 命令
then 命令 ;; done
elif 条件 模式)
then 命令 命令
else 命令 ;;
fi esac
until 条件 while 命令
命令 do
done 命令
done
shell函数:
[function] 函数名()
{
命令
}
I/O重定向:
标准文件: stdin stdout stderr
缺省文件指针: 0 1 2
输入重定向:commend<filename 告诉程序,不要从键盘接受输入,而要从指定的文件中接受输入。
输出重定向:commend>filename 将输出重定向,由输出屏幕输出到文件,而去会覆盖。
>>这个则是追加到 文件末尾,不会覆盖。
eg: cat f1 f2>>xyz cat f1 2>/dev/null
管道:cmd1 | cmd2 将一个命令的输入传递给另一个命令作为它的输入。
eg:who | wc -l
过滤器(模式搜索命令):grep 在输入流中搜索模式,显示含有该模式或不含有该模式的行和行号,或者文件名。
eg: ls | grep ^n | wc -l
shell函数:find -name -type
eg: find . -name "kong*" find -name "file"
find / -type f -pring | xargs grep "device"