1、shell的输入输出参数
#!/bin/sh
while read line
do
echo $line
done < $1
while read line
do
echo $line
done < $1
2、if语句的使用
#!/bin/sh
#echo $*
#echo $1
#echo $2
if [ "$1" = "wsm" ];then
echo "wsm"
elif [ $1 -eq 3 ];then
echo "1 param is 3"
fi
#echo $*
#echo $1
#echo $2
if [ "$1" = "wsm" ];then
echo "wsm"
elif [ $1 -eq 3 ];then
echo "1 param is 3"
fi
3、while语句的使用
#!/bin/sh
while [ 1 -lt 2 ]
do
echo "TEST"
sleep 1
done
while [ 1 -lt 2 ]
do
echo "TEST"
sleep 1
done
4、数组的使用
5、shell中数值计算(加减乘除等)
例子:
expr 24 \* 88 (计算24乘以88)
6、date命令的使用(时间转换)
例子:
date +%s (当前时间和1970年时间的秒数)date -d @date -d @1348790595 (时间的秒数转换为标准可读格式的时间)
7、sed命令使用
例子:
sed -i '/efg/d' a (删除a文件中的efg)sed 's/abc/MQ/g' a (替换a文件中的abd为MQ)sed 's/\<abc\>/MQ/g' ased 's/\<abc\>/MQ/g' ased 's/^\<abc\>/MQ/g' a
8、awk命令的使用
例子:
ps -ef|grep "postfix" (查找含有postfix的值)ps -ef|grep "postfix" |grep -v "grep" (查找含有postfix的值,过滤含有grep的值)ps -ef|awk '{print $8}'|grep "postfix" |grep -v "grep" (同上,打印第8个域值)ps -ef|awk -F ':' '{print $2}'|awk -F '?' '{print $1}' |grep "12" (以冒号、问号分隔,打印第2个和第8个域值)
9、shell脚本读取文件
10、进制转换:
例子:
echo 'obase=16; ibase=8; 12'|bc (把12这个值从16进制转换为8进制)echo $((16#12)) (把16进制12转换为10进制的值)