Bash - Increment the version number by 1 [closed] - bash

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm new to bash script. I need to bump up the version number of my build by 1.
For eg:- if my version is 1.0.0+1, bumping it would generate 1.0.1+2, ie basically incrementing the number by 1 before and after +.
How can this be done in bash script?

The bash approach which is basically close to Python style is to use a regular expression to separate the version number and the rest of the version string:
version=1.0.0+1
if [[ $version =~ ^([^+?]*[.])([0-9]*)[+]([0-9]+) ]]
then
# ... see below
else
echo Unexpected version string: "$version"
fi
When you have a match, you can refer to the capture groups in the regexp. In Python you would use something like result.group(1) to access the groups. In bash, you have for this purpose a single, predefined array BASH_REMATCH, which always refers to the most recent regexp matching. In our case, we would have
version_string_head=${BASH_REMATCH[1]} # 1.0.
version_count_hi=${BASH_REMATCH[2]} # 0
version_count_lo=${BASH_REMATCH[3]} # 1
The rest is trivial:
new_version_string=${version_string_head}$((version_count_hi + 1))+$((version_count_lo + 1))

Related

How to remove | substract directory name from bash? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
Within my bash script, which is running on a Linux server, I have a variable that points to a specific path, let's call this my-path.
my-path="/home/vMX-ENV/vMX-21.1R1/"
echo "The path is: $my-path"
...
From the given variable my-path, I am looking for a way only to display the version number 21.1R1.
The following is an example of what I am trying to accomplish.
./script.sh
the path is: /home/vMX-ENV/vMX-21.1R1/
the version is: 21.1R1
Is there a way to do this?
Thanks!
Bash has a fairly wide variety of built-in mechanisms for manipulating variables' values. Of particular interest for the present problem are parameter expansion forms that remove prefixes or suffixes that match specified shell patterns. For example:
# Remove any trailing slash and store the result in DIRNAME_NORM
DIRNAME_NORM=${DIRNAME_MAIN%/}
# Emit the value of $DIRNAME_NORM, less the longest prefix matching shell
# pattern *vMX-
echo "${DIRNAME_NORM##*vMX-}"
There is no need to rely on an external program for this case.
Using sed grouping and back referencing
$ sed 's/[^0-9]*\([^/]*\).*/\1/' input_file
21.1R1
/[^0-9]* - Exclude anything up to the next occurance of a digit character. As this part is not within the parenthesis () to be grouped, it will be excluded.
\([^/]*\) - This will group everything from the first digit up to the next occurance of / slash.
.*/ - Exclude everything else
\1 - Return the group with backreference \1.
awk can also be used.
$ awk -F"[/-]" '{print $6}' input_file
21.1R1
-F"[/-]" - Set delimiter to / and - then print column 6 which will contain the string.

Bash Script looking for string in parameter to set a variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm quite new to linux, not so much in programing language.
I'm looking for a way to set a variable (var2) to a given value depending on matching the last 3 Bytes of a MAc Adress in var1
Here's an example :
var1="AA:BB:CC:DD:EE:FF"
here is my "lookup table" base on the 3 last values of the mac adress"
DD:EE:FF > var2="Home"
AB:CD:EF > var2="Garage"
FE:DC:GH > var2="Bedroom1"
... (Max 20)
Important thing : The "lookup table" needs to be "readable" because it needs to be easily configurable. I would have used very ugly IF statements otherwise. :-)
Thanks to all the people who will take the time to help me on this.
Looks like all you need is a case statement.
case $var1 in
*:DD:EE:FF) var2="Home";;
*:AB:CD:EF) var2="Garage";;
*:FE:DC:GH) var2="Bedroom1";;
esac
I would simplify the table (referred to as table.txt below):
DD:EE:FF Home
AB:CD:EF Garage
FE:DC:GH Bedroom1
Then this works:
SUFF=$(echo $1 | cut -d ':' -f4-)
echo $SUFF
RES=$(grep ^$SUFF table.txt | cut -d ' ' -f 2)
echo $RES
example:
> ./run.sh AA:BB:CC:DD:EE:FF
DD:EE:FF
Home
I think you'll be able to adjust it for your script from here.

How to separate multiple parts of a line using ';' using shell script? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a '.GS3' file with multiple lines like this one:
0123456789 aaa.aaa3456
I want to separate it like this:
01234;56789 ;aaa.aaa;3456
I know the start and end of each part of the line. Is it possible to do this considering multiple lines? For example:
0123456789 aaa.aaa3456
aaaaabbbbbbbxxxxxxxwwww
Into
01234;56789 ;aaa.aaa;3456
aaaaa;bbbbbbb;xxxxxxx;wwww
.. but if it does have fixed lengths you can do it this way:
$ sed -r 's/^(.{5})(.{7})(.{7})(.{4})$/\1;\2;\3;\4/' test.txt
01234;56789 ;aaa.aaa;3456
aaaaa;bbbbbbb;xxxxxxx;wwww
I think .{5} is self explanatory. Due to the -r option the first group (.{5}) can be referenced by \1. It's a group due to ( and ).
The characters ^ and $ represent the beginning and ending of every line in the file test.txt.

Should I be using a case statement or an if/else [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am making a script in unix that will read a text file and each record has six fields separated by a pipe.
I don't know if I should use a case statement or an if/else statement.
If the $JOB has a value of P then NEW_NICE should be set to 3.
If the $JOB has a value of S then NEW_NICE should be set to 6.
All other values of $JOB should set NEW_NICE to 7.
I kinda wanna use a case statement because it would be simpler, but I also am not sure on how that would look.
I won't tell you which one to use since it's a matter of preference. Either is fine. Let's do some comparison shopping so you can see them side by side.
(It's best to avoid all-uppercase variable names so as not to clash with any of the shell's built-in variables, which are always uppercase. I use job and new_nice below.)
Here's how the case statement would look. Use *) for the default case.
case "$job" in
P) new_nice=3;;
S) new_nice=6;;
*) new_nice=7;;
esac
Here are two versions with if statements. In Bash you can use double brackets to avoid having to quote "$job":
#!/bin/bash
if [[ $job == 'P' ]]; then
new_nice=3
elif [[ $job == 'S' ]]; then
new_nice=6
else
new_nice=7
fi
If you're targeting plain sh then you'll have to use single brackets and quote "$job".
You could also move the else case up top if that looks better. That could be nice if 7 is the default value and 3 and 6 are less common overrides.
#!/bin/sh
new_nice=7
if [ "$job" = 'P' ]; then
new_nice=3
elif [ "$job" = 'S' ]; then
new_nice=6
fi

How to parse the List data type in Bash [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Context: Making some AWS API calls from Bash which give me a List Of Ids.
I am stuck at being able to parse it, and how do I pass it as an argument to a function.
Test Bash Script:
#!/bin/bash -ex
input_data in format:
[
"d-1",
"d-2",
"d-3",
"d-4"
]
# None of the following calls work
# assume lst=<above data type>
echo "1. $lst"
echo "2. ${lst[*]}"
echo "3. ${lst[#]}"
Any Pointers would be much appreciated. Thank You.
If you mean an array in bash (your original question seemed to indicate the input file was actual bash code), that uses () rather than []:
lst=("d-1" "d-2" "d-3" "pax is awesome")
echo "${lst[3]}" # Specific zero-based item.
for item in "${lst[#]}" ; do # Process all items.
echo "${item}"
done
The output of that is, as expected:
pax is awesome
d-1
d-2
d-3
pax is awesome
However, if you're actually getting JSON from the API, jq is a good tool to use, an example being shown in the following transcript:
pax:~> cat json.in
[
"d-1",
"d-2",
"d-3"
]
pax:~> idList=($(jq '.[]' json.in))
pax:~> for item in ${idList[#]} ; do echo ${item} ; done
"d-1"
"d-2"
"d-3"

Resources