0

I searched for if not commands, found a lot of answer for specific commands, but if I want something for any commands, how to do it? I'd like to know if there is a way for every condition to reverse it's result... not like knowing -eq and -ne...

what I want to do is a not of this

while [ -z ${!specificCommands[$d]} ]

and a not for this

for (( d=1; d<=$qtyOfParamInACommand; d++ ))

tried

for (( d=1; ![d<=$qtyOfParamInACommand]; d++ ))

but result are not as expected

so not just normal if... this is probably simple syntax. I will appreciate the answer specific for this command, but my point is to just reverse like in C with !

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Cher
  • 2,789
  • 10
  • 37
  • 64
  • 1
    possible duplicate of [Negate if condition in bash script](http://stackoverflow.com/questions/26475358/negate-if-condition-in-bash-script) – Jørgen R May 20 '15 at 13:39
  • does it also work the same for the while command? or in the middle of a for command? Example, for this for, how would I make a not of the condition in the middle, syntax correct? for (( d=1; d<=$qtyOfParamInACommand; d++ )) – Cher May 20 '15 at 13:50
  • Which language are we talking about? – Jørgen R May 20 '15 at 13:58
  • linux, I will add it in the title – Cher May 20 '15 at 13:59
  • did you try with a space? `! [d<=....]` – Chris Maes May 20 '15 at 14:02
  • thx a lot, I will mark the first answer as what solved the problem since it was mostly a stupid syntax error – Cher May 20 '15 at 14:05

1 Answers1

2

The opposite of:

while   [ -z ${!specificCommands[$d]} ]

is either of these:

while ! [ -z ${!specificCommands[$d]} ]
while   [ -n ${!specificCommands[$d]} ]

Note that you also need to quote the variable, or use double brackets where quotes aren't required. If you don't, you'll run into trouble when ${!specificCommands[$d]} is the empty string.

# better
while ! [ -z "${!specificCommands[$d]}" ]
while   [ -n "${!specificCommands[$d]}" ]

# best
while ! [[ -z ${!specificCommands[$d]} ]]
while   [[ -n ${!specificCommands[$d]} ]]

The negation of:

for ((d = 1; d <= qtyOfParamInACommand; d++))

is:

# note: broken
for ((d = 1; !(d <= qtyOfParamInACommand); d++))
for ((d = 1; d > qtyOfParamInACommand; d++))

Notice that the $ isn't necessary inside of ((...)).

Also, in actuality you wouldn't negate the condition, you'd flip the entire loop so it counts backwards. The loops I wrote above are actually broken. This is a more proper "opposite":

for ((d = qtyOfParamInACommand; d >= 1; d--))
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • thx! I just wonder, for the for, why can't the condition be between [] brackets? I would thing those [] brackets apply to enclose any kind of condition? – Cher May 20 '15 at 14:09
  • The brackets are a very specific command. `[` is just a (poorly chosen, IMO) synonym for the `test` command, which requires the last argument to be `]` so that it *looks* like syntax. The result is confusion for generations of shell programmers without any tangible benefit. – chepner May 20 '15 at 14:28
  • ah!! then I see... so if I get it, in the for, the test command is already there by default, so no need for [] – Cher May 20 '15 at 14:29