Skip to content

Latest commit

 

History

History
57 lines (53 loc) · 675 Bytes

42.md

File metadata and controls

57 lines (53 loc) · 675 Bytes

题目要求

写一个shell脚本,实现简单的弹出式菜单功能,用户能根据显示的菜单项从键盘选择执行对应的命令。

参考答案

#!/bin/bash
PS3="Please input your choice(1-4): "
select i in w ls pwd quit
do
    case $i in 
	w)
	    w
	    ;;
	ls)
	    ls
	    ;;
	pwd)
	    pwd
	    ;;
	quit)
	    exit
	    ;;
	*)
	    echo "Please input 1-3."
	    ;;
    esac
done

参考答案2

#!/bin/bash
echo -e "1) w\n2) ls\n3) pwd\n4) quit"
while :
do
read -p "Please input your choice(1-4): " c
case $c in 
    1)
	w
	;;
    2)
	ls
	;;
    3)
	pwd
	;;
    4)
	exit
	;;
    *)
	echo "Please input 1-4."
	;;
esac
done