用unix shell 做一个菜单 里面有4个选项:1 显示信息 2计算 3排序 4退出系统 (每个选项有具体要求如下)
1.显示信息:需要有1个或者2个输入的信息:假如输入第一个就显示第一个的信息输入第2个旧显示第2个的信息。这题输入的参数包括:用户名,登录目录的绝对路径设定的shell;...
1.显示信息:需要有1个或者2个输入的信息:假如输入第一个 就显示第一个的信息 输入第2个旧显示第2个的信息。这题输入的参数包括:用户名, 登录目录的绝对路径 设定的shell;和2011年的日历(输入的参数 第一个必须是目录名(directory name) 第二个必须是文件名 (file name) 显示的信息就是 要求里那些用户名什么的)
2.计算:输入2个数字(每个大于2位)和1个字母(A,S,M或D)分别代表一个运算(加,减,乘或除),进行运算并显示出结果。
3.输入一串大写字母。输入完毕后加个0.找寻并按照字母表顺序显示第一个字母。
4.询问是否退出:输入Y就是退出 输入N就是返回菜单选项 展开
2.计算:输入2个数字(每个大于2位)和1个字母(A,S,M或D)分别代表一个运算(加,减,乘或除),进行运算并显示出结果。
3.输入一串大写字母。输入完毕后加个0.找寻并按照字母表顺序显示第一个字母。
4.询问是否退出:输入Y就是退出 输入N就是返回菜单选项 展开
展开全部
又是你阿, 那给你一个完整回答吧
#!/bin/bash
PS3='Input option(1-4):'
stack="Display Cacluate Sort Quit"
select choice in $stack; do
if [ "$choice" = "Display" ]; then
read -p "Input you want to display[name/path/shell/cal]" info
if [ "$info" = "name" ]; then
echo "$USER"
elif [ "$info" = "path" ]; then
echo "$HOME"
elif [ "$info" = "shell" ]; then
echo "$SHELL"
elif [ "$info" = "cal" ]; then
cal 2011
fi
elif [ "$choice" = "Cacluate" ]; then
read -p "Input one number: " num1
read -p "Input the other number: " num2
read -p "Input method[A/S/M/D]: " mod
case $mod in
'A' ) echo "$num1 + $num2 is $((num1+num2))";;
'S' ) echo "$num1 - $num2 is $((num1-num2))";;
'M' ) echo "$num1 * $num2 is $((num1*num2))";;
'D' ) echo "$num1 / $num2 is $((num1/num2))";;
* ) echo "wrong method";;
esac
elif [ "$choice" = "Sort" ]; then
read -p "Input an string with 0 ended:" str
res=`echo $str | awk -F"0" '{print $1}'`
echo $res | awk -F "" '{ a=$1; for(i=2;i<NF;i++) if( $i < a) a=$i; print a}'
elif [ "$choice" = "Quit" ]; then
read -s -n1 -p "Do you really want to quit?(Y)"
if [ "$REPLY" = "Y" ];then
exit 0;
fi
else
echo "Invalid selection, Please input number."
fi
done
运行效果
[we@lemon shell]$ ./menu
1) Display
2) Cacluate
3) Sort
4) Quit
Input option(1-4):2
Input one number: 21
Input the other number: 32
Input method[A/S/M/D]: M
21 * 32 is 672
Input option(1-4):
#!/bin/bash
PS3='Input option(1-4):'
stack="Display Cacluate Sort Quit"
select choice in $stack; do
if [ "$choice" = "Display" ]; then
read -p "Input you want to display[name/path/shell/cal]" info
if [ "$info" = "name" ]; then
echo "$USER"
elif [ "$info" = "path" ]; then
echo "$HOME"
elif [ "$info" = "shell" ]; then
echo "$SHELL"
elif [ "$info" = "cal" ]; then
cal 2011
fi
elif [ "$choice" = "Cacluate" ]; then
read -p "Input one number: " num1
read -p "Input the other number: " num2
read -p "Input method[A/S/M/D]: " mod
case $mod in
'A' ) echo "$num1 + $num2 is $((num1+num2))";;
'S' ) echo "$num1 - $num2 is $((num1-num2))";;
'M' ) echo "$num1 * $num2 is $((num1*num2))";;
'D' ) echo "$num1 / $num2 is $((num1/num2))";;
* ) echo "wrong method";;
esac
elif [ "$choice" = "Sort" ]; then
read -p "Input an string with 0 ended:" str
res=`echo $str | awk -F"0" '{print $1}'`
echo $res | awk -F "" '{ a=$1; for(i=2;i<NF;i++) if( $i < a) a=$i; print a}'
elif [ "$choice" = "Quit" ]; then
read -s -n1 -p "Do you really want to quit?(Y)"
if [ "$REPLY" = "Y" ];then
exit 0;
fi
else
echo "Invalid selection, Please input number."
fi
done
运行效果
[we@lemon shell]$ ./menu
1) Display
2) Cacluate
3) Sort
4) Quit
Input option(1-4):2
Input one number: 21
Input the other number: 32
Input method[A/S/M/D]: M
21 * 32 is 672
Input option(1-4):
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询