!!:s/foo/bar/
!!:gs/foo/bar
##################################
shell 例子
判断参数是否为空-空退出并打印null
#!/bin/sh
echo $1
name=${1:?"null"}
echo $name
##################################
循环数组
for ((i=0;i<${#o[*]};i++))
do
echo ${o[$i]}
done
##################################
判断路径
if [ -d /root/Desktop/text/123 ];then
echo "找到了123"
if [ -d /root/Desktop/text ]
then echo "找到了text"
else echo "没找到text"
fi
else echo "没找到123 文件夹"
fi
##################################
匹配替换密码
#!/bin/sh
cat mailpassword > temp.txt
sed -i "s/:/ = /" temp.txt
w=`awk -F " = " '{print $1}' temp.txt`
for d in $w
do
grep -w $d svnpassword >/dev/null
if [ $? -eq 0 ]
then
sed -i "/^$d/d" svnpassword
grep "^$d" temp.txt >> svnpassword
#替换到转义就出问题
#sed -i "/^$d/c $(grep "^$d" temp.txt)" svnpassword
fi
done
rm temp.txt
##################################
多行合并
将两行并为一行(去掉换行符)
sed '{N;s/n//}' file
将4 行合并为一行(可扩展)
awk '{if (NR%4==0){print $0} else {printf"%s ",$0}}' file
将所有行合并
awk '{printf"%s ",$0}'
##################################
shift 用法
./cs.sh 1 2 3
#!/bin/sh
until [ $# -eq 0 ]
do
echo "第一个参数为: $1 参数个数为: $#"
#shift 命令执行前变量$1 的值在shift 命令执行后不可用
shift
done
##################################
给脚本加参数getopts
#!/bin/sh
while getopts :ab: name
do
case $name in
a) aflag=1
;;
b) bflag=1
bval=$OPTARG
;;
?) echo "USAGE:`basename $0` [-a] [-b value]"
exit 1
;;
esac
done
if [ ! -z $aflag ] ; then
echo "option -a specified"
echo "$aflag"
echo "$OPTIND"
fi
if [ ! -z $bflag ] ; then
echo "option -b specified"
echo "$bflag"
echo "$bval"
echo "$OPTIND"
fi
echo "here $OPTIND"
shift $(($OPTIND -1))
echo "$OPTIND"
echo " `shift $(($OPTIND -1))` "
##################################
判断脚本参数是否正确
./test.sh -p 123 -P 3306 -h 127.0.0.1 -u root
#!/bin/sh
if [ $# -ne 8 ];then
echo "USAGE: $0 -u user -p passwd -P port -h host"
exit 1
fi
while getopts :u:p:P:h: name
do
case $name in
u)
mysql_user=$OPTARG
;;
p)
mysql_passwd=$OPTARG
;; P)
mysql_port=$OPTARG
;;
h)
mysql_host=$OPTARG
;;
*)
echo "USAGE: $0 -u user -p passwd -P port -h host"
exit 1
;;
esac
done
if [ -z $mysql_user ] || [ -z $mysql_passwd ] || [ -z $mysql_port ] || [ -z $mysql_host ]










