LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 11-04-2004, 08:44 AM   #1
severian23
Member
 
Registered: Jan 2003
Location: Stone Mountain, GA
Distribution: Currently: RHEL/Centos 4.x-5.x servers, Ubuntu Desktop
Posts: 40

Rep: Reputation: 15
Bash, how do I test for *.txt files in a directory with IF?


Hello, I have a BASH script to process *.txt files in a directory.

The script works fine, but I would like to put the main module inside an IF statement to test for *.txt files. I thought this would be a simple:
if [ -e /data/files/*.txt ] ...
but that doesn't seem to work with a wildcard.

What would be a better method?

Thanks for your help!
 
Old 11-04-2004, 08:58 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524

Rep: Reputation: 93
Your qeustion is not entirely clear. What does the "main module" look like?
If you want to process *.txt files one-by-one:
Code:
for TXTFILE in *.txt ; do
    echo "Found *.txt file:  $TXTFILE" # do you "main module" here.
done
 
Old 11-04-2004, 09:04 AM   #3
lthaus
LQ Newbie
 
Registered: Nov 2004
Posts: 6

Rep: Reputation: 0
Hello..

Looks like you are were on the right path...

It does work for me in bash..
here is a clip..


[z535104@bmcsnt01 Servers]$ ls *.sh
test.sh
[z535104@bmcsnt01 Servers]$ if [ -e ./*.sh ]
> then
> echo true;
> else
> echo false;
> fi
true

[z535104@bmcsnt01 Servers]$ ls *.toad
ls: *.toad: No such file or directory
[z535104@bmcsnt01 Servers]$ if [ -e ./*.toad ]
> then
> echo true;
> else
> echo false;
> fi
false
 
Old 11-04-2004, 09:15 AM   #4
severian23
Member
 
Registered: Jan 2003
Location: Stone Mountain, GA
Distribution: Currently: RHEL/Centos 4.x-5.x servers, Ubuntu Desktop
Posts: 40

Original Poster
Rep: Reputation: 15
That if [ -e ./*.txt ] only works if there is ONE file in the directory; if there are two or more, the IF statement doesn't work.

In the module, I want to put the whole FOR statement inside an IF condition.

Okay, here is my script module:
# .txt file mover
txtprep_txtmover() {

for x in /data/138files/*.txt
do
fprefix=$(basename ${x} .txt) # gets file prefix
fname=${fprefix#*-*} # Get form name
cname=${fprefix%*-*} # Get company name

echo "Processing $fprefix" >> "$REPORTPATH/txtpreplog.txt"
# Convert company name to uppercase
cname=`echo $cname | tr a-z A-Z`
# Set form destination directory for Variform
DESTDIR="$BASEPATH/$cname/$fname"

#Test for directory, if not there, note in txtpreplog.txt
if [ -d "$DESTDIR" ]
then
# note exception data file for PS2PDF processing
echo "$DESTDIR $fprefix.txt" >> "$REPORTPATH/fileloc.txt"
mv $x $DESTDIR
else
echo "No Destination $DESTDIR" >> "$REPORTPATH/txtpreplog.txt"
fi
done
}
 
Old 11-04-2004, 09:19 AM   #5
lthaus
LQ Newbie
 
Registered: Nov 2004
Posts: 6

Rep: Reputation: 0
OK... but this does work...


[z535104@bmcsnt01 Servers]$ if [ `ls ./*.sh|wc -l` -gt 0 ]
> then
> echo true
> else
> echo false
> fi
true
[z535104@bmcsnt01 Servers]$ ls *.sh
1.sh 2.sh test.sh
[z535104@bmcsnt01 Servers]$

see if that will do for you..
v.
 
Old 11-04-2004, 09:29 AM   #6
severian23
Member
 
Registered: Jan 2003
Location: Stone Mountain, GA
Distribution: Currently: RHEL/Centos 4.x-5.x servers, Ubuntu Desktop
Posts: 40

Original Poster
Rep: Reputation: 15
That does work, can you tell me what you did in the IF condition?

I'd like to understand the logic of it.
 
Old 11-04-2004, 09:45 AM   #7
severian23
Member
 
Registered: Jan 2003
Location: Stone Mountain, GA
Distribution: Currently: RHEL/Centos 4.x-5.x servers, Ubuntu Desktop
Posts: 40

Original Poster
Rep: Reputation: 15
Okay, I think I understand now-

in " if [ `ls ./*.sh|wc -l` -gt 0 ]"

`ls ./*.sh|wc -l` is getting a directory list of each .sh file, wc -l is counting each line of the ls output, one line for each file. The output of the line altogether is an integer, 0 for no files, etc.

The IF condition is testing the number of files being greater than (-gt) zero.

Thanks for your help!
 
Old 11-04-2004, 09:47 AM   #8
lthaus
LQ Newbie
 
Registered: Nov 2004
Posts: 6

Rep: Reputation: 0
Quote:
Originally posted by severian23
That does work, can you tell me what you did in the IF condition?

I'd like to understand the logic of it.
Sure..

Basicly looking for any files that met the test condition.. with the ls command
Pipe that through Wordcount -lines.. that gives you an number.
That number -greaterthan 0 pulls a true

if [ `ls ./*.sh|wc -l` -gt 0 ] # list all sh files , then count them. back ticks force this to happen first... then you get your 'test' using numbers.
> then
> echo true
> else
> echo false
> fi
true
[z535104@bmcsnt01 Servers]$ ls *.sh
1.sh 2.sh test.sh
Hope that helps you out..
Sorry if I was too basic, I don't know what level of experiance you have...
If you have any other questions.. Just letme know..
.
v
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to access all files in a directory shinni Programming 5 04-24-2009 03:46 PM
Using Bash, Find script files in a directory or subdirectories within... ray5_83 Programming 4 10-10-2008 07:42 PM
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 12:13 AM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 07:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 04:16 AM


All times are GMT -5. The time now is 03:02 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration