UNIX Shell循环用户输入

时间:2017-11-28 21:25:22

标签: shell unix

我希望用户选择以下其中一项:l,m,c或a。如果用户输入任何其他输入,它将告诉他们“无效输入”并要求他们再次输入。如果用户输入无效输入,我在尝试让它循环回来时遇到问题。我想我需要使用while循环,但我不确定。

public partial class MainWindow : Window
    {
        string connectionString;

        public MainWindow()
        {
            InitializeComponent();

            connectionString = ConfigurationManager.ConnectionStrings["data_here"].ConnectionString;
        }

1 个答案:

答案 0 :(得分:0)

使用select循环(使用case语句来减少一些冗长)。 (将==[ ... ]一起使用会告诉我您正在使用bash):

IFS= read -r -p "Enter the file name: " file
numlines=$(wc -l < "$file")
numwords=$(wc -w < "$file")
numchars=$(wc -c < "$file")
select variable in l m c a; do
    case $variable in
        l) echo "The file $file has $numlines lines"
           ;;
        m) echo "The file $file has $numwords words"
           ;;
        c) echo "The file $file has $numchars characters"
           ;;
        a) echo "The file $file has $numlines lines"
           echo "The file $file has $numwords words"
           echo "The file $file has $numchars characters"
           ;;
        *) echo "Invalid input"
           ;;
    esac
done