LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   syntax error for unexpected token `(' (http://www.linuxquestions.org/questions/linux-newbie-8/syntax-error-for-unexpected-token-%60-485505/)

Steve Spurr 09-20-2006 10:14 PM

syntax error for unexpected token `('
 
I'm really new and getting rapidly frustrated...Syntax errors seem to be pretty common.
Here is my code, which is pretty basic
I want to read multiple file names from the console, and check each one to see if it is a valid file name. I've started by trying to write a basic test for one file name, just to see if I can open it. Later I'll get around to reading it, but baby steps first.

while true
do
echo -n Please enter file names to process:
read files
file=`echo ${files} | awk 'BEGIN{FS=" "} {print$1}'`
if [ `filehandle= fopen(${file}, r)` == NULL ]
then
echo Bad file
else
echo Good file
exit 0
fi
done
My file is called File_tester
The error I get is;
File_tester: command substitution: line 18: syntax error near unexpected token `('
File_tester: command substitution: line 18: `filehandle= fopen(${file}, r)'
File_tester: line 18: [: -eq: unary operator expected

I know the problem is with my if test, but what?
Hopefully someone can help
Thanks

bartonski 09-20-2006 11:41 PM

Correct me if I'm wrong, but fopen() isn't executable by the shell.

On my FC4 installation:

Quote:

$ type fopen
-bash: type: fopen: not found
Also, man -a fopen only yields the man page for the C function.

It looks like you are testing to see if the file is readable; to do this, you would use the bash '-r' test:

Quote:

while true
do
echo -n Please enter file names to process:
read files
file=`echo ${files} | awk 'BEGIN{FS=" "} {print$1}'`
if [ ! -r ${file} ]
then
echo Bad file
else
echo Good file
exit 0
fi
done

Steve Spurr 09-21-2006 05:34 AM

syntax error near unexpected token `('
 
Changing the file to:

while true
do
echo -n Please enter file names to process:
read files
file=`echo ${files} | awk 'BEGIN{FS=" "} {print$1}'`
if [ ! -r ${file} ]
then
echo Bad file
else
echo Good file
exit 0
fi
done


did get rid of my syntax error. Thanks However, if I give it an existing file name, it tells me bad file, but if I give it a directory, such as " /etc ", it tells me good file. Obviously, still not working the way I want.
Hopefully you have some more good advice
Thanks

ntubski 09-21-2006 12:31 PM

Try giving the full path of the file. eg /etc/fstab not fstab. If you don't give the full path your script will only find files that are in the same directory as the script.

Steve Spurr 09-21-2006 02:22 PM

syntax error near unexpected token `('
 
while true
do
echo -n Please enter file names to process:
read files
file=`echo ${files} | awk 'BEGIN{FS=" "} {print$1}'`
if [ ! -r ${file} ]
then
echo Bad file
else
echo Good file
exit 0
fi
done

did get rid of my syntax error. Thanks However, if I give it an existing file name, it tells me bad file, but if I give it a directory, such as " /etc ", it tells me good file. Obviously, still not working the way I want.
Hopefully you have some more good advice
Thanks

Quote:

Originally Posted by ntubski
Try giving the full path of the file. eg /etc/fstab not fstab. If you don't give the full path your script will only find files that are in the same directory as the script.

That did it. Thanks.

I know this is not part of the original post, but how do I read in multiple args from the shell. In other words, I want to be able to enter x number of file names, then process each one in sequence. I don't know in advance how many will be entered
Thanks

ntubski 09-21-2006 09:38 PM

You can use the shift command. It shifts all the arguments down by one. So read $1, do stuff with it, then shift so whatever is in $2 goes into $1, repeat until $1 is empty.

bartonski 09-22-2006 08:19 AM

Quote:

However, if I give it an existing file name, it tells me bad file, but if I give it a directory, such as " /etc ", it tells me good file. Obviously, still not working the way I want.
Hopefully you have some more good advice
Thanks
Here's the mechanics behind why that doesn't work:

from man bash
Quote:

-r file
True if file exists and is readable.
so all you're saying, when you give the -r test a directory is that the directory exists, and is readable. It's not seeing it as a directory. The -d test will test to see if the file is a directory. If you wanted to, you could test to see if the name you are giving is a directory, then if it is, look through its contents file by file.

Take a look at man bash, and search for CONDITIONAL EXPRESSIONS. This will give all of the available file tests. There are dozens. I actually reccomend reading the whole bash man page. (caveat: I've never actually done so myself, but I know that there's a bunch of good stuff in there)


All times are GMT -5. The time now is 10:37 PM.