views:

62

answers:

2

Hi,

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.

A: 

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:

karlphillip
+1  A: 

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
Greg