LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-20-2006, 10:14 PM   #1
Steve Spurr
LQ Newbie
 
Registered: Sep 2006
Posts: 3

Rep: Reputation: 0
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
 
Old 09-20-2006, 11:41 PM   #2
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
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
 
Old 09-21-2006, 05:34 AM   #3
Steve Spurr
LQ Newbie
 
Registered: Sep 2006
Posts: 3

Original Poster
Rep: Reputation: 0
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
 
Old 09-21-2006, 12:31 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,695

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
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.
 
Old 09-21-2006, 02:22 PM   #5
Steve Spurr
LQ Newbie
 
Registered: Sep 2006
Posts: 3

Original Poster
Rep: Reputation: 0
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
 
Old 09-21-2006, 09:38 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,695

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
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.
 
Old 09-22-2006, 08:19 AM   #7
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
syntax error near unexpected token `fi' Warmduvet Programming 21 10-08-2013 10:28 AM
error: unexpected token `newline' CarlosV Programming 4 05-17-2011 03:47 AM
syntax error near unexpected token `else' josedias Programming 3 09-11-2006 07:09 PM
Trouble with Bash -- syntax error near unexpected token `fi' anamericanjoe Programming 5 05-19-2006 02:59 PM
syntax error near unexpected token ` mattyspatty Programming 8 05-07-2006 05:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:11 PM.

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