LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-10-2013, 03:43 AM   #1
shozda
LQ Newbie
 
Registered: Jun 2013
Posts: 2

Rep: Reputation: Disabled
Cool program in bash which draw a number


i would like to write a program wchich draws a number on of 100 and than it wirte to user it's too much if its bigger than drawed number or it's to low if number (which wrote user) is lower.
I have stomething like this but I have got errors:


Code:
num=`$((RANDOM/327))`
echo „write a number from 0 -100”
read type
until  [ $num = $type ]
do
if [$num –lt $type]
then echo “too much!”
sleep 1
clear
echo “Try again!”
read type
esle if [$num –gt $type] then
echo “too low!”
sleep 1
echo “Try again!”
read type
fi
fi
done
well I get multiple errors. could anybodie help?
 
Old 06-10-2013, 04:29 AM   #2
litzel
Member
 
Registered: May 2013
Location: Athens, Greece
Distribution: Xubuntu 12.04, Linux Mint 13
Posts: 80

Rep: Reputation: 5
Hi,

What kind of errors are you getting?

I can suggest using "" on echo and elif for else if for starters.
 
Old 06-10-2013, 04:59 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Well like the post above, telling us what errors you get would be better than us guessing.

I also found it difficult to understand exactly what it is you are trying to do?
You mention about the user entering numbers in the range 0 - 100 in the script but then have used the number 327??? I am not sure I see the point here?

Some of the things I noticed:

1. Use `` or $() but not both, especially in the same point of execution ... I can say that $() is recommended for a bash script.

2. You use -lt and -gt in some tests but then use '=' when checking equality, however this is a string test and not a numerical one, you should in fact use -eq

3. Further to point 2 above, instead of using [] for numerical tests, I would suggest using (()) which are designed for this and then you can also use the standard mathematical symbols like <, >, etc

4. If you have more than 2 tests to check against, I would also suggest looking into the case statement


Hope some of that helps. If you could maybe explain a little further your aim, we may have other suggestions too
 
Old 06-10-2013, 02:40 PM   #4
litzel
Member
 
Registered: May 2013
Location: Athens, Greece
Distribution: Xubuntu 12.04, Linux Mint 13
Posts: 80

Rep: Reputation: 5
Try and execute this, if I understood well what you want to do then it should work just fine:

Code:
r=$(( $RANDOM % 100 ) + 1)
echo "write a number from 0 - 100"
read x
until [ $r -eq $x ]
do
if [ $r –lt $x ]
then 
echo "too much!";
sleep 1;
clear;
echo "Try again!"
read x
elif [ $r –gt $x ]
then
echo "too low!";
sleep 1;
echo "Try again!";
read x;
fi
fi
done
Hope I've helped
 
Old 06-11-2013, 03:04 AM   #5
shozda
LQ Newbie
 
Registered: Jun 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Got it almoust

Ok quite good working prog is:
Code:
type=$((RANDOM/327))
until [ $num = $type ];
do
clear
echo “give me number from 1-100”
read num
if [$num –lt $type]
then echo -e “to low!”
elif [$num –gt $type] 
then
echo  –e “to much!”
fi
sleep 1
done

however I have an error because it always pick up 0 (zero) I don't know why. Maybey it's fedora or sth.

Last edited by shozda; 06-11-2013 at 03:08 AM.
 
Old 06-11-2013, 06:15 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If using [ ], its recommended to use [[ ]] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS.
As above, use (( )) instead for numeric comparisons, OR use the correct operator -eq instead of '='
http://www.tldp.org/LDP/abs/html/comparison-ops.html

when using [ ] or [[ ]] always leave a space on each side
Code:
if [[ a -eq 0 ]]
then
Please indent your code for easy reading for eg if blocks and loop blocks
Code:
if [$num –lt $type]
then 
    echo -e “to low!”
elif [$num –gt $type]
If you add 'set -xv' at the top, it'll show you what's happening.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a program to draw Directional map diagrams? (FLOWCHARTS) izquierdista Linux - Software 2 07-28-2010 07:31 AM
corel draw like program in linux? smurthygr Linux - Software 13 11-30-2008 09:42 AM
a program to draw into the desktop? Samus_ Linux - Software 2 05-31-2008 07:24 PM
i want to draw electrcal circuits suggest me a program amolgupta Linux - Software 5 04-09-2006 11:42 AM
Is there a Linux program that can draw box plots? Baryonic Being Linux - Software 1 02-12-2005 12:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration