LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash, how do I test for *.txt files in a directory with IF? (https://www.linuxquestions.org/questions/programming-9/bash-how-do-i-test-for-%2A-txt-files-in-a-directory-with-if-251035/)

severian23 11-04-2004 08:44 AM

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!

Hko 11-04-2004 08:58 AM

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


lthaus 11-04-2004 09:04 AM

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

severian23 11-04-2004 09:15 AM

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
}

lthaus 11-04-2004 09:19 AM

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.

severian23 11-04-2004 09:29 AM

That does work, can you tell me what you did in the IF condition?

I'd like to understand the logic of it.

severian23 11-04-2004 09:45 AM

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!

lthaus 11-04-2004 09:47 AM

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


All times are GMT -5. The time now is 04:02 AM.