See the following error:
$ test ! command -v git
bash: test: -v: binary operator expected
But both the following two methods work:
$ test ! 'command -v git'; echo $?
1
$ test ! "command -v git"; echo $?
1
See the following error:
$ test ! command -v git
bash: test: -v: binary operator expected
But both the following two methods work:
$ test ! 'command -v git'; echo $?
1
$ test ! "command -v git"; echo $?
1
Any hints for this behavior?
On Monday, January 24, 2022 at 9:48:24 AM UTC+8, Keith Thompson wrote:
"hongy...@gmail.com" <hongy...@gmail.com> writes:
See the following error:
$ test ! command -v gitAs expected, since `test` has no `-v` operator.
bash: test: -v: binary operator expected
But both the following two methods work:
$ test ! 'command -v git'; echo $?As expected. Both could be simplified to `echo 1`.
1
$ test ! "command -v git"; echo $?
1
They "work" in the sense that they do not print an error message. BothThank you for pointing this out:
test whether the specified quoted string is empty. You could replace "command" and "git" by "foo" and "bar" respectively, with the same
results.
$ test ! "foo -v bar"; echo $?
1
$ test ! 'foo -v bar'; echo $?
1
I want to check if the `git` command exists on the system. And also noticed the following usage here [1]:Any hints for this behavior?Yes. State what you're trying to do, then figure out what command you
need to use to do that.
$ if test ! `command -v git` ; then echo "git not found. Please make sure git is installed."; fi
But it seems that the command substitution, i.e., "``" or "$()" shell structure is not designed to just only obtain the exit code. So, I think the above usage is at least not idiomatic and would like to improve it.
"hongy...@gmail.com" <hongy...@gmail.com> writes:
See the following error:
$ test ! command -v gitAs expected, since `test` has no `-v` operator.
bash: test: -v: binary operator expected
But both the following two methods work:
$ test ! 'command -v git'; echo $?As expected. Both could be simplified to `echo 1`.
1
$ test ! "command -v git"; echo $?
1
They "work" in the sense that they do not print an error message. Both
test whether the specified quoted string is empty. You could replace "command" and "git" by "foo" and "bar" respectively, with the same
results.
Any hints for this behavior?Yes. State what you're trying to do, then figure out what command you
need to use to do that.
Also study the documentation for the `test`
command. And next time, please do all this before posting.
On Monday, January 24, 2022 at 10:15:15 AM UTC+8, hongy...@gmail.com wrote:
On Monday, January 24, 2022 at 9:48:24 AM UTC+8, Keith Thompson wrote:
"hongy...@gmail.com" <hongy...@gmail.com> writes:Thank you for pointing this out:
See the following error:As expected, since `test` has no `-v` operator.
$ test ! command -v git
bash: test: -v: binary operator expected
But both the following two methods work:As expected. Both could be simplified to `echo 1`.
$ test ! 'command -v git'; echo $?
1
$ test ! "command -v git"; echo $?
1
They "work" in the sense that they do not print an error message. Both
test whether the specified quoted string is empty. You could replace
"command" and "git" by "foo" and "bar" respectively, with the same
results.
$ test ! "foo -v bar"; echo $?
1
$ test ! 'foo -v bar'; echo $?
1
I want to check if the `git` command exists on the system. And also noticed the following usage here [1]:Any hints for this behavior?Yes. State what you're trying to do, then figure out what command you
need to use to do that.
$ if test ! `command -v git` ; then echo "git not found. Please make sure git is installed."; fi
But it seems that the command substitution, i.e., "``" or "$()" shell structure is not designed to just only obtain the exit code. So, I think the above usage is at least not idiomatic and would like to improve it.
To summarize, which of the following method is preferred?
$ test ! `command -v git` ; echo $?
1
$ ! (command -v git >/dev/null 2>&1); echo $?
1
HZ
If I want to simply check for existence of a command I'd use
'whence',
or 'type', or if you want 'command' then I'd write
command -v git ; echo $?
or in contexts of tests either of
command -v git || echo git non-existing
if command -v git 2>/dev/null
then echo git existing
else echo git non-existing
fi
if ! command -v git
then echo git non-existing
fi
Redirect errors as desired (one sample provided above).
$ test ! `command -v git` ; echo $?
1
$ ! (command -v git >/dev/null 2>&1); echo $?I would avoid unnecessary stuff like subshell-expansion, and
1
subshells, and old `...` syntax, and test.
Why do you use that in the first place? (Just a rhetorical question. See Keith's
suggestion for a procedure to develop appropriate shell code;
the core is: "figure out what command you _need_ to use".)
"hongy...@gmail.com" <hongyi.zhao@gmail.com> writes:
See the following error:
$ test ! command -v git
bash: test: -v: binary operator expected
As expected, since `test` has no `-v` operator.
On Monday, January 24, 2022 at 2:59:49 PM UTC+8, Janis Papanagnou wrote:
If I want to simply check for existence of a command I'd use
'whence',
$ whence
whence: command not found
"hongy...@gmail.com" <hongyi.zhao@gmail.com> writes:
On Monday, January 24, 2022 at 2:59:49 PM UTC+8, Janis Papanagnou wrote:
If I want to simply check for existence of a command I'd use
'whence',
$ whence
whence: command not found
"whence" is a builtin command in zsh.
On 2022-01-23 20:48, Keith Thompson wrote:
"hongy...@gmail.com" <hongy...@gmail.com> writes:
See the following error:
$ test ! command -v git
bash: test: -v: binary operator expected
As expected, since `test` has no `-v` operator.That depends on which 'test' you're using. The bash built-in 'test'
(which the OP appears to be using) does have a (unary) -v operator.
From 'help test' in bash:
-v VAR True if the shell variable VAR is set.
For which the OP's syntax is (also) obviously wrong.
On Mon, 24 Jan 2022 20:26:36 -0500, hongy...@gmail.com <hongy...@gmail.com> wrote:
On Tuesday, January 25, 2022 at 12:04:43 AM UTC+8, John-Paul Stewart wrote:
On 2022-01-23 20:48, Keith Thompson wrote:
"hongy...@gmail.com" <hongy...@gmail.com> writes:That depends on which 'test' you're using. The bash built-in 'test'
See the following error:
$ test ! command -v git
bash: test: -v: binary operator expected
As expected, since `test` has no `-v` operator.
(which the OP appears to be using) does have a (unary) -v operator.
From 'help test' in bash:
-v VAR True if the shell variable VAR is set.
For which the OP's syntax is (also) obviously wrong.
I'm not using the -v operator 'test' command, instead, here is the -v parameter of 'command':
$ help command
-v print a description of COMMAND similar to the `type' builtin
So, I really don't understand why you give the comments above.In order to avoid having the -v option be interpreted as an option passed to the
test command, the "command -v git" must be passed as a single string to the test
command. That's done by enclosing command -v git in quotes.
test ! "command -v git"
On Tuesday, January 25, 2022 at 12:04:43 AM UTC+8, John-Paul Stewart wrote:
On 2022-01-23 20:48, Keith Thompson wrote:
"hongy...@gmail.com" <hongy...@gmail.com> writes:That depends on which 'test' you're using. The bash built-in 'test'
See the following error:
$ test ! command -v git
bash: test: -v: binary operator expected
As expected, since `test` has no `-v` operator.
(which the OP appears to be using) does have a (unary) -v operator.
From 'help test' in bash:
-v VAR True if the shell variable VAR is set.
For which the OP's syntax is (also) obviously wrong.
I'm not using the -v operator 'test' command, instead, here is the -v parameter of 'command':
$ help command
-v print a description of COMMAND similar to the `type' builtin
So, I really don't understand why you give the comments above.
On Mon, 24 Jan 2022 20:26:36 -0500, hongy...@gmail.com <hongyi.zhao@gmail.com> wrote:
On Tuesday, January 25, 2022 at 12:04:43 AM UTC+8, John-Paul Stewart wrote: >>> On 2022-01-23 20:48, Keith Thompson wrote:
"hongy...@gmail.com" <hongy...@gmail.com> writes:That depends on which 'test' you're using. The bash built-in 'test'
See the following error:
$ test ! command -v git
bash: test: -v: binary operator expected
As expected, since `test` has no `-v` operator.
(which the OP appears to be using) does have a (unary) -v operator.
From 'help test' in bash:
-v VAR True if the shell variable VAR is set.
For which the OP's syntax is (also) obviously wrong.
I'm not using the -v operator 'test' command, instead, here is the -v parameter of 'command':
$ help command
-v print a description of COMMAND similar to the `type' builtin
So, I really don't understand why you give the comments above.
In order to avoid having the -v option be interpreted as an option passed to the
test command, the "command -v git" must be passed as a single string to the test
command. That's done by enclosing command -v git in quotes.
test ! "command -v git"
On Tuesday, January 25, 2022 at 12:04:43 AM UTC+8, John-Paul Stewart wrote:
On 2022-01-23 20:48, Keith Thompson wrote:
"hongy...@gmail.com" <hongy...@gmail.com> writes:That depends on which 'test' you're using. The bash built-in 'test'
See the following error:
$ test ! command -v git
bash: test: -v: binary operator expected
As expected, since `test` has no `-v` operator.
(which the OP appears to be using) does have a (unary) -v operator.
From 'help test' in bash:
-v VAR True if the shell variable VAR is set.
For which the OP's syntax is (also) obviously wrong.
I'm not using the -v operator 'test' command, instead, here is the -v parameter of 'command':
From where can I find the specification that running test on an empty
string will return 1, as denoted by the following:
$ test ''; echo $? 1
"David W. Hodgins" <dwho...@nomail.afraid.org> writes:
On Mon, 24 Jan 2022 20:26:36 -0500, hongy...@gmail.com <hongy...@gmail.com> wrote:
On Tuesday, January 25, 2022 at 12:04:43 AM UTC+8, John-Paul Stewart wrote:
On 2022-01-23 20:48, Keith Thompson wrote:
"hongy...@gmail.com" <hongy...@gmail.com> writes:That depends on which 'test' you're using. The bash built-in 'test'
See the following error:
$ test ! command -v git
bash: test: -v: binary operator expected
As expected, since `test` has no `-v` operator.
(which the OP appears to be using) does have a (unary) -v operator.
From 'help test' in bash:
-v VAR True if the shell variable VAR is set.
For which the OP's syntax is (also) obviously wrong.
I'm not using the -v operator 'test' command, instead, here is the -v parameter of 'command':
$ help command
-v print a description of COMMAND similar to the `type' builtin
So, I really don't understand why you give the comments above.
In order to avoid having the -v option be interpreted as an option passed to theThat would be valid but useless. It checks whether the string
test command, the "command -v git" must be passed as a single string to the test
command. That's done by enclosing command -v git in quotes.
test ! "command -v git"
"command -v git" is empty.
From where can I find the specification that running test on
an empty string will return 1, as denoted by the following:
$ test ''; echo $?
1
On Tuesday, 25 January 2022 at 07:31:01 UTC, hongy...@gmail.com wrote:
<snip>
From where can I find the specification that running test on
an empty string will return 1, as denoted by the following:
$ test ''; echo $?RTFM...
1
If you're using bash, enter the command
man bash
Search for CONDITIONAL EXPRESSIONS
Read the opening paragraphs (*PLEASE*) and then scroll down
through all the test / [ / [[ options until you get to this:
string
-n string
True if the length of string is non-zero.
And there you have it.
For the non-builtin 'test' executable, enter
man test
or
man [
Here, you will find
-n STRING
the length of STRING is nonzero
STRING equivalent to -n STRING
On Tuesday, January 25, 2022 at 12:04:43 AM UTC+8, John-Paul Stewart wrote:
On 2022-01-23 20:48, Keith Thompson wrote:
"hongy...@gmail.com" <hongy...@gmail.com> writes:That depends on which 'test' you're using. The bash built-in 'test'
See the following error:
$ test ! command -v git
bash: test: -v: binary operator expected
As expected, since `test` has no `-v` operator.
(which the OP appears to be using) does have a (unary) -v operator.
From 'help test' in bash:
-v VAR True if the shell variable VAR is set.
For which the OP's syntax is (also) obviously wrong.
I'm not using the -v operator 'test' command, instead, here is the -v parameter of 'command':
On Mon, 24 Jan 2022 17:26:36 -0800, hongy...@gmail.com wrote:[snip]
On Tuesday, January 25, 2022 at 12:04:43 AM UTC+8, John-Paul Stewart wrote: >>> On 2022-01-23 20:48, Keith Thompson wrote:
"hongy...@gmail.com" <hongy...@gmail.com> writes:That depends on which 'test' you're using. The bash built-in 'test'
See the following error:
$ test ! command -v git
bash: test: -v: binary operator expected
As expected, since `test` has no `-v` operator.
(which the OP appears to be using) does have a (unary) -v operator.
From 'help test' in bash:
-v VAR True if the shell variable VAR is set.
For which the OP's syntax is (also) obviously wrong.
I'm not using the -v operator 'test' command, instead, here is the -v
parameter of 'command':
No, you are NOT using the -v parameter of 'command'. That may have been
your intention, but that's NOT what you did.
[1] by issuing the error as "bash: test:", bash has confirmed
that you invoked the bash builtin test. Had you used the test(1)
binary, bash would not have inserted the "bash: " into the error
message, and instead issued
test: '-v': binary operator expected
In addition, the error message is explicitly saying that it was 'test'
that was (mis-)interpreting the -v operator, rather than it being parsed
as an option to 'command'.
The mention of 'test' in the error message should have been a big clue
to the OP that the line was not being interpreted the way he expected.
On Mon, 24 Jan 2022 17:26:36 -0800, hongy...@gmail.com wrote:
On Tuesday, January 25, 2022 at 12:04:43 AM UTC+8, John-Paul Stewart wrote:
On 2022-01-23 20:48, Keith Thompson wrote:
"hongy...@gmail.com" <hongy...@gmail.com> writes:That depends on which 'test' you're using. The bash built-in 'test'
See the following error:
$ test ! command -v git
bash: test: -v: binary operator expected
As expected, since `test` has no `-v` operator.
(which the OP appears to be using) does have a (unary) -v operator.
From 'help test' in bash:
-v VAR True if the shell variable VAR is set.
For which the OP's syntax is (also) obviously wrong.
I'm not using the -v operator 'test' command, instead, here is the -v parameter of 'command':No, you are NOT using the -v parameter of 'command'. That may have been
your intention, but that's NOT what you did.
The builtin test command treats it's parameters as an expression.
In your case
test ! command -v git
you gave test four parameters:
a '!', instructing test to /complement/ the results of the
following expression
a string 'command', which would be the left hand side of
a "binary" expression
a string '-v', which would be the "operator" of a "binary"
expression, and
a string 'git', which would be the right hand side of a
"binary" expression
By issuing the error message
bash: test: -v: binary operator expected
bash tells you that the builtin[1] test encountered an unexpected
and invalid '-v' operator (instead of one of the "known" binary
operators) between the 'command' string and the 'git' string.
[1] by issuing the error as "bash: test:", bash has confirmed
that you invoked the bash builtin test. Had you used the test(1)
binary, bash would not have inserted the "bash: " into the error
message, and instead issued
test: '-v': binary operator expected
On Tuesday, January 25, 2022 at 11:39:33 PM UTC+8, Lew Pitcher wrote:
On Mon, 24 Jan 2022 17:26:36 -0800, hongy...@gmail.com wrote:
On Tuesday, January 25, 2022 at 12:04:43 AM UTC+8, John-Paul Stewart wrote: >>>> On 2022-01-23 20:48, Keith Thompson wrote:No, you are NOT using the -v parameter of 'command'. That may have been
"hongy...@gmail.com" <hongy...@gmail.com> writes:That depends on which 'test' you're using. The bash built-in 'test'
See the following error:
$ test ! command -v git
bash: test: -v: binary operator expected
As expected, since `test` has no `-v` operator.
(which the OP appears to be using) does have a (unary) -v operator.
From 'help test' in bash:
-v VAR True if the shell variable VAR is set.
For which the OP's syntax is (also) obviously wrong.
I'm not using the -v operator 'test' command, instead, here is the -v parameter of 'command':
your intention, but that's NOT what you did.
The builtin test command treats it's parameters as an expression.
In your case
test ! command -v git
you gave test four parameters:
a '!', instructing test to /complement/ the results of the
following expression
a string 'command', which would be the left hand side of
a "binary" expression
a string '-v', which would be the "operator" of a "binary"
expression, and
a string 'git', which would be the right hand side of a
"binary" expression
By issuing the error message
bash: test: -v: binary operator expected
bash tells you that the builtin[1] test encountered an unexpected
and invalid '-v' operator (instead of one of the "known" binary
operators) between the 'command' string and the 'git' string.
Thank you for your in-depth analysis. I checked and confirmed with the following tests:
$ ( set -x ; test ! command -v git )
+ test '!' command -v git
bash: test: -v: binary operator expected
$ ( set -x ; test ! 'command -v git' )
+ test '!' 'command -v git'
$ ( set -x ; test ! "command -v git" )
+ test '!' 'command -v git'
[1] by issuing the error as "bash: test:", bash has confirmed
that you invoked the bash builtin test. Had you used the test(1)
binary, bash would not have inserted the "bash: " into the error
message, and instead issued
test: '-v': binary operator expected
I confirmed your above conclusion:
$ /bin/test command -v git
/bin/test: ā-vā: binary operator expected
HZ
I know I'm stupid to ask this,
but why do you insist on 'test'ing a linux/unix command?
Surely:
command -v git && echo "git installed" || echo "git not installed"
would suffice.
On Wednesday, January 26, 2022 at 7:56:30 PM UTC+8, Chris Elvidge wrote:
I know I'm stupid to ask this,
Absolutely no. Everyone does things according to their own logic, which is not so stupid, at least from their own point of view.
but why do you insist on 'test'ing a linux/unix command?
Just in order to verify the above-mentioned working mechanism of test.
Surely:
command -v git && echo "git installed" || echo "git not installed"
would suffice.
Agreed.
On 26/01/2022 12:51, hongy...@gmail.com wrote:
Just in order to verify the above-mentioned working mechanism of test.But you didn't. You verified that you had the command structure wrong.
Try: test $(command -v git)
Or: C=$(command -v git); test -v C
At least then you know which -v is implemented when.
On Monday, January 24, 2022 at 9:48:24 AM UTC+8, Keith Thompson wrote:
"hongy...@gmail.com" <hongy...@gmail.com> writes:
See the following error:As expected, since `test` has no `-v` operator.
$ test ! command -v git bash: test: -v: binary operator expected
But both the following two methods work:As expected. Both could be simplified to `echo 1`.
$ test ! 'command -v git'; echo $?
1
$ test ! "command -v git"; echo $?
1
They "work" in the sense that they do not print an error message. Both
test whether the specified quoted string is empty. You could replace
"command" and "git" by "foo" and "bar" respectively, with the same
results.
Thank you for pointing this out:
$ test ! "foo -v bar"; echo $?
1
$ test ! 'foo -v bar'; echo $?
1
Any hints for this behavior?Yes. State what you're trying to do, then figure out what command you
need to use to do that.
I want to check if the `git` command exists on the system. And also
noticed the following usage here [1]:
$ if test ! `command -v git` ; then echo "git not found. Please make
sure git is installed."; fi
But it seems that the command substitution, i.e., "``" or "$()" shell structure is not designed to just only obtain the exit code. So, I think
the above usage is at least not idiomatic and would like to improve it.
[1] https://gitlab.com/QEF/q-e/-/merge_requests/1725#note_818943793
Also study the documentation for the `test`
command. And next time, please do all this before posting.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 498 |
Nodes: | 16 (2 / 14) |
Uptime: | 22:46:04 |
Calls: | 9,828 |
Calls today: | 7 |
Files: | 13,761 |
Messages: | 6,191,777 |