Bash Scripting - Using a function as test.
Hello,
For exercise, I've been trying to get this to work but something is wrong.. Can't find a way to fix it.
#!/bin/bash
# calculate first x prime numbers
NOARGS=65
if [ $# -ne 1 ]
then
echo "usage: `basename $0` $filename" >&2
exit $NOARGS
fi
# function to check if a number is prime
prime_number () {
echo "is $1 a prime number?"
return 0
}
x=$1
a=0
# continue searching until you have x prime numbers
until [ $a == $x ]
do
if [ ( prime_number $a ) ]
then
let "a=$a+1"
fi
done
exit 0
=> I always get the error:
lieven@cthulhu:~/testscripts$ ./prime.sh 2
./prime.sh: line 24: syntax error near unexpected token `prime_number'
./prime.sh: line 24: ` if [ ( prime_number $a ) ]'
How can I correctly use the function result as a test factor??
Is there really no other way to use a function's result except by $? to get the exit status?
Thanks!
Last edited by ldp; 01-04-2005 at 06:21 AM.
|