Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use portable "command -v" to detect installed programs #765

Merged
merged 1 commit into from
Sep 3, 2020
Merged

Use portable "command -v" to detect installed programs #765

merged 1 commit into from
Sep 3, 2020

Conversation

eli-schwartz
Copy link
Contributor

The "which" utility is not guaranteed to be installed either, and if it is, its behavior is not portable either.

Conversely, the "command -v" shell builtin is required to exist in all POSIX 2008 compliant shells, and is thus guaranteed to work everywhere.

Examples of open-source shells likely to be installed as /bin/sh on Linux, which implement the 11-year-old standard: ash, bash, busybox, dash, ksh, mksh and zsh.

See:
https://mywiki.wooledge.org/BashFAQ/081
https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then/85250#85250

The "which" utility is not guaranteed to be installed either, and if it
is, its behavior is not portable either.

Conversely, the "command -v" shell builtin is required to exist in all
POSIX 2008 compliant shells, and is thus guaranteed to work everywhere.

Examples of open-source shells likely to be installed as /bin/sh on
Linux, which implement the 11-year-old standard: ash, bash, busybox,
dash, ksh, mksh and zsh.

See:
https://mywiki.wooledge.org/BashFAQ/081
https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then/85250#85250
@eli-schwartz
Copy link
Contributor Author

Note: I haven't the foggiest clue why this is executing a system() command line in a shell. Does ruby not have a standard library implementation of e.g. python's shutil.which() that natively iterates over the $PATH and stats for a matching file?

@eli-schwartz
Copy link
Contributor Author

This will simply think nothing ever exists by default, since "which" doesn't exist, but for extra bonus breakage if you do have a version of which, but one that returns 0 even when the command is not found (while emitting human-readable messages), you get this highly amusing Linux error:

$ travis report
Traceback (most recent call last):
	18: from /usr/bin/travis:23:in `<main>'
	17: from /usr/bin/travis:23:in `load'
	16: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/bin/travis:18:in `<top (required)>'
	15: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/cli.rb:62:in `run'
	14: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/cli/parser.rb:36:in `new'
	13: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/cli/parser.rb:36:in `new'
	12: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/cli/api_command.rb:43:in `initialize'
	11: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client.rb:34:in `new'
	10: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client.rb:34:in `new'
	 9: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client/session.rb:36:in `initialize'
	 8: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client/session.rb:36:in `each_pair'
	 7: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client/session.rb:36:in `block in initialize'
	 6: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client/session.rb:36:in `public_send'
	 5: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client/session.rb:50:in `agent_info='
	 4: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client/session.rb:270:in `set_user_agent'
	 3: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/tools/system.rb:73:in `description'
	 2: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/tools/system.rb:33:in `full_os'
	 1: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/tools/system.rb:42:in `os_name'
/usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/tools/system.rb:42:in ``': No such file or directory - sw_vers (Errno::ENOENT)

@BanzaiMan BanzaiMan merged commit d60ef2c into travis-ci:master Sep 3, 2020
@eli-schwartz eli-schwartz deleted the portable-command-v branch September 3, 2020 18:21
@PaulinaParangerHr
Copy link

This will simply think nothing ever exists by default, since "which" doesn't exist, but for extra bonus breakage if you do have a version of which, but one that returns 0 even when the command is not found (while emitting human-readable messages), you get this highly amusing Linux error:

$ travis report
Traceback (most recent call last):
	18: from /usr/bin/travis:23:in `<main>'
	17: from /usr/bin/travis:23:in `load'
	16: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/bin/travis:18:in `<top (required)>'
	15: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/cli.rb:62:in `run'
	14: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/cli/parser.rb:36:in `new'
	13: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/cli/parser.rb:36:in `new'
	12: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/cli/api_command.rb:43:in `initialize'
	11: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client.rb:34:in `new'
	10: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client.rb:34:in `new'
	 9: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client/session.rb:36:in `initialize'
	 8: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client/session.rb:36:in `each_pair'
	 7: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client/session.rb:36:in `block in initialize'
	 6: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client/session.rb:36:in `public_send'
	 5: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client/session.rb:50:in `agent_info='
	 4: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/client/session.rb:270:in `set_user_agent'
	 3: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/tools/system.rb:73:in `description'
	 2: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/tools/system.rb:33:in `full_os'
	 1: from /usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/tools/system.rb:42:in `os_name'
/usr/lib/ruby/gems/2.7.0/gems/travis-1.9.1/lib/travis/tools/system.rb:42:in ``': No such file or directory - sw_vers (Errno::ENOENT)

None

@ghost
Copy link

ghost commented Mar 10, 2021

UaezapHKqfZ_ACyu6WOU1Q

@ghost
Copy link

ghost commented Mar 10, 2021

💯

@ghost
Copy link

ghost commented Mar 10, 2021

11903_uhd

@ghost
Copy link

ghost commented Mar 10, 2021

Commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants