LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 06-13-2012, 08:31 PM   #1
cmb271
LQ Newbie
 
Registered: May 2012
Location: Atlanta, Georgia
Distribution: Windows XP, Ubuntu 12.04, Crunchbang
Posts: 15

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


term.pngI'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 08:32 PM. Reason: Forgot something
 
Old 06-14-2012, 02:15 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
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.
 
Old 06-14-2012, 02:53 PM   #3
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 608

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


Reply

Tags
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 02:56 AM
GUI based package-manager installation on console based linux kevinchkin Linux - Software 2 07-14-2009 12:25 AM
Console Based Games? pxumsgdxpcvjm Linux - Games 2 08-15-2007 07:16 AM
Console application? (e.g. Hyperterminal) jtdwyer Linux - Newbie 1 12-29-2005 10:51 PM
Console based IM software? menhilmor Linux - Software 6 02-14-2003 08:34 PM


All times are GMT -5. The time now is 11:35 AM.

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