我正在编写一个由各种命令组成的数学工具包。我想写的命令之一是找到3位数的因子。请将命令命名为“myfactors”。这是一个示例脚本:
$ myfactors abc
abc is not a number. Please enter a number
$ myfactor 72
72 is not a 3 digit number
$ myfactor 105
The factors are: 1 3 5 7 15 21 35 105
答案 0 :(得分:1)
请检查一下,我在Ubuntu中使用了因子GNU工具。
#!/bin/bash
num=$1
if [ "$num" -ge 100 ]
then
factor="`factor $num`"
echo "Factor of number $num is $factor"
else
echo "Enter number is not a 3 digit number"
fi
或者你我们限制性地接受3位数字
#!/bin/bash
num=$1
if [ "$num" -ge 100 ] && [ "$num" -lt 1000 ]
then
factor="`factor $num`"
echo "Factor of number $num is $factor"
else
echo "Enter number is not a 3 digit number"
fi