LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 08-10-2015, 07:04 AM   #1
Masoom
LQ Newbie
 
Registered: Jul 2015
Posts: 12

Rep: Reputation: Disabled
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.
 
Old 08-10-2015, 08:48 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
what error are you getting ?

https://lmddgtfy.net/?q=linux%20test...ltiple%20files

Last edited by schneidz; 08-10-2015 at 09:00 AM.
 
Old 08-10-2015, 09:42 AM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213
Quote:
Originally Posted by Masoom View Post
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.

Last edited by rknichols; 08-10-2015 at 09:43 AM.
 
1 members found this post helpful.
Old 08-10-2015, 10:05 AM   #4
Masoom
LQ Newbie
 
Registered: Jul 2015
Posts: 12

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-10-2015, 10:06 AM   #5
Masoom
LQ Newbie
 
Registered: Jul 2015
Posts: 12

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-10-2015, 10:16 AM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
i think you will need to get creative like so:
Code:
declare -a Files
Files=(Fri*.*)
if [ ${#Files[@]} -eq 2 ]
then
    printf "Friday files are existing and going to be removed \n"
    rm -f "${Files[@]}"
fi

Last edited by schneidz; 08-10-2015 at 10:17 AM.
 
1 members found this post helpful.
Old 08-10-2015, 10:22 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Or if you want to use just basics, use a for loop with your test inside it
 
Old 08-10-2015, 11:21 AM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213
Quote:
Originally Posted by Masoom View Post
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:
  1. Two specific files, and there should be no other files that match the pattern,
  2. Two specific files, and there could be others that match the pattern,
  3. At least two files match the pattern, but there could be others,
  4. 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.
 
Old 08-12-2015, 01:35 AM   #9
Masoom
LQ Newbie
 
Registered: Jul 2015
Posts: 12

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-12-2015, 05:16 AM   #10
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Why not use find command. You can even delete things.

Quote:
find /your/log/file/path -iname "Fri*" -delete -depth 0
-iname is incase sensitive search
-delete deletes
-depth 0 takes care that only files in the given directory are affected and non in lower subdirs.
 
Old 08-12-2015, 09:20 AM   #11
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213
Quote:
Originally Posted by zhjim View Post
Why not use find command. You can even delete things.
It's a bit difficult to require that two matching file be present and to get the message printed, and printed just once.
 
Old 08-13-2015, 03:40 AM   #12
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
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.
 
Old 08-13-2015, 03:44 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,902

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
what about:
Code:
rm Fri*
??
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unary operator expected error greenpool Linux - Newbie 1 01-31-2012 07:36 PM
Binary Operator Expected error in Shell Script mangatmodi Programming 9 10-25-2009 01:36 AM
error: unary operator expected ?? Lynda_M Programming 3 11-29-2008 08:03 PM
unary operator expected error! Lynda_M Programming 3 11-29-2008 12:04 PM
Binary operator expected - error mike9287 Linux - Newbie 9 07-17-2006 08:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration