1:变量引用:${var} $var
2:变量赋值:var=10 变量、符号、值之间不能出现空格
3:nowdate=date +%y%m%d
“ 可以将shell命令的输出赋值
5:算术运算:expr 命令 或$[1=2],及$[运算]
6:退出码:$? 表示最后一次退出码,也可以用 exit code 指定
7:if then
if cmd ; then cmds
fi
或者
if cmd
then
cmds
fi
当cmd退出码为0时执行then 部分cmds
if cmd
then
cmds
else
cmds
fi
if cmd
then
cmds
elif cmd
then
cmds
elif cmd
then
cmds
fi
只能与退出码相关为条件码
用test conditon 替代 cmd 从而可以达到与退出码无关为条件码
if test -e file
then
cmds
fi
用[ condition ] 替代 test condition [ 与 ]后前必须有空格
if [ -e file ]
then
cmds
fi
8:if then 的高级特性
if ((expr)) (())用于运算
then
cmds
fi
if [[ ]] [[]] 由于正则表达式字符串
9: case 为单个变量值条件
case varible in
pattern1 | pattern2)
cmds;;
pattern3)
cmds;;
*)
cmds;;
esac
9:$# 传递参数个数
$0 表示程序名(含路径) $1 … $9 ${10} … 引用参数
${$#} 最后一个参数要为 ${!#}
‘\n’ 符号前也要加$
IFS_OLD=$IFS
IFS=$’\n’
10.文本编辑命令sed 和 awk
sed -e script -f file desfile
-e 表示多个命令
sed -e ‘s/aa/bb/;s/xx/yy’ desfile
对输入行处理,先处理第一个命令’s/aa/bb/’,在将处理后的作为新的内容处理第二个命令’s/xx/yy’
sed -e ‘s/aa/bb/’ -e ‘s/aa/bb/’
同上边一样
sed -i ‘s/ //g’ aa.txt #去掉中所用空格aa.txt -i 在 aa.txt修改
-f 表示多个命令的写在一起的文件
sed 会将desfile处理后得整个内容显示出来,但不会修改源文件desfile
sed 更多替换标记
s/pattern/replacement/flags
s///2 替换匹配的第二个
s///g 替换一行所有匹配的
s///p 表明被修改的内容也要打印出来
s///w 将替换的结果写到文件
sed -n 不显示内容
sed -n -e ‘s///p’ 仅显示被修改的内容