我正在编写一个 bash 脚本来查找所有小于给定整数的素数。
这是代码:
#!/bin/bash
BADARGS=65
if [ -z $1 ]
then
echo "Usage:`basename $0` cannot have a null parameter."
exit $BADARGS
elif [ $1 -lt 2 ]
then
echo "Usage:`basename $0` should have the value 2 or more as the parameter."
exit $BADARGS
fi
TRUE=0
FALSE=
Primes(){
checkPrime=( $(factor $1) ) # this puts factors into array
if [ -z "${checkPrime[2]}" ] # third element is null
then
return $TRUE
else
return $FALSE
fi
}
printf "2 "
let "n = 3"
while [ $n -le $1 ]
do
if Primes $n
then
printf "$n "
fi
let "n += 2"
done
printf "\n"
# END
我正在使用 MacOS,当我执行脚本时收到此错误消息:
Jessicas-MacBook-Pro:Documents jessicalott$ ./primes.sh 10
2 ./primes.sh: line 16: factor: command not found
3 ./primes.sh: line 16: factor: command not found
5 ./primes.sh: line 16: factor: command not found
7 ./primes.sh: line 16: factor: command not found
9
我今天早上真的开始用 bash 写作,所以任何帮助都将不胜感激。我认为这可能与我没有使用 Linux 的事实有关,但我希望不是这样。