Avatar of mjs2011
mjs2011
Flag for Saudi Arabia asked on

I need assistance to write shell script for guess games

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 .
LinuxShell ScriptingUbuntu

Avatar of undefined
Last Comment
arnold

8/22/2022 - Mon
Gerwin Jansen

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.
mjs2011

ASKER
the guess2 program in attachment file
guess2.txt
arnold

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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
mjs2011

ASKER
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.
arnold

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

ASKER
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
arnold

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
mjs2011

ASKER
both of them doesn't running " cannot get output of guess2"
arnold

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?
arnold

One additional command whose output may help narrow what issue you are trying to address
ldd guess2
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
David Favor

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.
phoffric

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.
mjs2011

ASKER
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
arnold

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.
arnold

./guess2
echo $? what do you get?
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
phoffric

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
Your help has saved me hundreds of hours of internet surfing.
fblack61
mjs2011

ASKER
Thanks I found the solution I have wrote shell script as below

#!/bin/bash
./guess2
Value=$?
arnold

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