decrement a four part number in shell script

I have a four part number

eg: 1.21.1.3

I need to find a way in shell script to decrement this by one and put in a loop

so the values printed will be

1.21.1.2
1.21.1.1
1.21.1.0

Which is the best way to do this in shell script??

Code as below...you can let x be read by while loop from a file.

x=1.21.1.4
y=${x##[0-9]*.}
while  [[ ${y} -ge  0 ]]
do
echo ${x%.[0-9]*}.${y}
(( y-- ))
done
o/p:-
1.21.1.4
1.21.1.3
1.21.1.2
1.21.1.1
1.21.1.0

:D:D:D:D:D

---------- Post updated at 10:13 ---------- Previous update was at 10:01 ----------

I mean in above that if you have
a file like below:-

cat file.txt:-
1.2.3.4
2.3.4.5
3.4.5.6
.
.
.

Just add another while loop to read
the value of x :-

New code:-

while read x
do
y=${x##[0-9]*.}
while  [[ ${y} -ge  0 ]]
do
echo "${x%.[0-9]*}"".""${y}"
(( y-- ))
done
done < file.txt
o/p:-
1.2.3.4
1.2.3.3
1.2.3.2
1.2.3.1
1.2.3.0
2.3.4.5
2.3.4.4
2.3.4.3
2.3.4.2
2.3.4.1
2.3.4.0
3.4.5.6
3.4.5.5
3.4.5.4
3.4.5.3
3.4.5.2
3.4.5.1
3.4.5.0

:D:D:D:D

awk 'BEGIN{FS=".";} {if($4 >0){ while(x>=0) {x=$4--;print $1"."$2"."$3"."x;}}' filename

#filename has the IPs to be printed

Hi Ahmad,

Your code works well, but you do not need the [0-9] and if you change the (( y-- )) and the [] it will be posix compliant:

x=10.121.123.114
y=${x##*.}
while [ $y -ge 0 ]
do
  echo ${x%.*}.${y}
  y=$(( y-1 ))
done

Np problem man this ([[ & (())) is because I am using bash...but ${x##[0-9]*.} is just an easy way to insure that the input is a number
else it will give an error.

Hi Ahmad, that is the thing. If your last number is e.g. a letter your code will not raise an error, whereas if you leave out the [0-9] it will.
If I feed e.g.:

x=10.123.4.A

the output will be :

10.123.A

Whereas if we leave out the [0-9]:

./test: line 3: [: a: integer expression expected

which is what you want. The pattern operator will not raise the error, but the test will.

No problem man...:b::b::b::b::D:D:D:D:D

thanks a lot all of you ahmad, Scrutinizer, tene...
This is working for me. You guyz made my day:):):slight_smile: