Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > I need little help with "if test"

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-16-07, 05:07
sant sant is offline
Registered User
 
Join Date: Sep 2003
Posts: 17
I need little help with "if test"

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
Reply With Quote
  #2 (permalink)  
Old 11-16-07, 16:59
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Posts: 1,646
Once INV* is expanded, it may stand for multiple files. Those files will be separated by spaces and, thus, cause a syntax error if used with "test". You could iterate over all INV* files:
Code:
for f in "" /ftp/.../INV*; do if [ -n "$f" ]; then ftp prodinven > ... exit $? fi done

Another alternative may be to list all INV* files and then check if the listing is empty or not.
__________________
Knut Stolze
Data Warehousing on System z
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 11-18-07, 06:57
mike_bike_kite mike_bike_kite is online now
Registered User
 
Join Date: Jun 2007
Location: London
Posts: 955
Hey stolze - very nice code! - but you should explain why it's better than the original so sant can learn from it rather than just copying it in.

Mike
Reply With Quote
  #4 (permalink)  
Old 11-19-07, 07:19
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Posts: 1,646
My first sentence was hinting at the problem. INV* is expanded first, leading to something like
Code:
INV1 INV2 INVwhatever
If you inject that into your original statement, you get:
Code:
if test -r INV1 INV2 INVwhatever
When your shell parses that, it finds "test -r INV1" and then doesn't know what to do with INV2 and raises a syntax error. Thus, you have to accommodate for a variable number of files after expansion.
__________________
Knut Stolze
Data Warehousing on System z
IBM Germany Research & Development
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On