Discussion Locked

bash script

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

How would I write a script for the scriptfile "puzzlescript" to make the gotchascript echo back "gotcha"
? The idea here is to set up the first script named gotcha so that when $sum and $rightsum are equal
the script will echo back gotcha. It is set up to look at and execute the scriptfile 'puzzlescript'
to see if its value is equal to $rightsum. Puzzlescript is set up in another part of the directory
and has to be written in such a way that when the gotchascript comes to run it, it triggers the echo
gotcha from the gotchascript.

#!/bin/bash
#gotchascript

if [ ~/puzzle ]
then
sum=` ~/puzzle 5 10 20 $$`
rightsum=`expr 35 + $$`
if [ $sum -eq $rightsum ]
then
echo gotcha
echo
fi

What I've come up with so far is,

#!/bin/bash
#puzzlescript

sum=`expr 5 + 10 + 20 + $$`
echo $sum

but it won't cause the echo gotcha from the gotchascript

6 replies

by timthetoolman

Hi,

I'm a bit hampered here as I no longer have my dual boot to Linux so I cant test this stuff...

...but what do you get if you echo sum and rightsum just before your if statement in gotcha ?

Do they look right?

Also if you run puzzle directly, is the sum correctly echo'd from that?

You need to narrow down exactly where the problem is and the best way to do that is to work out at exactly what point you're not getting the result you want.

Cheers,
Tim.

by shanghai sam

I assume ~/puzzle should be "puzzlescript" here. If you are testing for the existance of ~/puzzle before it is called, and verification that it is executable, do:

if [ -x ~/puzzle ]
for the first part.

Also, when comparing numerical values,as you do in the second 'if' statement, it should be:

if [ $sum == $rightsum ]

I am a bit puzzled by your use of '$$' in the summation. $$ is a special variable containing the current process ID number. Further, it looks like you actually wantthe puzzle script to use the command line arguments. If that is indeed what you want, then puzzle should look like:

#!/bin/bash
# modified to accept command line input.
addthese=`echo $@|sed 's/ / + /g'`
sum=`expr $addthese`
echo $sum

With those changes, you should be well on your way to making this work as intended.

--
Brainbench 'Most Valuable Professional' for Internet Security
Brainbench 'Most Valuable Professional' for Network Concepts
Brainbench 'Most Valuable Professional' for Linux Admin
http://www.brainbench.com

This discussion is closed to new replies.