1.bash配置文件
/etc/profile:系统的
~/.bash_profile:用户登陆的时候读取
~/.bashrc:每次启动shell时读取
2.shell参数
$#:参数个数
$i:第i个参数值;i只能0-9,用shift语句,可移除最前的那个参数,这时$#减一,$*从第二个开始
$*:所有参数
$?:上一个shell命令(或程序执行)的的返回结果
1.`shell命令a`: 直接执行命令a
2.变量名=`shell命令a`
$变量名: 执行命令a
3.read a 从终端、文件中读入
read b
echo `expr $a + $b` 计算a+b的结果
4.if (test 条件 ) then
执行语句
else
执行语句
fi
5.if [-opt 文件名 ];then
执行语句
else
执行语句
fi
opt 可以是 d:文件存在且是目录
e或者f:文件存在
r:文件存在且可读
w:文件存在且可写
x:文件存在且可执行
6.while[ 条件 ];
do
执行语句
done
7.echo $[$a+1] :输入a++
8. //多重分支的例子
#! /bin/sh
case "$1" in
start)
echo "is start"
;;
stop)
echo "is stop"
;;
*)
echo "is nothing"
esac
9. for 变量名 in 列表; :如 for i in 0 1 2 3;
do
执行
done
10. exit 返回值
11. 函数
function myfun
{
$1
$2
return `expr$1 + $2`
}
myfun 4 9 ;调用函数求和
Var=$? ;获取前一个命令的值-13
12. 只启动一个daemon
#!/bin/sh
WHOAMI=`whoami`
PID=`ps -u $WHOAMI | grep mydaemon | awk '{ print $1 }'`
if( test "$1" = "start" ) then
if( test "$PID" = "" ) then
./mydaemon
fi
fi
if( test "$1" = "stop") then
if(test "$PID" != "" ) then
kill $PID
fi
fi