一、需求描述
变量string="Bigdata process framework is Hadoop,Hadoop is an open source project"
执行脚本后,打印输出string字符串变量,并给出用户以下选项:
(1)打印srting长度 (2)删除字符串中所有的Hadoop (3)替换第一个Hadoop为Mapreduc (4)替换全部Haddop为Mapreduce
用户输入数字1|2|3|4,可以执行对应项的功能,输入q|Q退出交互模式
二、思路分析
将不同的功能模块划分,并编写函数 function len_of_string function del_hadoop function rep_hadoop_mapreduce_first function rep_hadoop_mapreduce_all
三、程序代码
- #!/bin/bash
- string="Bigdata process framework is Hadoop,Hadoop is an open source project"
- function print_tips
- {
- echo "**********************************"
- echo "(1)打印srting长度"
- echo "(2)删除字符串中所有的Hadoop"
- echo "(3)替换第一个Hadoop为Mapreduc"
- echo "(4)替换全部Haddop为Mapreduce"
- echo "**********************************"
- }
- function len_of_string
- {
- echo "${#string}"
- }
- function del_hadoop
- {
- echo "${string//Hadoop/}"
- }
- function rep_hadoop_mapreduce_first
- {
- echo "${string/Hadoop/Mapreduce}"
- }
- function rep_hadoop_mapreduce_all
- {
- echo "${string//Hadoop/Mapreduce}"
- }
- while true
- do
- echo "【string=$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_hadoop
- ;;
- 3)
- rep_hadoop_mapreduce_first
- ;;
- 4)
- rep_hadoop_mapreduce_all
- ;;
- q|Q)
- exit
- ;;
- *)
- echo "Error,input only in {1|2|3|4|q|Q}"
- ;;
- esac
- done