(1)变量:首字符为字母,其他位置可下划线数字,不能空格,标点符号
var=name 注意没空格
env 列出环境变量
export 设置环境变量
参数变量:
$# 参数个数,不包括文件本身
$0 脚本文件名
$1,$2,...参数。
$? 上次执行结果
变量的运算
数学运算: + - * / % 等等
expr $a + $b 注意空格
逻辑运算 lt gt le gt eq ne
test 2 -lt 3 ,对应表达式中可用 [ 2 lt 3 ]
多个逻辑运算 -o or, -a and
test 2 -lt 1 -o 2 -gt 1
字符串比较
test "s"="s"
test "s"!="S"
test -n "ss"
test -z ""
文件测试 -r -w -x -f -d -s(文档大小不为0)
test -d /home
(2)管道和重定向
管道: ls | wc -l
重定向:
> 将内容重定向,覆盖原文件,>>追加到末尾
<输入重定向,<<here文档操作
wc -l <<EOF
hello
test
EOF
result: 2
(3)if 运用
if test 2 -gt 1 ##可用 if [ 2 -gt 1 ]
then
echo "true"
else
echo "false"
fi
(4) case
case i in
1)
echo "one" ;;
2)
echo "two";;
*)
echo "other";;
esac
(5) for
for i in /*
do
if [ -d $i ]
then
echo $i
fi
done
(6) while ,until
while :
do
...
done
until :
do
...
done
(7) function name()
{
...
}
参数传递 $1 $2....
返回值 执行最后表达式或echo
---export
导出变量在子shell中有效
--trap cmd sig 对信号进行处理
$(())算术扩展
sh -x -v -n 调试shell