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

getbtpcli fails with a grep-related error on macOS #5

Closed
qmacro opened this issue Sep 2, 2021 · 1 comment
Closed

getbtpcli fails with a grep-related error on macOS #5

qmacro opened this issue Sep 2, 2021 · 1 comment
Assignees

Comments

@qmacro
Copy link
Contributor

qmacro commented Sep 2, 2021

This issue relates to the content in the 2021-09-01-btp-cli-installation branch of this repo.

Running the getbtpcli script on a standard macOS device results in an error relating to grep:

; getbtpcli 
Proceed (with Enter) only if you accept the SAP Developer Licence 3.1
(see https://tools.hana.ondemand.com/developer-license-3_1.txt) ...
usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
	[-e pattern] [-f file] [--binary-files=value] [--color=when]
	[--context[=num]] [--directories=action] [--label] [--line-buffered]
	[--null] [pattern] [file ...]
# ~
;

This issue was encountered and logged in this comment to the blog post SAP Tech Bytes: btp CLI – installation that accompanies this repo branch.

@qmacro qmacro self-assigned this Sep 2, 2021
@qmacro
Copy link
Contributor Author

qmacro commented Sep 2, 2021

The line where this error occurs is this one, in the get_version function of the getbtpcli script:

"$file" --version 2> /dev/null | grep -P -o '(?<=v)\d+\.\d+\.\d+'

This tries to determine the version of the btp executable (in $file) by parsing the output from the --version option. Here's what that looks like (i.e. when btp --version is invoked):

SAP BTP command line interface (client v2.8.0)

OK

The empty line and the "OK" are actually emitted to STDOUT so we throw them away by redirecting STDOUT to nowhere (2> /dev/null).

Then we have the single line to parse a version number from. We're looking for the form "x.y.z" which in this example would be "2.8.0". In order to identify such a version number as "accurately" as possible, the regular expression is being employed:

(?<=v)\d+\.\d+\.\d+

This uses a lookbehind assertion (?<=v) which says what is to be matched (\d+\.\d+\.\d+) must directly follow a lower case v; the assertion is there because the v needs to be there but not included in what's matched. Such assertions are part of the Perl Compatible Regular Expressions family, which are not available with the stock macOS version of grep (identified at the time of writing as "2.5.1-FreeBSD"). They are available with the more widespread GNU version of grep, via the -P option, which is used here.

I'd recommend you install the GNU version of grep as it's more powerful and arguably more widespread. You can do this simply by using Homebrew, and then adding the path to the newly installed executable, so that it comes before the location of the stock macOS grep. In other words:

brew install grep
export PATH=/usr/local/opt/grep/libexec/gnubin:$PATH

(add this export to your .bashrc file).

For those wondering, the -o option tells grep to just return the actual string that matched (i.e. just the "x.y.z" version number), rather than the default which would be the entire line that it found with such a match.

Finally, not that it helps much, but there's actually a note in the script, in this get_version function, that says we're using the GNU version:

# Determine the version (uses the proper grep, from GNU)

@qmacro qmacro pinned this issue Sep 2, 2021
@qmacro qmacro closed this as completed Sep 2, 2021
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

No branches or pull requests

1 participant