I am a beginner with BASH I am trying to automate the following process:
- get release version --> file "release"
- increase it
- save it again in the same file "release"
- git add all
- git commit "releaseX"
- git push
I will call this file releaseit.sh
for that I start with some code I got from: Extract version number from file in shell script
BASH:
read version < release
echo $version
echo ${version%.*}.$((${version##*.} 1))
How do I pass the last line to a variable and save it again into release file?
Desired result: if release contains 0.1.34 a new release file will contain 0.1.35 will be added committed and pushed.
CodePudding user response:
Disregarding the discussion of whether you should do it or not, you could do this:
#!/bin/bash
read version < release
echo $version
nextversion=${version%.*}.$((${version##*.} 1))
echo "$nextversion" >release
Same as Stanislav's answer above, just storing it in a variable, if you ever need it later in the script.
CodePudding user response:
You really don't want to do this. The release number should not be stored in a file in the repo, since doing this will lead to multiple commits in the repo that all share the same version. This is a recipe for disaster. However, if you do want to do this, you might want something like:
#!/bin/sh
version_file=release
if test "$1" != "-f" && git rev-parse HEAD > /dev/null 2>&1 \
&& ! git diff-index --quiet HEAD; then
echo 'Repo is dirty. Aborting' >&2
exit 1
fi
IFS=. read maj min patch < "$version_file"
if { ! test "$maj" -ge 0 || ! test "$min" -ge 0 || ! test "$patch" -gt 0; } 2>/dev/null; then
echo "Invalid version in $version_file. Aborting" >&2
exit 1
fi
case $1 in
maj) : $(( maj = 1 ));;
min) : $(( min = 1 ));;
*) : $(( patch = 1 ));;
esac
ver="$maj.$min.$patch"
echo "$ver" > "$version_file" || exit
git add "$version_file" || exit
git commit -m "Increment version to $ver" || exit
git push || exit
CodePudding user response:
Just use > to overwrite the file content:
read version < release
echo ${version%.*}.$((${version##*.} 1)) > release
But I'd agree with the comments - you shouldn't store your build version in Git, read this.
