F5F Stay Refreshed Software Operating Systems Linux Bash - Swap a digit for another in a command

Linux Bash - Swap a digit for another in a command

Linux Bash - Swap a digit for another in a command

J
Jazzy_Senpai
Member
180
10-04-2025, 03:21 AM
#1
Hi, here’s a revised version of your request. Let me know if you need further adjustments.

The goal is to update the IP address in the configuration file based on certain network mask conditions. You’re currently extracting the last octet and planning to append a new digit from the range. Consider using a loop or conditional logic to iterate through the range and modify the IP accordingly. Make sure to test each step to avoid errors. Let me know if you’d like help structuring the script further!
J
Jazzy_Senpai
10-04-2025, 03:21 AM #1

Hi, here’s a revised version of your request. Let me know if you need further adjustments.

The goal is to update the IP address in the configuration file based on certain network mask conditions. You’re currently extracting the last octet and planning to append a new digit from the range. Consider using a loop or conditional logic to iterate through the range and modify the IP accordingly. Make sure to test each step to avoid errors. Let me know if you’d like help structuring the script further!

J
JimHogg8
Junior Member
20
10-04-2025, 10:52 PM
#2
Its a bit tricky with different subnet ranges, so i dont know how much you want to get into that but here is a start: a=(${ip//./ }) will give to an array with all the numbers between the dots you can access each number with ${a[0]} ${a[1]} etc. Then you can do what you want to do with the range. If I take my interpretation of your question it would be: a=(${ip//./ }) range="$(($lastoct + $norpi))" newRange=$(($range+${a[3]})) echo "${a[0]}.${a[1]}.${a[2]}.$newRange" EDIT corrected wrong bracket type
J
JimHogg8
10-04-2025, 10:52 PM #2

Its a bit tricky with different subnet ranges, so i dont know how much you want to get into that but here is a start: a=(${ip//./ }) will give to an array with all the numbers between the dots you can access each number with ${a[0]} ${a[1]} etc. Then you can do what you want to do with the range. If I take my interpretation of your question it would be: a=(${ip//./ }) range="$(($lastoct + $norpi))" newRange=$(($range+${a[3]})) echo "${a[0]}.${a[1]}.${a[2]}.$newRange" EDIT corrected wrong bracket type

L
LightCloud
Member
145
10-09-2025, 07:08 PM
#3
This approach would convert the IP address into a numerical format, enabling you to create an array from it.
L
LightCloud
10-09-2025, 07:08 PM #3

This approach would convert the IP address into a numerical format, enabling you to create an array from it.

B
brightzewl
Junior Member
33
10-09-2025, 08:58 PM
#4
I made a mistake with the symbols instead of using parentheses. Trying alternatives like a=(${ip//./}) didn’t help, and other characters besides spaces or slashes didn’t work either. I’m not sure why this happens—it’s something I’ve always done without thinking about questioning it.
B
brightzewl
10-09-2025, 08:58 PM #4

I made a mistake with the symbols instead of using parentheses. Trying alternatives like a=(${ip//./}) didn’t help, and other characters besides spaces or slashes didn’t work either. I’m not sure why this happens—it’s something I’ve always done without thinking about questioning it.