From a4d375af418ebcfc43d79d722fe89228b95b134d Mon Sep 17 00:00:00 2001 From: LouisFonda Date: Sun, 14 Jul 2024 14:59:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=A5=87=E5=81=B6?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/basic/13-function-even.sh | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tutorials/basic/13-function-even.sh diff --git a/tutorials/basic/13-function-even.sh b/tutorials/basic/13-function-even.sh new file mode 100644 index 0000000..e3f966f --- /dev/null +++ b/tutorials/basic/13-function-even.sh @@ -0,0 +1,22 @@ +#Program: +# 编写一个函数判断一个数的奇偶性 +#History: +# 2024/07/14 LouisFonda + +# 函数is_even +is_even () { + test $(expr $1 % 2) -eq 0 && return 0 || return 1 +} + +read -p "请输入一个整数:" num +# 判断这个数是否合法 +if [ "$num" != "" ] && echo "$num" | grep -qE "^-?[0-9]+";then + if is_even $num;then + echo "这个数是偶数" + else + echo "这个数是奇数" + fi +else + echo "输入的数值不和法,请输入整数" +fi +