LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to I make a console based application with I/O. (http://www.linuxquestions.org/questions/programming-9/how-to-i-make-a-console-based-application-with-i-o-4175411318/)

cmb271 06-13-2012 08:31 PM

How to I make a console based application with I/O.
 
1 Attachment(s)
Attachment 9891I'm working on a project that is text based that runs in the terminal. I'm working on the script on actions and reactions but I need to know what should I program it in. All it needs to do is Print a question or comment on the screen, allow the user to input a text like A1, A2 that will redirect the user to a action and possibly access different files for answers to some questions. It's going to be like a console based or terminal based application that talks to its users with limited commands. I need to know what programming language should I use, I have difficulty with Python I don't know why.

I always here it is the easiest programming language to learn but every time i try something goes wrong. It is to annoying, or not compatible with my current version which is Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2


Any other good programming language, would bash be good for this type of idea, maybe ruby or perl, I'm not even close to advanced enough for C# or C++ and it is completely terminal based.

Picture is kinda example of the kinda of interface I want but not the general idea only the interface and Input output ability

Nominal Animal 06-14-2012 02:15 PM

There is no correct answer to your question!

Personally, I'd find out if Bash provides enough functionality. I'd prefer an interpreted "script" language over a compiled one, since these kinds of programs tend to evolve over time, and having it be trivial to fix issues or add new features is extremely useful. I'd start with Bash, simply because I've done a lot of similar stuff with Bash, and it seems to fit.

To show you how simple similar stuff is in Bash, here is a commented version of guess-the-number game:
Code:

#!/bin/bash

# Range of random values allowed (integers only)
MIN_VALUE=1
MAX_VALUE=1000

# Generate a random value using the RANDOM special variable.
VALUE=$(( MIN_VALUE + ( RANDOM % (MAX_VALUE - MIN_VALUE + 1 ) ) ))

# Output the premise.
printf 'I\047m thinking of a number between %d and %d, inclusive.\n' $MIN_VALUE $MAX_VALUE

# No tries yet. Loop.
TRIES=0
while read -p 'Your guess: ' GUESS ; do

    # Handle special values of guess:
    case "$GUESS" in
      [A-Za-z]*) # Anything beginning with a letter?
        printf 'You give up? I was thinking of %d.\n' $VALUE
        exit 1
        ;;
      *)        # Everything else.
        # Is GUESS not valid arithmetic expression?
        if ! ( exec &>/dev/null ; GUESS=$(( $GUESS )) ); then
            printf 'I don\047t understand what you mean by "%s". Try again.\n' "$GUESS"
            continue
        fi
        GUESS=$(( $GUESS ))
        ;;
    esac

    # Make sure GUESS is an integer.
    GUESS=$(( GUESS )) || exit $?

    # If GUESS is outside the limits, complain.
    if [ $GUESS -lt $MIN_VALUE ]; then
        printf 'Your guess %d is under the minimum, %d. Try again!\n' $GUESS $MIN_VALUE
        continue
    fi
    if [ $GUESS -gt $MAX_VALUE ]; then
        printf 'Your guess %d is over the maximum, %d. Try again!\n' $GUESS $MAX_VALUE
        continue
    fi

    # This is a new try.
    TRIES=$(( TRIES + 1 ))

    if [ $GUESS -gt $VALUE ]; then
        printf '%d is too high.\n' $GUESS
    elif [ $GUESS -lt $VALUE ]; then
        printf '%d is too low.\n' $GUESS
    else
        printf '%d is right! You only used %d tries, too!\n' $VALUE $TRIES
        exit 0
    fi
done

Note that I am using the POSIX-compatible test operator [ rather than the compound command [[ most here will recommend you to use instead. That is because I want to keep my Bash scripts POSIX compatible; I sometimes need to run them using e.g. dash instead. In fact, if you modify the line containing RANDOM to
Code:

VALUE=$( awk -v min=$MIN_VALUE -v max=$MAX_VALUE 'BEGIN { srand(); printf("%d\n", min+int(rand()*(max-min+1))) }' )
which uses awk to generate the random value since POSIX shells do not provide $RANDOM, the script also works if you run it using dash (#!/bin/dash).

Other details that come very handy with command-line scripts are ANSI escape sequences. For example, I like to output important error messages in red:
Code:

message="Some error message"
errorcode=1
printf '\033[0;31mError %d: \033[1;31m%s\033[0;31m.\033[0m\n' $errorcode "$message" >&2

Note that if the script is not always interactive, it would be best to use ANSI escape sequences only if run interactively, with the output going to a terminal. To test if standard input, output, or error (>&2) are terminals, you can use
Code:

if ( stty &>/dev/null ); then
    echo "Standard input is a terminal"
fi

if ( stty &>/dev/null ) <&1 ; then
    echo "Standard output is a terminal"
fi

if ( stty &>/dev/null ) <&2 ; then
    echo "Standard error is a terminal"
fi

Something like this is very useful:
Code:

if ( stty &>/dev/null ) <&1 ; then
    ANSI_RED="$(printf '\033[0;31m')"
    ANSI_NORMAL="$(printf '\033[0m')"
else
    ANSI_RED=""
    ANSI_NORMAL=""
fi

printf "${ANSI_RED}"'No! Bad dog!'"${ANSI_NORMAL}"'\n̈́'

Note that I switch between double quotes and single quotes, because I don't want the shell to interpret ! or \n.

414N 06-14-2012 02:53 PM

If your terminal application needs to be platform-independent then I would probably prefer Python or Java (maybe a bit of a overkill) over a simple bash script.
What are your issues with Python?


All times are GMT -5. The time now is 06:14 AM.