Reputation: 1165
The following script checks whether FILE is in the directory.
The FILE includes 'name.pdf'
How to verify, that files name*.pdf are also in the directory?
if [ -f "$FILE" ]; then # ----> how to add the condition that name*.pdf is also OK
echo "OK $f"
else
echo "!!! Not OK $f"
fi
Upvotes: 0
Views: 106
Reputation: 197
You could follow something like below
#!/bin/bash
if [[ $(find . -name "name-*.pdf") ]]; then
echo "File Exists"
else
echo "File Doesnt exists"
fi
Upvotes: -1
Reputation: 198
You can do this:
if [ -f name*.pdf ]; then
echo "OK $f"
else
echo "!!! Not OK $f"
fi
EDIT: as stated in the comments this method won't work if more than one file match, so I've found another way:
name=name*.pdf;
if compgen -G $name > /dev/null; then
echo "OK $f"
else
echo "!!! Not OK $f"
fi
Upvotes: 1
Reputation: 297
Does this do what you want?
# List the files, but stream any output to /dev/null
# We are only interested in the exit status
if ls name*.pdf >/dev/null 2>&1; then
echo File matching pattern exist
else
echo Files matching pattern do not exist
fi
This works because if
is checking the exit status of the ls
command. This is exactly how if [ 'some condition' ]
works, which is a shortcut for if test 'some condition'
: if
is checking the exit status of test
. ls
has an exit status of 0
if it finds files, otherwise it's 1
or 2
both of which if
will evaluate as false
.
If you want a more general solution, you can define the prefix and extension as variables:
prefix='name' #You can define the prefix and extension as variables
ext='pdf'
if $prefix*.$ext >/dev/null 2>&1; then
#the rest of the code is the same as earlier.
Upvotes: 1