The script uses several global variables, which can lead to unintended side effects and can make the script harder to understand and maintain. Consider passing necessary values as function arguments instead.
we'll address the issue related to handling invalid user input. The original script prompts the user for input without proper validation or clear instructions on the expected input format.
To improve this, we'll add input validation and provide clear instructions to the user on how to input valid options.
Example:
# ... (existing code)
echo "Which part of the version do you want to increase: Major (M), minor (m) or patch (p)?"
read -n 1 -r -p "Your choice: " choice
echo
case $choice in
M|m)
new_tag_number=$(echo $tag_latest | awk -F. '{ printf("%d.%d.%d", $1 + 1, 0, 0) }')
;;
m)
new_tag_number=$(echo $tag_latest | awk -F. '{ printf("%d.%d.0", $1, $2 + 1) }')
;;
p)
new_tag_number=$(echo $tag_latest | awk -F. '{ printf("%d.%d.%d", $1, $2, $3 + 1) }')
;;
*)
echo "Invalid input. Please choose a valid option: M, m, or p."
exit 1
;;
esac
# ... (existing code)
In this modified part of the script, after presenting the user with the options (Major, Minor, or Patch), we use a case statement to check the user's input ($choice). If the input matches one of the valid options, the script proceeds accordingly. If the input is invalid (doesn't match any option), the script prints an error message and exits.
This change ensures that the script validates the user's input and provides clear instructions on how to input valid options, improving the user experience and reducing the likelihood of erroneous input causing unexpected behavior.