如何创建带有“shell 提示符”的 CLI 程序?
我对编程很陌生。下面的任务看起来很简单,但我不知道该怎么做。感谢有人能给我一些指导。
我使用的是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否正在寻找类似以下内容的内容?
Are you looking for something along the lines of the following?
通过 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: