1. 程式人生 > 其它 >shell字串處理完整指令碼(三)

shell字串處理完整指令碼(三)

#!/bin/bash
#
string="Hello LiLei,LiLei is my friend,Hello GoodBey"

function print_tips
{
echo "------------------------"
echo "(1) ´列印string長度"
echo "2.刪除字串中所有的LiLei"
echo "3.替換第一個Hello 為你好"
echo "4.替換全部的Hello 為你好"
echo "------------------------"


}

function len_of_string
{
echo "${#string}"
}

function del_LiLei
{
echo "${string//LiLei/}"
}

function rep_Hello_nihao_first
{
echo "${string/Hello/nihao}"
}

function rep_all
{
echo "${string//Hello/nihao}"
}

while true
do
echo "$string"
echo
print_tips
read -p "Pls input your choice(1|2|3|4|q|Q) : " choice
case $choice in
1)
len_of_string
;;
2)
del_LiLei
;;
3)
rep_Hello_nihao_first
;;
4)
rep_all
;;
q|Q)
exit
;;
*)
echo "Error,input only in {1|2|3|4|q|Q}"
;;
esac
done