1.变量替换
example1:根据匹配规则删除变量部分内容
example2:根据匹配规则替换变量部分内容
2.字符串处理
注意: ${}计算索引是从0开始 expr计算索引是从1开始。
扫描二维码关注公众号,回复: 5245180 查看本文章
3.关于字符串的完整脚本实例
#!/bin/bash
string="Bigdata process framework is Hadoop,Hadoop is an open source project"
function tips_info
{
echo "*********************************"
echo "*** (1) 打印string长度"
echo "*** (2) 在整个字符串中删除Hadoop"
echo "*** (3) 替换第一个Hadoop为Mapreduce"
echo "*** (4) 替换全部Hadoop为Mapreduce"
echo "*********************************"
}
function print_len
{
if [ -z "$string" ];then
echo "Error,string is null"
exit 1
else
echo "${#string}"
fi
}
function del_hadoop
{
if [ -z "$string" ];then
echo "Error,string is null"
exit 1
else
echo "${string//hadoop/}"
fi
}
function rep_hadoop_mapreduce_first
{
if [ -z "$string" ];then
echo "Error,string is null"
exit 1
else
echo "${string/Hadoop/Mapreduce}"
fi
}
function rep_hadoop_mapreduce_all
{
if [ -z "$string" ];then
echo "Error,string is null"
exit 1
else
echo "${string//Hadoop/Mapreduce}"
fi
}
while true
do
echo "[string=\"$string\"]"
tips_info
read -p "please Switch a Choice: " choice
case "$choice" in
1)
echo
echo "Length Of String is:`print_len`"
echo
continue
;;
2)
echo
echo "删除Hadoop后的字符串为:`del_hadoop`"
;;
3)
echo
echo "替换第一个Hadoop后的字符串为:`rep_hadoop_mapreduce_first`"
;;
4)
echo
echo "替换全部的Hadoop后的字符串为:`rep_hadoop_mapreduce_all`"
;;
q|Q)
exit 0
;;
*)
echo "error,unlegal input,legal input only in { 1|2|3|4|q|Q }"
continue
;;
esac
done