脚本功能需求:
输入一个数字,然后运行对应的命令。
显示如下:
**cmd menu** 1-date 2-ls 3-who 4-pwd
输入相关数字可以运行相应命令
#!/bin/bash
#author:tongby
#version:t1
#date:2023-10-21
echo "***cmd menu***1-date 2-ls 3-who 4-pwd"
while :
do
read -p "please choice a number 1-4:" n
case $n in
1)
date
break
;;
2)
ls
break
3)
who
break
;;
4)
pwd
break
;;
*)
echo "worning choice,please have a try again!"
;;
esac
done
书写不难,主要是回顾case用法
脚本功能需求:
执行后打印一行“please input a number:”
用户输入数值后,打印出该数值,然后再次要求用户输入数值
直到用户输入“end”停止
#!/bin/bash
#author:tongby
#version:t1
#date:2023-10-21
while :
do
read -p "Please input a number:(when you input "end" to quit)" n
num=`echo $n |sed -r 's/[0-9]//g'|wc -c`
if [ $n == "end" ]
then
exit
fi
if [ $num -ne 1 ]
then
echo "what you input is not a number!Try again!"
else
echo "The number you entered is: $n"
fi
done
wc -c计算字符串长度,回车也算一个字符