bash сравнить пустые строки ошибка

Чтобы выйти из виртуальной консоли, вам нужно ввести exit.

Ваша рабочая среда будет запущена в одном из виртуальных терминалов. На Ubuntu он находится на tty7. Чтобы добраться до него, нажмите Ctrl + Alt + F7.

0
задан 17 July 2018 в 18:32

6 ответов

Это не ошибка.

Вы не процитировали подстановку команд, поэтому она была расширена до нуля (вместо остальной пустой строки). Фактически выполняемые тесты:

[ = ]
[ != ]

С помощью одного аргумента [ ] проверяет, не является ли аргумент пустой строкой, и даже ни =, ни != не являются пустыми строками.

Используйте цитирование

(если вам не нужны эффекты некотируемого расширения, чего вы, скорее всего, не захотите)

[ "$(echo)" = "" ] # $? = 0
[ "$(echo)" != "" ] # $? = 1
3
ответ дан 18 July 2018 в 17:20

Когда вы выполняете [ $(echo) != $(echo) ] без цитирования, это эквивалентно [ != ]

Теперь, man bash говорит в SHELL BUILTIN COMMANDS, что поведение теста [ ... ] зависит от количества аргументов следующим образом:

          0 arguments
                 The expression is false.
          1 argument
                 The expression is true if and only if the argument is not null.

Итак, поскольку без цитирования != не является нулевым, тест TRUE.

Если вы приводите аргументы, вы получите ожидаемое поведение:

$ [ "$(echo)" != "$(echo)" ]
$ echo $?
1
$ [ "$(echo)" = "$(echo)" ]
$ echo $?
0

, потому что теперь вы даете ему 3 аргумента:

  3 arguments
         The following conditions are applied in the order listed.
         If the second argument is one of the  binary  conditional
         operators listed above under CONDITIONAL EXPRESSIONS, the
         result of the expression is the result of the binary test
         using  the first and third arguments as operands.  The -a
         and -o operators are  considered  binary  operators  when
         there  are  three arguments.  If the first argument is !,
         the value is the negation of the two-argument test  using
         the second and third arguments.  If the first argument is
         exactly ( and the third argument is exactly ), the result
         is  the one-argument test of the second argument.  Other‐
         wise, the expression is false.
3
ответ дан 18 July 2018 в 17:20

Когда вы выполняете [ $(echo) != $(echo) ] без цитирования, это эквивалентно [ != ]

Теперь, man bash говорит в SHELL BUILTIN COMMANDS, что поведение теста [ ... ] зависит от количества аргументов следующим образом:

          0 arguments
                 The expression is false.
          1 argument
                 The expression is true if and only if the argument is not null.

Итак, поскольку без цитирования != не является нулевым, тест TRUE.

Если вы приводите аргументы, вы получите ожидаемое поведение:

$ [ "$(echo)" != "$(echo)" ]
$ echo $?
1
$ [ "$(echo)" = "$(echo)" ]
$ echo $?
0

, потому что теперь вы даете ему 3 аргумента:

  3 arguments
         The following conditions are applied in the order listed.
         If the second argument is one of the  binary  conditional
         operators listed above under CONDITIONAL EXPRESSIONS, the
         result of the expression is the result of the binary test
         using  the first and third arguments as operands.  The -a
         and -o operators are  considered  binary  operators  when
         there  are  three arguments.  If the first argument is !,
         the value is the negation of the two-argument test  using
         the second and third arguments.  If the first argument is
         exactly ( and the third argument is exactly ), the result
         is  the one-argument test of the second argument.  Other‐
         wise, the expression is false.
3
ответ дан 20 July 2018 в 16:43

Когда вы выполняете [ $(echo) != $(echo) ] без цитирования, это эквивалентно [ != ]

Теперь, man bash говорит в SHELL BUILTIN COMMANDS, что поведение теста [ ... ] зависит от количества аргументов следующим образом:

          0 arguments
                 The expression is false.
          1 argument
                 The expression is true if and only if the argument is not null.

Итак, поскольку без цитирования != не является нулевым, тест TRUE.

Если вы приводите аргументы, вы получите ожидаемое поведение:

$ [ "$(echo)" != "$(echo)" ]
$ echo $?
1
$ [ "$(echo)" = "$(echo)" ]
$ echo $?
0

, потому что теперь вы даете ему 3 аргумента:

  3 arguments
         The following conditions are applied in the order listed.
         If the second argument is one of the  binary  conditional
         operators listed above under CONDITIONAL EXPRESSIONS, the
         result of the expression is the result of the binary test
         using  the first and third arguments as operands.  The -a
         and -o operators are  considered  binary  operators  when
         there  are  three arguments.  If the first argument is !,
         the value is the negation of the two-argument test  using
         the second and third arguments.  If the first argument is
         exactly ( and the third argument is exactly ), the result
         is  the one-argument test of the second argument.  Other‐
         wise, the expression is false.
3
ответ дан 20 July 2018 в 16:53

Когда вы выполняете [ $(echo) != $(echo) ] без цитирования, это эквивалентно [ != ]

Теперь, man bash говорит в SHELL BUILTIN COMMANDS, что поведение теста [ ... ] зависит от количества аргументов следующим образом:

          0 arguments
                 The expression is false.
          1 argument
                 The expression is true if and only if the argument is not null.

Итак, поскольку без цитирования != не является нулевым, тест TRUE.

Если вы приводите аргументы, вы получите ожидаемое поведение:

$ [ "$(echo)" != "$(echo)" ]
$ echo $?
1
$ [ "$(echo)" = "$(echo)" ]
$ echo $?
0

, потому что теперь вы даете ему 3 аргумента:

  3 arguments
         The following conditions are applied in the order listed.
         If the second argument is one of the  binary  conditional
         operators listed above under CONDITIONAL EXPRESSIONS, the
         result of the expression is the result of the binary test
         using  the first and third arguments as operands.  The -a
         and -o operators are  considered  binary  operators  when
         there  are  three arguments.  If the first argument is !,
         the value is the negation of the two-argument test  using
         the second and third arguments.  If the first argument is
         exactly ( and the third argument is exactly ), the result
         is  the one-argument test of the second argument.  Other‐
         wise, the expression is false.
3
ответ дан 23 July 2018 в 10:08

Когда вы выполняете [ $(echo) != $(echo) ] без цитирования, это эквивалентно [ != ]

Теперь, man bash говорит в SHELL BUILTIN COMMANDS, что поведение теста [ ... ] зависит от количества аргументов следующим образом:

          0 arguments
                 The expression is false.
          1 argument
                 The expression is true if and only if the argument is not null.

Итак, поскольку без цитирования != не является нулевым, тест TRUE.

Если вы приводите аргументы, вы получите ожидаемое поведение:

$ [ "$(echo)" != "$(echo)" ]
$ echo $?
1
$ [ "$(echo)" = "$(echo)" ]
$ echo $?
0

, потому что теперь вы даете ему 3 аргумента:

  3 arguments
         The following conditions are applied in the order listed.
         If the second argument is one of the  binary  conditional
         operators listed above under CONDITIONAL EXPRESSIONS, the
         result of the expression is the result of the binary test
         using  the first and third arguments as operands.  The -a
         and -o operators are  considered  binary  operators  when
         there  are  three arguments.  If the first argument is !,
         the value is the negation of the two-argument test  using
         the second and third arguments.  If the first argument is
         exactly ( and the third argument is exactly ), the result
         is  the one-argument test of the second argument.  Other‐
         wise, the expression is false.
3
ответ дан 31 July 2018 в 17:06

Другие вопросы по тегам:

Похожие вопросы: