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

发布于 2024-09-18 04:38:05 字数 656 浏览 9 评论 0原文

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

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

当 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'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.

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.

[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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末染殇 2024-09-25 04:38:05

您是否正在寻找类似以下内容的内容?

#!/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

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
余生共白头 2024-09-25 04:38:05

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

如果您需要的话,可以自定义您的 bash 提示符。

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

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.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文