Link to home
Start Free TrialLog in
Avatar of mjs2011
mjs2011Flag 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 .
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

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

ASKER

the guess2 program in attachment file
guess2.txt
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
Avatar of 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.
what does guess2 do?
run it what does it do?
VALUE=$(guess2)
echo "This is the $VALUE to guess/"
what do you see?
Avatar of 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
ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mjs2011

ASKER

both of them doesn't running " cannot get output of guess2"
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?
One additional command whose output may help narrow what issue you are trying to address
ldd guess2
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.
Avatar of phoffric
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.
Avatar of 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
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.
./guess2
echo $? what do you get?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of mjs2011

ASKER

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

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