During test of a file, binary operator expected error
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
During test of a file, binary operator expected error
Hi,
To remove some files automatically, I made a script but during Test the binary operator expected Error is being raised. Here is a piece of code from where error generating
---------------
Code:
if test -f Fri*.* ; then
printf "Friday files are existing and going to be removed \n"
rm Fri*.*
fi
-----------------------------
Error is on the first line, Here a point is to be noted if I test one file then there is no error for example if I write the first line like
Code:
if test -f FriDB.tar.gz ; then
printf "Friday files are existing and going to be removed \n"
rm Fri*.*
fi
Discussion regarding the same error has been done on this same forum but unfortunately I failed to follow them.
Regards.
Here is a piece of code from where error generating
---------------
Code:
if test -f Fri*.* ; then
printf "Friday files are existing and going to be removed \n"
rm Fri*.*
fi
That fails because the filename expansion results in multiple words being inserted in the test command. It will look like
Code:
test -f FriDB.tar.gz FriFile2.xx FriFile3.yy FriFile4.zz
The test command has no idea what to do with the rest of those arguments.
There are many ways to accomplish what you are trying to do. My own favorite is to store the result of the expansion in an array, then test the first element of the array.
Code:
declare -a Files
Files=(Fri*.*)
if [ "${Files[0]}" != "Fri*.*" ]; then
printf "Friday files are existing and going to be removed \n"
rm -f "${Files[@]}"
fi
Or, an alternative using nullglob to avoid repeating the pattern:
Code:
shopt -s nullglob
declare -a Files
Files=(Fri*.*)
if [ ${#Files[*]} -gt 0 ]; then
printf "Friday files are existing and going to be removed \n"
rm -f "${Files[@]}"
fi
Note: I've used "[ ... ]" in place of the test command. That is strictly a matter of preference. The two constructs are identical.
Hi,
Thanks schneidz & rknichols, Suppose I have two files which begins with "FriXXX" letters, I wanted that rm command should be run when both files exist, so I try with your provided solutions, can you explain it more using array I am feeling it easy to understand.
Regards.
Hi,
Thanks schneidz & rknichols, Suppose I have two files which begins with "FriXXX" letters, I wanted that rm command should be run when both files exist, so I try with your provided solutions, can you explain it more using array I am feeling it easy to understand.
Regards.
Hi,
Thanks schneidz & rknichols, Suppose I have two files which begins with "FriXXX" letters, I wanted that rm command should be run when both files exist, so I try with your provided solutions, can you explain it more using array I am feeling it easy to understand.
Regards.
The answer depends on whether you need to test for:
Two specific files, and there should be no other files that match the pattern,
Two specific files, and there could be others that match the pattern,
At least two files match the pattern, but there could be others,
Any two files that match the pattern, and there should be exactly two.
Somehow I don't feel like offering code for each of those cases. The suggestion from schneidz above should cover case 4. Which one seems to be of concern to you.
Thanks to all that try to expand my logic in scripting but I got my point through the following
FriFiles=$(ls Fri*.* | wc -l)
if test "$FriFiles" -ge 2 ; then
rm Fri*.*
fi
Regards.
That requirement is not that hard. And I doubt that it really is needed if I read the starting post. I guess that he just wants to delete all files with Fri in their name. Be it 1, two or a 100 thousand.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.