C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-15-2009, 07:24 PM   #1
Registered User
 
Join Date: Oct 2009
Posts: 16
Unhappy Script File

Quote:
My teacher gave us this assigment and he is very vague on his questions. But the question reads write the largest script file that calculates the smallest of three integer numbers that are read from the keyboard. Make it able to recongnize some input errors. I guess he does care what type of errors they are as long as they can be read. The problem is I don't know at all how to do that part of the assigment.
Code:
#This program accepts three numbers and shows the largest of them
#
echo
echo "Enter three numbers and I will show you the largest of them>> \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
victory1 is offline   Reply With Quote
Old 11-16-2009, 10:58 AM   #2
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,225
You can use functions, arrays, and POSIX regular expressions in bash scripts:
Code:
#!/bin/bash

function testnumber {
	if [[ $1 =~ "^[[:digit:]]+$" ]]; then
		return 1;
	elif [[ $1 =~ "^[[:digit:]]+[.][[:digit:]]+$" ]]; then
		return 1;
	else
		return 0;
	fi
}

testray=(1.5 x 14 4f7 this 2 666 1.3.4)

for e in ${testray[@]}; do
	testnumber $e;
	if [[ $? > 0 ]]; then
		echo $e
	fi
done
This accepts integers and decimal numbers. Return values with bash are wierd -- unless you are returning a single number <256, you need to set a global. But here that's okay, so we use $?, which is the return value of the last executed function.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc link external library spank C Programming 6 08-08-2007 03:44 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C Programming 3 03-04-2005 02:46 PM
Possible circular definition with singleton objects techrolla C++ Programming 3 12-26-2004 10:46 AM
System drdroid C++ Programming 3 06-28-2002 10:12 PM
Need a suggestion on a school project.. Screwz Luse C Programming 5 11-27-2001 02:58 AM


All times are GMT -6. The time now is 05:45 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22