0

Because bash returns me that != is invalid, but it's a basic operator.

I try to use it in a three expression for loop

for (( c=1; ${!c}!=""; c++ )) do

wxi
  • 7
  • 4
  • What are you trying to achieve? `${c}` cannot be in any case empty because of `((c++))` at the end of every cycle... – ingroxd Feb 07 '19 at 14:15
  • @oguzismail it's a comparison, how can i exit the loop if i don't write a comparison ? ingroxd it's not ${c} but ${!c}. ${c} is the same as $c. ${!c} mean $valueOfC – wxi Feb 07 '19 at 14:25
  • 2
    If you are trying to loop over arguments, just `for i in "$@"; do` – tripleee Feb 07 '19 at 14:35
  • 4
    The C-style `for` loop is an arithmetic context. So your test is invalid, and there's no way to fix it. You must use another design. What do you want really? to loop on the arguments? in this case, use `for arg in "$@"; do ...` – gniourf_gniourf Feb 07 '19 at 14:35
  • @tripleee i try to verify if the argument is not empty. gniourf_gniourf what do you mean by arithmetic context ? What that context permit me to do ? And how can i repair in my case ? – wxi Feb 07 '19 at 14:46
  • https://www.gnu.org/software/bash/manual/html_node/Arithmetic-Expansion.html – tripleee Feb 07 '19 at 14:46
  • `for arg in "$@"; do if [ "$arg" ]; then echo not empty; else echo empty; done` – tripleee Feb 07 '19 at 14:47
  • @tripleee i don't understand the test `if [ "$arg" ]` – wxi Feb 07 '19 at 14:49
  • It tests if the string is non-empty. – tripleee Feb 07 '19 at 14:50
  • @tripleee `The evaluation is performed according to the rules listed below (see Shell Arithmetic).` Shell arithmetic `== != equality and inequality ` – wxi Feb 07 '19 at 14:52
  • @tripleee here he use it https://www.cyberciti.biz/faq/bash-for-loop/ – wxi Feb 07 '19 at 14:53
  • That's pretty much what gniourf_gniourf was trying to tell you. – tripleee Feb 07 '19 at 14:54
  • He telled me `So your test is invalid`. my test uses `!=`. you said `gnu.org/software/bash/manual/html_node/…`. The page say `The evaluation is performed according to the rules listed below (see Shell Arithmetic).`. Shell arithmetic say `== != equality and inequality`. I use `!=`. What's the point ? – wxi Feb 07 '19 at 14:57
  • 1
    The point is that `""` is not a valid integer value, which is the *only* thing you can work with inside an arithmetic context. – chepner Feb 07 '19 at 15:11
  • 1
    Also, the error message I see, at least, doesn't say that `!=` is invalid; it says that an 'operand [is] expected (err token is "!= ")'. This means it doesn't see a valid operand *following* `!=`. – chepner Feb 07 '19 at 15:16

2 Answers2

1

You can, but it's not really the usual way to check if there are empty arguments.

for((i=1; i<=$#; ++i)); do
   [[ "${!i}" ]] || echo "$0: Argument $i is empty" >&2
done

If you don't care about the index, just looping over the actual arguments is even simpler.

for arg in "$@"; do
   [[ "$arg" ]] || echo "$0: Empty argument" >&2
done

Notice how we print diagnostics to standard error, and take care to include the name of the script which generates the diagnostic in the message.

You very rarely need to check for empty arguments in practice, but perhaps this is a learning exercise.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Check if the argument is empty is just a step of the script. But i understand your way of doing it. Instead of verify how many arguments is filled, you tell directly to `for` to take the array of arguments so he can compute only the filled arguments, thanks ! – wxi Feb 07 '19 at 15:03
1

You can't do string comparison in a (()) because it's only arithmetic.

What you could do is something like this, where the string check is separate test after incrementing the counter var:

c=0
while (( c += 1 )) && [[ -n ${!c} ]]; do
    echo "$c ${!c}"
done
Olli K
  • 1,720
  • 1
  • 16
  • 17