-1

I have the following so far:

#!/bin/sh 

getVersionInfo() {
    yarn info my-package version
}

getVersion() {
    VERSION_REGEX='^'
    $1 =~ $VERSION_REGEX
}

VERSION_INFO=$(getVersionInfo)
VERSION=$(getVersion "$VERSION_INFO")
echo $VERSION

I eventually want to run the script and be able to upgrade all my projects in one command to the latest version of my-package.

I am stuck on two things:

  1. why does the console print out TWICE?
  2. how do i work out the regex (or use another technique) to get the version number?

The yarn info my-package version output is:

└─ my-package@workspace:shared
   ├─ Version: 0.0.28
   │
   └─ Dependencies
      ├─ @grpc/grpc-js@npm:^1.5.3 → npm:1.5.9
      ├─ @types/dinero.js@npm:^1.9.0 → npm:1.9.0
      ├─ @types/gulp-sourcemaps@npm:^0.0.35 → npm:0.0.35

I am using yarn version 3.2.0

Fred Johnson
  • 2,539
  • 3
  • 26
  • 52
  • 2
    try `VERSION=$(yarn info my-package version | grep -oE 'Version: (?[0-9.]*)')` – kj-crypto Mar 31 '22 at 09:34
  • 2
    This is not a Bash script; the shebang specifically requests to run it with `sh`. See also [Difference between `sh` and `bash`](https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash) – tripleee Mar 31 '22 at 09:36
  • @kj-crypto that pretty much does it. I just need to get rid of "Version: " infront of the number! – Fred Johnson Mar 31 '22 at 09:37
  • @0stone0 ah ok. new to bash, no idea :D. thanks! – Fred Johnson Mar 31 '22 at 09:37
  • Forget what I said @FredJohnson, see Tripleee's comment, it's about sterr'. Still sleeping ;) – 0stone0 Mar 31 '22 at 09:38
  • @tripleee oh right :O didn't realise that! – Fred Johnson Mar 31 '22 at 09:39
  • `$1 =~ $VERSION_REGEX` is not bash. Run it a see the error messages. – Renaud Pacalet Mar 31 '22 at 09:39
  • Again, please still [edit] to explain what you hope the code should actually mean. Code which doesn't do at all what you want it to is not a good way to explain what you actually mean. – tripleee Mar 31 '22 at 09:42
  • @FredJohnson what about this `VERSION=$(yarn info my-package version | grep -oE '[0-9.]*')` – kj-crypto Mar 31 '22 at 09:43
  • It's not clear what you want your code to do. The second function will simply not print anything at all, so the command subtitution will simply capture an empty string (though if this were Bash and you fixed its syntax, its exit code would reveal whether or not there was a match on the regex, but of course, with that regex, there always would be. The proper Bash syntax would look like `[[ $1 =~ $VERSION_REGEX ]]`) – tripleee Mar 31 '22 at 09:44
  • Oh so to add to the confusion `^` is a regex metacharacter which matches every possible string (it checks whether the string has a beginning, which of course even the empty string does). If you want to search for `^` literally, the regex for that is `\^`. But it's not clear which parts of the output from `yarn package` you want to extract. The entire three lines which contain a caret, including the fugly block drawing characters? – tripleee Mar 31 '22 at 09:48
  • That should be posted as an answer rather than as a comment. Comments are temporary and cannot be accepted as an answer. There is no mechanism for converting comments to answers, so I cannot do it for you. You will need to copy your comment into the answer box and then delete your comment. – Stephen Ostermiller Mar 31 '22 at 14:38

1 Answers1

0

Thanks to @kj-crypto the solution requires no functions and can be a one-liner:

VERSION="$(yarn info my-package version | grep -oE 'Version: (?[0-9.]*)' | grep -oE '(?[0-9.]*)')"
rofrol
  • 14,438
  • 7
  • 79
  • 77
Fred Johnson
  • 2,539
  • 3
  • 26
  • 52
  • The `(?` is a syntax error, you mean simply `[0-9.]*` without parentheses. Basically then a duplicate of https://stackoverflow.com/questions/11568859/how-to-extract-text-from-a-string-using-sed which avoids having to `grep` twice. – tripleee Apr 01 '22 at 08:20
  • Nothing ever _requires_ a function, except if you need code to be recursive. – tripleee Apr 01 '22 at 08:22
  • Your're welcome to post a working answer – Fred Johnson Apr 01 '22 at 10:23
  • You still have not provided clarifications for what the code should do. You want all the version numbers extracted from the output? Or just the first one? – tripleee Apr 02 '22 at 07:06