Thread: Script File

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    16

    Unhappy Script File

    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

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM