How do I use round brackets in an 'if' condition

round brackets examples
square brackets in math
when to use square brackets
parentheses
can you use brackets in an essay
use of brackets in legal documents
curly brackets
when to use closed brackets

I'm creating a bash script and somewhere inside I have this code:

if [ $# -eq 2 -a (! -r "$2" -o ! -f "$2") ]; then
echo "rvf: bestand \""$2"\" bestaat niet of is onleesbaar" 1>&2
exit 2
fi

When i try to run this inside the script I get this error:

Syntax Error (bash -n):
rvf: line 14: syntax error in conditional expression
rvf: line 14: syntax error near `-a'
rvf: line 14: `if [[ $# -eq 2 -a (! -r "$2" -o ! -f "$2") ]]; then'

How does '()' work inside Bash scripts?

[[ doens't support -a, and it is considered obsolete and non portable for [. The correct solution using [ would be

if [ "$#" -eq 2 ] && { [ ! -r "$2" ] || [ ! -f "$2" ]; }; then

Grouping is done with { ... } rather than ( ... ) to avoid creating an unnecessary subshell.

Using [[ is simplifies to

if [[ "$#" -eq 2 && ( ! -r "$2" || ! -f "$2" ) ]]; then

Parentheses can be used for grouping inside [[; as a compound command, it uses separate parsing and evaluation rules, compared to an ordinary command like [ (which is just an alias for test, not syntax of any kind).

In either case, De Morgan's laws lets you refactor this to something a little simpler:

if [ "$#" -eq 2 ] && ! { [ -r "$2" ] && [ -f "$2" ] }; then

if [[ "$#" -eq 2 && ! ( -r "$2" && -f "$2" ) ]]; then

Using Parentheses (Round Brackets), Parentheses or round brackets are used add information to text (usually as an explanation or an afterthought). The use of periods/full stops with parentheses� Brackets are used to add information or a comment, but the different types are not interchangeable. Learn how to use brackets with clear examples.

The Difference between Round Brackets (Parentheses) and Square , Writers often question whether punctuation, particularly periods (full stops), should be inside or outside a closed round bracket. The quick answer is that the punctuation follows logic. In other words, if the round brackets enclose a full sentence, then the period stays inside with its sentence. Don't Overuse Round Brackets Using lots of brackets in your writing is usually a sign of bad sentence structure. Brackets also look a little informal in business correspondence. Luckily, the latter issue is easily solved. You do not have to use brackets all the time. You have a choice between round brackets, commas, and dashes.

Restructure your logic. "Not A or Not B" is just a more complicated way to say "not (A and B)".

In bash, try

if [[ "$#" == 2 ]] && ! [[ -r "$2" && -f "$2" ]]; then

Better,

if [[ "$#" == 2 && -r "$2" && -f "$2" ]]
then : all good code
else : nope code
fi

Even better,

if [[ "$#" == 2 ]]            # correct args
then if [[ -r "$2" ]]         # is readable
     then if [[ if -f "$2" ]] # is a file
          then echo "all good"
               : do all good stuff
          else echo "'$2' not a file"
               : do not a file stuff
          fi
     else echo "'$2' not readable"
          : do not readable stuff
     fi
else echo "Invalid number of args"
     : do wrong args stuff
fi

Clear error logging is worth breaking the pieces out.

Even better, imho -

if [[ "$#" != 2 ]]
then : wrong args stuff
fi
if [[ ! -r "$2" ]]
then : unreadable stuff
fi
if [[ ! -f "$2" ]] 
then : do not a file stuff
fi

: do all good stuff

Brackets ( ) [ ] | Writing, Brackets are symbols that we use to contain "extra information", or information that is Round brackets are basically used to add extra information to a sentence. Round brackets. Round brackets (also called parentheses, especially in American English) are mainly used to separate off information that isn’t essential to the meaning of the rest of the sentence. If you removed the bracketed material the sentence would still make perfectly good sense.

How to Use Brackets in Academic Writing: Some Common Rules , MLA suggests that parentheses (round brackets) be used; CMOS suggests using square brackets; and some style guides, such as APA, do not� Another reason to use brackets in quotes is to add a word, prefix, or suffix in order to fit the quote into your sentence. In the statement below, the ing is added so the sentence will flow. I tried to make dish mild enough for everyone, but my idea of "add[ing] Cayenne pepper to taste" was not the same as my friend's idea.

How to Use a Bracket in Grammar, Parentheses or Round Brackets ( ). The most commonly used bracket pair in English is the parentheses. This pair of round brackets is used when a writer wants to� Definition of Brackets in Punctuation. Brackets are the punctuation marks that are in the form of a pair and are majorly used to include certain written text within them besides the main text (written without brackets). Types of Brackets. The following are the types of brackets being used these days for different purposes:

How to use round brackets (parentheses) in fiction writing, Round brackets, or parentheses, crop up less frequently than many punctuation symbols in fiction writing, but that doesn't mean we must ban� Angle brackets are used in group theory and commutative algebra to specify group presentations, and to denote the subgroup or ideal generated by a collection of elements. Matrices. An explicitly given matrix is commonly written between large round or square brackets:

Comments
  • Are you using [ like your posted code or [[ like in your error message?
  • BTW, note that neither [ or [[ is "if syntax"; they're independent commands, which can be used without if being involved (and if can be used without either of them; if somecommand; then ... will check whether somecommand returned a zero or nonzero exit status, whatever that command may be).
  • As an additional aside, $2 should be in quotes to prevent its value from being subject to word-splitting and glob-expansion -- so you should just have echo "...$2..." instead of echo "..."$2"..."; if you want to prevent any characters directly concatenated on the right-hand side from being parsed as part of an attempted variable name, then echo "...${2}..." instead.
  • (Quoting $# also prevents problems with insane IFS values containing numeric characters when using [ rather than [[, so it's not a bad practice regardless).
  • True; I'll just drop the comment and imply that it is necessary :)