0

I need to increase the minor version by 1 using sh command if the branch is release.

Expected result in below code is 12.3.6

#! /bin/sh
BRANCH='release/12.3.5'
echo $BRANCH | awk -F"/" '{print $1}'
echo $BRANCH | awk -F"/" '{print $2}'
BRANCH_PREFIX=$(echo $BRANCH | awk -F"/" '{print $1}')
BRANCH_SUFFIX=$(echo $BRANCH | awk -F"/" '{print $2}')
echo $BRANCH
echo "$BRANCH_PREFIX"
echo "$BRANCH_SUFFIX"
# BRANCH_SUFFIX=12.3.5
if [ -z "$BRANCH_SUFFIX" ]; then echo "testing"; else VERSION=$(awk -vFS=. -vOFS=. '{$NF++;print}' >>"$BRANCH_SUFFIX"); fi;
echo "$VERSION"

Output:

release
12.3.5
release/12.3.5
release
12.3.5
<No out put for this- echo "$VERSION">>

Not sure why echo $BRANCH_PREFIX and echo $BRANCH_SUFFIX not printing values

Shabar
  • 2,617
  • 11
  • 57
  • 98
  • 1
    https://stackoverflow.com/questions/8653126/how-to-increment-version-number-in-a-shell-script – KamilCuk Aug 24 '23 at 12:29
  • @KamilCuk Updated the question with your reference answer. Apparently something missing – Shabar Aug 24 '23 at 12:43
  • 1
    You are not `echo`ing `$BRANCH` into awk. You should also be using [shellcheck](https://github.com/koalaman/shellcheck) to fix a lot of minor issues with the script. Your code as-is gives me a "File not found" error. Fixing the issue I mentioned gives the desired output: it's just a typo. – Jared Smith Aug 24 '23 at 13:00
  • @JaredSmith Can you please explian this further "You are not echoing $BRANCH into awk."? – Shabar Aug 24 '23 at 23:08
  • 2
    @Shabar replace `BRANCH_PREFIX=$($BRANCH | awk -F"/" '{print $1}')` with `BRANCH_PREFIX=$(echo $BRANCH | awk -F"/" '{print $1}')` and your code will work. Your code as is will give a file not found error because it expands `$BRANCH` to `release/12.3.5` which gets interpreted as the path to an executable, which it isn't. – Jared Smith Aug 24 '23 at 23:16
  • @JaredSmith Thanks, Almost everything fixed. Apparently `$(awk -vFS=. -vOFS=. '{$NF++;print}' >>"$BRANCH_SUFFIX")` this won't work in `sh`. It works in `bash` with `<<<"$BRANCH_SUFFIX`. Any suggetions to work this in `sh`? – Shabar Aug 24 '23 at 23:43
  • 1
    This worked `$(echo "$BRANCH_SUFFIX" | awk 'BEGIN{FS=OFS="."} {$3+=1} 1')` https://stackoverflow.com/a/59435668 – Shabar Aug 24 '23 at 23:57
  • https://stackoverflow.com/a/61921674/2303693 – Shabar Aug 25 '23 at 00:03

0 Answers0