#!/bin/bash
read -p "请输入第一个数字: " num1
if ! [[ "$num1" =~ ^[0-9]+$ ]]; then
echo "错误:数字非法输入!"
exit 1
fi
read -p "请输入第二个数字: " num2
if ! [[ "$num2" =~ ^[0-9]+$ ]]; then
echo "错误:数字非法输入!"
exit 1
fi
read -p "请输入运算符(+、-、*、/): " operator
if [[ "$operator" != "+" && "$operator" != "-" && "$operator" != "*" && "$operator" != "/" ]]; then
echo "错误:运算符非法输入!"
exit 1
fi
if [[ "$operator" == "/" && "$num2" -eq 0 ]]; then
echo "错误:除数不能为0。"
exit 1
fi
result=0
case "$operator" in
"+")
result=$((num1 + num2))
;;
"-")
result=$((num1 - num2))
;;
"*")
result=$((num1 * num2))
;;
"/")
result=$((num1 / num2))
;;
esac
echo "结果: $result"