面试题答案
一键面试#!/bin/bash
# 定义计算商的函数
calculate_quotient() {
local dividend=$1
local divisor=$2
if (( divisor == 0 )); then
echo "错误:除数不能为零"
return 1
fi
local quotient=$(echo "scale=2; $dividend / $divisor" | bc)
echo "商为:$quotient"
return 0
}
# 主脚本部分
if [ $# -ne 2 ]; then
echo "用法:$0 <被除数> <除数>"
exit 1
fi
calculate_quotient "$1" "$2"
result=$?
if [ $result -eq 0 ]; then
echo "函数执行成功"
else
echo "函数执行失败"
fi