LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-15-2009, 02:50 PM   #1
pvictory1
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Rep: Reputation: 0
Unhappy A script that recognizes some input errors


Hello this is my first time posting and what I can't figure out is I need to add into the script to make it recognize some input errors and I just do know how to do this. Please help


Code:
#This program accepts three numbers and shows the largest of them
#
echo
echo "Enter three numbers and I will show you the largest>> \c
read num1 num2 num3
if test "$num1" -gt "$num2" -a "$num1" -gt "$num3"
then 
   echo "The largest number is: $num1"
elif test "$num2" -gt "$num1" -a "$num2" -gt "$num3"
then
   echo "The largest number is:$num2"
else
   echo "The largest number is:$num3"
fi 
echo "done!"
echo
exit 0
 
Old 11-15-2009, 05:56 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
Welcome to LQ.

Quote:
Originally Posted by pvictory1 View Post
make it recognize some input errors
- Exactly what type of errors should it recognize (numbers equal or greater than 100, strings, carriage returns, empty $#, what)?
- What have you tried and in what way did it not work (post code and errors)?

Code:
function help() { echo "${FUNCNAME}: Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.tldp.org/LDP/abs/html/; }
 
Old 11-15-2009, 06:08 PM   #3
DaveQB
Member
 
Registered: Oct 2003
Location: Sydney, Australia.
Distribution: PCLinuxOS 2010.12, Debian Lenny
Posts: 396

Rep: Reputation: 38
To check what is entered is a number, you could do it a number of ways.

One way to to search it.

echo $num | grep [[:alpha:]]
if [ "$?" -eq 1 ]
then
echo You entered a number.
fi

Or you could just use

Z=$((num1 + 1))
if [ "$?" -eq 0 ]
then
echo You entered a number.
fi
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Input/output errors at start-up fof3 Linux - Newbie 3 02-13-2006 09:52 PM
KNOPPIX 4.02 Input/Output Errors nomind Linux - Newbie 2 11-03-2005 11:17 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM
Input/Output errors on files Poetics Slackware 4 08-25-2003 06:57 PM
overcoming input/output errors heiseltc Linux - General 0 02-26-2003 01:29 PM


All times are GMT -5. The time now is 06:08 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration