BGN*_*BGN 2 bash shell command-line prompt
我对编程很陌生。下面的任务看起来很简单,但我不知道如何去做。感谢有人能给我一些指导。
我正在使用 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
Run Code Online (Sandbox Code Playgroud)
我想我知道如何在屏幕上打印一些东西并从用户那里获得输入,但我发现它看起来很不专业。我相信有更好的方法来创建这种程序。
谁能给我一些指导如何创建这种程序?感谢您能给我任何示例程序的链接。谢谢。
小智 5
您是否正在寻找以下方面的内容?
#!/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
Run Code Online (Sandbox Code Playgroud)