Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-13-2012, 09:31 PM   #1
LQ Newbie
Registered: May 2012
Location: Atlanta, Georgia
Distribution: Linux Mint 17
Posts: 25

Rep: Reputation: Disabled
How to I make a console based application with I/O.

Click image for larger version

Name:	term.png
Views:	15
Size:	27.5 KB
ID:	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

Last edited by cmb271; 06-13-2012 at 09:32 PM. Reason: Forgot something
Old 06-14-2012, 03:15 PM   #2
Nominal Animal
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 944Reputation: 944Reputation: 944Reputation: 944Reputation: 944Reputation: 944Reputation: 944Reputation: 944
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:

# Range of random values allowed (integers only)

# Generate a random value using the RANDOM special variable.

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

# No tries yet. Loop.
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"
         GUESS=$(( $GUESS ))

    # 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
    if [ $GUESS -gt $MAX_VALUE ]; then
        printf 'Your guess %d is over the maximum, %d. Try again!\n' $GUESS $MAX_VALUE

    # 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
        printf '%d is right! You only used %d tries, too!\n' $VALUE $TRIES
        exit 0
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
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:
message="Some error message"
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
if ( stty &>/dev/null ); then
    echo "Standard input is a terminal"

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

if ( stty &>/dev/null ) <&2 ; then
    echo "Standard error is a terminal"
Something like this is very useful:
if ( stty &>/dev/null ) <&1 ; then
    ANSI_RED="$(printf '\033[0;31m')"
    ANSI_NORMAL="$(printf '\033[0m')"

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.
Old 06-14-2012, 03:53 PM   #3
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 628

Rep: Reputation: 185Reputation: 185
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?


linux, programming, programming language, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make a terminal based application cmb271 Linux - Software 1 06-11-2012 03:56 AM
GUI based package-manager installation on console based linux kevinchkin Linux - Software 2 07-14-2009 01:25 AM
Console Based Games? pxumsgdxpcvjm Linux - Games 2 08-15-2007 08:16 AM
Console application? (e.g. Hyperterminal) jtdwyer Linux - Newbie 1 12-29-2005 11:51 PM
Console based IM software? menhilmor Linux - Software 6 02-14-2003 09:34 PM

All times are GMT -5. The time now is 06:25 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration