the question is:
use a script to take two numbers as arguments and output their sum using (i) bc, (ii) expr. include error-checking to test whether two arguments were entered.
My answer:
echo " The first number is"
read a
echo " The second number is"
read b
c=`echo "scale=2; $a + $b"|bc`
echo _____________________________________________
echo " The sum of two numbers using bc:$c "
echo
d=`expr $a + $b`
echo " The sum of two numbers using expr:$d "
echo _____________________________________________
I can't include the error checking in this program. how can i do it? please help!
Get the numbers from arguments $1
and $2
, not by prompting for input.
Check the number of arguments using $#
.
if [ $# -ne 2 ]
then
echo "Usage: $0 number1 number2" >&2 # write error message to stderr
exit 1
fi
a=$1
b=$2
# rest of your script goes here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.