hi
i wanted to check if the file exist or not(multiple files)
DIRE=/home/V478
if [ -f "${DIRE}"/abc.* ];
then
echo "file present"
else
echo "file not present"
fi
But i am getting the error as
: [: unexpected operator/operand
hi
i wanted to check if the file exist or not(multiple files)
DIRE=/home/V478
if [ -f "${DIRE}"/abc.* ];
then
echo "file present"
else
echo "file not present"
fi
But i am getting the error as
: [: unexpected operator/operand
The syntax will not do.
if [ -f "${DIRE}"/abc.* ];
Will expand to
if [ -f /home/V478/abc.one /home/V478/abc.two /home/V478/abc.three ]
Before the test occurs if these files exist in /home/V478
What shell? bash has some nifty features which make this easier. Since you're using [ for tests, I'll assume sh.
You cannot have multiple arguments in test. You need to loop over them, even if you only care about one.
for f in "$DIRE"/abc.*; do
if [ -f "$f" ]; then
echo "some files exist"
else
echo "globbing failed."
fi
break
fi
in bash we can use some options so that globbing abc.* returns nothing if the files don't exist. using this and an array, we can simply count the elements returned:
#!/bin/bash
shopt -s nullglob
files=( "$DIR"/abc.* );
shopt -u nullglob # or keep it set..whatever.
if (( ${#files[@]} > 0 )); then
echo "files are present"
else
echo "file not present"
fi
See my file_exists tip
if file_exists "${DIRE}"/abc.*
then
echo "file present"
else
echo "file not present"
fi