linux shell命令之Shell包装sed
Shell包装的脚本指的是内嵌系统命令或者工具的脚本,
这种脚本保留了传递给命令的一系列参数,因为包装脚本
中包含了许多带有参数的命令,使它能够完成特定的目的。
一个包装了sed的脚本
#package_sed.sh:该脚本用于将一个文件中的某个字符串替换为另一个字符串
#!/bin/bash
#提示用户需要修改的文件名
echo "please input the file which you want to change: "
read file
#判断命令行输入的参数是否为文件
if [ -f "$file" ]
then
file_name=$file
else
echo "File \"$file\" does not exist."
exit $bad_parameter
fi
#提示用户输入需要修改的字符串
echo "Please input the old pattern of string: "
read old_pattern
#提示用户修改后的字符串
echo "Please input the string which you want to modify: "
read new_pattern
#'s'在sed中是替换命令,pattern/表示匹配模式
# “g”即全局标志,用来自动替换每行中
# 出现的全部$old_pattern模式,而不仅仅替换第一个匹配
sed -e "s/$old_pattern/$new_pattern/g" $file_name
#成功调用脚本,将返回0
exit 0
cat test.txt
hello world 1 2 3
./package_sed.sh
please input the file which you want to change:
test.txt
Please input the old pattern of string:
world
Please input the string which you want to modify:
everyone
hello everyone 1 2 3
浙公网安备 33010602011771号