Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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
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.
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
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.
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.