0

I have a Java console application which accepts the user input and performs action based on the input. Below is the sample:

**Payout Table          Multiplier**
=======================================
Royal Flush     |       250
Straight Flush  |       50
Four of a Kind  |       25
Full House      |       9
Flush           |       6
Straight        |       5
Three of a Kind |       3
Two Pairs       |       2
Royal Pair      |       1

-----------------------------------
Balance: $500
Enter bet:

I have to enter the amount to bet with:

Enter bet: 300

HAND: [K Spades, J Spades, 10 Clubs, 9 Diamonds, J Hearts]  
Enter positions of cards to keep (e.g. 1 4 5):

Enter positions of cards to keep (e.g. 1 4 5):  
3

Held Cards: [10 Clubs]  
NEW HAND: [10 Clubs, 5 Clubs, 10 Hearts, A Diamonds, 10 Diamonds]

**Three of a Kind**

Your balance: $1400  
One more game (y or n)?

y

I just need help with an idea of a bash script to automate this manual process.

mkrieger1
  • 14,486
  • 4
  • 43
  • 54
  • 1
    The point of a console app is generally that the user will interact with the app. How about passing arguments via command line to a non console application instead? – Omar Abdel Bari Oct 19 '20 at 18:57
  • You mean, you're asking how to **pipe** input to the program (using `|` or ` – Andreas Oct 19 '20 at 18:59

1 Answers1

1

Depending on the complexity of the different cases that you would like to enter, you might be able to use expect: https://linux.die.net/man/1/expect

Example for a similar problem: Use Expect in a Bash script to provide a password to an SSH command

Example:

#!/usr/bin/expect
eval spawn java Main

# May need to adjust prompts to match better
interact -o -nobuffer -re "Enter bet: " return
send "300\r"
interact -o -nobuffer -re "Enter positions of cards to keep (e.g. 1 4 5):\r" return
send "3\r"
interact -o -nobuffer -re "One more game (y or n)?" return
send "y\r"
interact
Will
  • 5,943
  • 3
  • 29
  • 41