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.