We help IT Professionals succeed at work.

I need assistance to write shell script for guess games

mjs2011
mjs2011 asked
on
I need assistance to write shell script to resolve guess number game, called “guess.sh" this shell script will use  guess2  program  which written by C Program to generate a secret number and shell script “guess.sh” will ask user to guess a secret number which generated by guess2 program .
Comment
Watch Question

Gerwin Jansen, EE MVETopic Advisor
Most Valuable Expert 2016

Commented:
We can offer help, can you show/post what you have already? Starting with pseudo code is what works best in developing such (small) scripts.

Author

Commented:
the guess2 program in attachment file
guess2.txt
Distinguished Expert 2017

Commented:
The binary file is not helpful.
What do you get when you run guess2?
In a shell script you need to assign the info.
You then loop through the number of attempts you wish to grant
Read is a way to get user input

Author

Commented:
the input is guessing number, outputs will be 0 if the guess number is equal secret number and 1 if the guess large than secret number and 2 if it is lower.
Distinguished Expert 2017

Commented:
what does guess2 do?
run it what does it do?
VALUE=$(guess2)
echo "This is the $VALUE to guess/"
what do you see?

Author

Commented:
I need to know how to execute guess2 (Program written by C program) inside shell scrip and get the output of guess2 and execute if condition on it

do I have to write
./guess2  on shell script to execute program ,

I need this program to run inside shell scrip and generate secret key, and user will enter guess number
Distinguished Expert 2017
Commented:
Please see the example in the comment above.

VALUE=$(./guess2)
echo "The value $VALUE is the random number."

Author

Commented:
both of them doesn't running " cannot get output of guess2"
Distinguished Expert 2017

Commented:
What happens when you try
./guess2
On the command line?
Post the output from running the below two commands.
file guess2
uname -a

Where did you guess2 from?
Did you compile it? Do you gave access to its source?
Distinguished Expert 2017

Commented:
One additional command whose output may help narrow what issue you are trying to address
ldd guess2
David FavorFractional CTO
Distinguished Expert 2018

Commented:
Note: The guess2.txt file you attached is an executable.

No sensible person will run a random executable on their machine.

This is a great way to end up with a hacked machine.

As mentioned above, either attach the source code or better pseudo code explaining your target code flow, as going through source code can take many hours, so you'll likely have far faster/more answers for pseudo code than for source code.
Instead of attaching programs, copy your source code in these comments. Hit the "CODE" button, and code tags will appear. Copy your code between the start and end code tags.

Author

Commented:
I will see you how to run C program

./guess2 <enter guess number> "then enter"

./guess2 55554433333
echo $?
1

echo s$ " to check the result, 0 that means the guess number is equal to secret number which generated inside the c program or 1 if guess number is large or 2 if guess number is low".

I cannot attach the code of C Program inside the shell script because the c program is encrypted, so that I need to run it from inside the shell script.

Let me see you what happen, when I tried to run C program inside shell script

1. Create file : touch guess2.sh
2. vi guess2.sh
3.
#!/bin/bash
./guess.2
$?
4. :wq
5. ./guess.sh

Let me to see you what happen, when I tried to run C program inside shell script

1. Create file : touch guess2.sh
2. vi guess2.sh
3.
#!/bin/bash
./guess.2
$?
4. :wq
5. ./guess.sh

I have run shell script three times , each times I got 0, that means C program is not working properly , because I didn't enter guess number to get 0 "Which means the guess number is equal to secret number" and the secret number is random number will change every time, its impossible to get 0 in three attempts
Distinguished Expert 2017

Commented:
you should first prompt the user for a guess.

echo -n "Please enter your guess: "
read guess
./guess2 $guess

now you can test whether the output of $? is 0 or 1.
Distinguished Expert 2017

Commented:
./guess2
echo $? what do you get?
Gerwin Jansen, EE MVETopic Advisor
Most Valuable Expert 2016
Commented:
The guess2 program is what you got and it likely contains a preprogrammed number, hence the encryption.

When you guess a number it should tell you if your guess is too high, too low or correct. It can do that by setting a return code. If your guess 500000 and it returns a 1 this could mean your guess is too high. If your guess is correct, it would return a 0. If your guess is too low it could return a -1 - did you try guessing a few low and high numbers?

When you are getting return codes like that, you can 'binary' guess (very fast) and find the right number.

Guess examples:

10000 returns 1
(number is less than 10000)

100 returns -1
(number is between 100 and 10000)

5000 returns 1
(number is between 100 and 5000)

etc.
1. Where did you get this c program? What documentation comes with this program?

2. Is the purpose of your script to find the secret number? If so, then, if as you say, the c program generates a new secret number every time it is run, then your chances of discovering the secret number is very slim. What makes you think that the secret number changes every time the program is run?

3. If, on the other hand, the secret number in the c program does not change from run to run, then your script can use a binary search to find the number quickly.
https://www.khanacademy.org/computing/computer-science/algorithms/intro-to-algorithms/a/a-guessing-game

4. To implement a binary search, your script will need a Loop and and if statement. And then it needs to keep track of whether the last guess was high or low

Author

Commented:
Thanks I found the solution I have wrote shell script as below

#!/bin/bash
./guess2
Value=$?
Distinguished Expert 2017

Commented:
That makes little sense.
What is the point?
What does guess2 outputs?
The $Value will be the e xit code, not sure I see .....