在select循环中使用case语句

时间:2017-11-22 19:05:35

标签: bash shell

我正在进行bash编程,其中我在select循环中使用case语句。但是,每当我输入任何输入时,控件都会进入默认情况而不是实际应该去的情况。例如。当我输入日期,而不是去日期的情况下,它将进入默认情况。您是否有人注意到某些错误?

 #!/bin/bash
    select command in date pwd ls
    do
    case $command in
         date)date;;
         pwd)pwd;;
         ls)ls;;
         *)echo"wrong";;

    esac
    done

1 个答案:

答案 0 :(得分:1)

  

[...]当我输入日期时,它会转到默认大小写。

select内置程序使用编号选项。您需要为第一个选项“date”输入1,为第二个选项“pwd”输入2,依此类推。

help select的相关部分:

select: select NAME [in WORDS ... ;] do COMMANDS; done
    [...]  If the line consists of the number
    corresponding to one of the displayed words, then NAME is set
    to that word.  If the line is empty, WORDS and the prompt are
    redisplayed.  If EOF is read, the command completes.  Any other
    value read causes NAME to be set to null.  [...]