LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-04-2005, 06:17 AM   #1
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Rep: Reputation: 18
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.
 
Old 01-04-2005, 06:31 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
if prime_number $a
should work
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting: how to load a function from a file? frankie_DJ Programming 2 08-16-2005 02:16 PM
Bash scripting Gunslinger_ROL Programming 5 09-28-2004 11:37 AM
Mail Function in bash scripting chynna_v Programming 2 09-14-2004 04:36 AM
if-while-for-until-function-test...Help ziox Programming 2 08-09-2004 01:22 PM
HELP with BASH scripting atwah Linux - Newbie 6 09-09-2003 01:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration