0

I have this code that works perfectly on one Macbook but doesn't work on another Linux machine in bash. When run in the Linux machine, I get an error "conditional binary operator expected" "syntax error near ${testfile}'" when trying to check if the file in the variable is readable or a file.

for file in "${@:2}"
do
 if [[ ! -rf ${file} ]]

when I make it [ ! -rf "$file" ] I get: [: -rf: unary operator expected

4
  • -rf does indeed seem to work on my MacBook but I have no idea what it's supposed to do, and I get the same error when I try it on a random old Debian box where presumably I have an older version of Bash. What's the actual purpose of this script? Commented Jan 18, 2018 at 8:19
  • I'm guessing maybe it's a way to write [[ ! -r $file -a ! -f $file ]] which of course then should be portable back to Bash v2 and possibly beyond. Commented Jan 18, 2018 at 8:21
  • with this, I get told: syntax error near -a' @tripleee Commented Jan 18, 2018 at 8:25
  • My bad, that should be [[ ! -r $file && ! -f $file ]] but I'm still guessing here. Can you confirm that you are trying to check that the file exists and is a regular file and is readable? Commented Jan 18, 2018 at 9:18

2 Answers 2

1
[[ ! -r "${file}" && ! -f "${file}" ]]

or:

[ ! -r "${file}" -a ! -f "${file}" ]
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a difference between using -a and not using -a?
@novel you can't use -a with [[, you also can't use && with [. These solutions are equal.
Well except that more broadly [[ is in many ways different than [, and often more convenient (e.g. in that it's less demanding when it comes to quoting its arguments).
1

Just using the -r test will work

if [[ ! -r ${file} ]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.