bash 如何创建带有“shell 提示”的 CLI 程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3673411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:36:41  来源:igfitidea点击:

How to create a CLI program with "shell prompt"?

bashshellcommand-lineprompt

提问by BGN

I'm very new in programming. The following task looks very simple but I don't know how to do it. Appreciate if anyone can give me some guidance.

我对编程很陌生。下面的任务看起来很简单,但我不知道如何去做。感谢有人能给我一些指导。

I'm using Linux OS. I would like to create a CLI program and let the user run it in shell terminal. I plan to use Bash shell script to create this program, but C++ or Perl should be ok too. The CLI program will accept commands from the user, execute it, and then optionally show the result summary on the screen.

我正在使用 Linux 操作系统。我想创建一个 CLI 程序并让用户在 shell 终端中运行它。我打算使用 Bash shell 脚本来创建这个程序,但是 C++ 或 Perl 应该也可以。CLI 程序将接受来自用户的命令,执行它,然后有选择地在屏幕上显示结果摘要。

When the CLI program is running, I would like to make it always shows a "shell prompt" at the left-hand side of the shell terminal, just like the Bash shell prompt. The prompt indicates that the CLI program is waiting for the user to type in a command and press the return key.

当 CLI 程序运行时,我想让它总是在 shell 终端的左侧显示一个“shell 提示符”,就像 Bash shell 提示符一样。提示表明 CLI 程序正在等待用户输入命令并按回车键。

[AAA@Bash user]$ # This is Bash shell
[AAA@Bash user]$./CliApp.sh
CliApp > # The CLI program is running, user can type in command here
CliApp > exit
[AAA@Bash user]$ # Now the user has returned to the Bash shell

I think I know how to print something on the screen and get inputs from the users, but I found that it looks very unprofessional. I believe that there is a better way to create this kind of program.

我想我知道如何在屏幕上打印一些东西并从用户那里获得输入,但我发现它看起来很不专业。我相信有更好的方法来创建这种程序。

Can anyone give me some guidance how to create this kind of program? Appreciate if you can give me the links to any example program. Thanks.

谁能给我一些指导如何创建这种程序?感谢您能给我任何示例程序的链接。谢谢。

回答by Greg

Are you looking for something along the lines of the following?

您是否正在寻找以下方面的内容?

#!/bin/bash                                                                                                    
input=""
while [ "$input" != "exit" ]
do
    echo -n "cliApp($mode) > " && read input
    if [ "$input" = "config" ]
    then
        mode="config"
    elif [ "$input" = "up" ]
    then
        mode=""
    elif [ "$input" = "exit" ]
    then
    break
    else
        if [ "$mode" = "config" ]
        then
            if [ "$input" = "list" ]
            then
                echo "ABCD"
            else
                echo "Invalid"
            fi
        else
            echo "Invalid"
        fi
    fi
done

exit

回答by karlphillip

Writing a command interpreter through shell script sounds a bit redundant to me, since you must be using a command interpreter (bash, csh, sh, or something else) to be able to do that.

通过 shell 脚本编写命令解释器对我来说听起来有点多余,因为您必须使用命令解释器(bash、csh、sh 或其他东西)才能做到这一点。

It is possible to customize your bash prompt if that's what you're looking for.

如果这是您要查找的内容,则可以自定义您的 bash 提示。

There are some C/C++ libraries you could used to assist you on building your own command processor/interpreter, that includes fancyfeatures, like tab completion. You should take look at these:

您可以使用一些 C/C++ 库来帮助您构建自己的命令处理器/解释器,其中包括一些奇特的功能,例如制表符补全。你应该看看这些: