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 11-16-2007, 03:22 AM   #1
sancre
LQ Newbie
 
Registered: Nov 2007
Posts: 5

Rep: Reputation: 0
I need a little help with "if test -r"


Hello

I need make a script in Linux. It don`t work when I have several files in the directory (it return ./script: line 1: test: too many arguments), only when I have one file it work fine. I need make several actions when I have files, but I have this problem. Can anybody help me.

this is the script

if test -r /ftp/GESTOP/AI/entrada/INV*
then
ftp prodinven > /home/sant/inven/log/ftp.log 2>&1
exit 0
else
exit 1
exit 0
exit 1
fi


thank you
 
Old 11-16-2007, 03:31 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
when you put the star in, you expand it to all files matching it. so it becomes "if test -r filea fileb filec filed ...." which clearly makes no sense.... what do you expect a correct answer to that test to mean?
 
Old 11-16-2007, 03:33 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
./script: line 1: test: too many arguments
The error message is clear enough: when the shell expands the wild character "*" it produces a statement like
Code:
if test -r /ftp/GESTOP/AI/entrada/INV_file1 /ftp/GESTOP/AI/entrada/INV_file2 /ftp/GESTOP/AI/entrada/INV_file3 ...
but multiple arguments is not permitted for the test command. You have to loop through the files and test them one at a time:
Code:
for file in /ftp/GESTOP/AI/entrada/INV*
do
   test -r $file && <your_command_here>
done
Edit: Sorry Chris for redundancy. As always you're too fast in replying!

Last edited by colucix; 11-16-2007 at 03:35 AM.
 
Old 11-16-2007, 04:00 AM   #4
sancre
LQ Newbie
 
Registered: Nov 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Maybe I don,t explain correctly, because my english is very bad I need make things only when I have files INV* in this directory.
This is my complete script

if test -r `/ftp/GESTOP/AI/entrada/INV*`
then
/home/sant/inven/channame > /home/sant/inven/log/channame.log 2>&1
cd /home/sant/inven
/home/sant/inven/forjcl > /home/sant/inven/log/for.log 2>&1
ftp prodinven > /home/sant/inven/log/ftp.log 2>&1
rm /home/sant/inven/jclftp/INV.*
exit 0
else
exit 1
exit 0
exit 1
fi
 
Old 11-16-2007, 04:38 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
if [ "$(ls /where/ever/ | wc -l)" -gt 0 ]
then
...
fi
 
Old 11-16-2007, 06:05 AM   #6
sancre
LQ Newbie
 
Registered: Nov 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Good, you example is ok, is valid for me,thank you for you help
 
  


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
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
LXer: Ubuntu team gets "edgy" with latest test release LXer Syndicated Linux News 0 07-21-2006 01:54 PM
Apache Server only displaying "Test" page. Fedora Core 4 Tear Syden Linux - Newbie 2 03-17-2006 07:40 PM
any software to test proxies as the "proxy hunter" on windows platform? chamberlain Linux - Networking 0 05-10-2005 07:42 PM

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

All times are GMT -5. The time now is 08:46 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