Home > Back-end >  Merging two commands together
Merging two commands together

Time:03-19

I am currently getting the latest version from my git tags using:

git fetch --tags; git tag -l | awk -F'[/]' '{print $2}' | sort | tail -1 | awk -F. -v OFS=. '{$NF = 1 ; print}'

which returns a version such as 1.0.0.

Then I use the following to update the version:

ls *.yml | awk -F'[.]' '{print $1 "/1.0.0"}' | xargs -L 1 -I@ bash -c 'git tag -m @ @'; git push --tags

How do I automatically pass the output from the first command into the second, so that I don't have to manually change 1.0.0?

I have tried:

git tag -l | awk -F'[/]' '{print $2}' | sort | tail -1 | awk -F. -v OFS=. '{$NF = 1 ; print}' | read version; ls *.yml | awk -F'[.]' '{print $1 "/" $version}' | xargs -L 1 -I@ bash -c 'echo @'

but it did not work (probably because of forking?)

CodePudding user response:

The | read version spawns a subshell and stores stdin in the variable version, but then the pipeline finishes, the subshell is closed, and the assignment (made in the subshell) is discarded; general issue is the assignment, in the subshell, is not passed up to the calling/parent process.

One idea using OP's current code:

version=$(git tag -l | awk -F'[/]' '{print $2}' | sort | tail -1 | awk -F. -v OFS=. '{$NF  = 1 ; print}')

ls *.yml | awk -F'[.]' -v ver="${version}" '{print $1 "/" ver}' | xargs -L 1 -I@ bash -c 'echo @'

NOTES:

  • I'm assuming OP's current code works as desired; the objective here is just to capture the version at the top-level and make it available for the follow-on awk
  • there are likely more efficient ways to extract and update the version string but would need to see the output from get tag-l and sample contents from some *.yml files (if OP wants to investigate this further then a new question should be asked)

CodePudding user response:

You can use the result of a command, using $(...), as you can see from this example:

Starting with:

How to perform a calculation? (Usage of $(( ... )))

Prompt> echo $((3 * 2))

An example of a command, which results in a number:

Prompt> cat Analyse.txt | wc -l
1337

Usage of that result in a calculation:

Prompt> echo $(($(cat Analyse.txt | wc -l) * 2))
2674

However: your command for retrieving the version number is quite complicated, and the command for using that version number is also quite large, so the combination of both might make your script quite messy and ununderstandable (and unmaintainable), so you might opt for the answer of mark.

  • Related