本文共 2278 字,大约阅读时间需要 7 分钟。
sed 常见用法
(1)添加注释
注释掉指定行:
说明:注释掉第2行和第三行
删除c语言的注释(//)
(2)删除注释
(3)获取脚本所在目录
shell脚本文件名称:loc.sh
内容:
执行:
ctier@allinone-yunyingyong-2-v-o:/tmp/abc/ccc$ ./loc.sh
/tmp/abc/ccc
(4)在键值对后面增加export
prop.txt的内容如下:
name=whuang
age=27
运行结果:
ctier@allinone-yunyingyong-chanjet02-v-o:/tmp/abc$ sed -e "s#\(.*\)=.*#&\nexport \1#" prop.txt
name=whuang
export name
age=27
export age
对于path变量
ctier@allinone-yunyingyong-chanjet02-v-o:/tmp/abc$ sed "s#\(.*\)=\(.*\)#if [ x\"$\1\" = x ];then\n\t&\nelse\n\t&:\"$\1\"\nfi\nexport \1#" prop.txt
if [ x"$name" = x ];then
name=whuang
else
name=whuang:"$name"
fi
export name
if [ x"$age" = x ];then
age=27
else
age=27:"$age"
fi
export age
(5)解决中标麒麟注册服务失败的问题
#!/bin/sh if [ `id -u` -ne 0 ];then echo "Please rerun this script as root ." exit 2 fi if [ -z "$1" ];then echo "please specify patch path:" exit 2 fi
#-------------------------------- function start ------------------------------------------ delete_Required_Start() { filePath="$1" if [ -f "$filePath" ];then sed -i '2,10{/Required-Start/d;}' "$filePath" sed -i '2,10{/Required-Stop/d;}' "$filePath" fi } #-------------------------------- function end ------------------------------------------
this_dir=`pwd` patch_path="$1" ls "$patch_path/patch.sh" >/dev/null 2>&1 if [ $? -eq 0 ];then patch_path="$patch_path/patch" fi server_bin="$patch_path/patch/build/SERVER/bin" tomcat_bin="$patch_path/patch/build/STOOLS/tomcat/bin" cd "$server_bin" for ii in `ls *7d`;do delete_Required_Start "$ii" done
cd "$tomcat_bin" for ii in `ls *7d`;do delete_Required_Start "$ii" done
sed -i 's/log_warning_message\([ ]*(\)/log_warning_msg\1/' /lib/lsb/init-functions
cd "$this_dir" |
转载地址:http://blwno.baihongyu.com/