2

I am trying to separate the ip/block address and place the ip in a variable and the block in another. This is what I have so far.

#!/bin/bash

ipblock="100.100.40.160/29"
block="$(basename $ipblock)"
#ipaddress="100.100.40.160"

echo "block=\"$block\""
#echo "address=\"$ipaddress\""

I need the ip address equation so that the results are ...

block="29"
address="100.100.40.160"
Vituvo
  • 1,008
  • 1
  • 9
  • 29
  • I don't see how your link makes my question a duplicate. Somewhat same scenario but not duplicate. I could not use their answer to formulate an equation to give me the answer I was looking for. Thanks for trying. – Vituvo Apr 23 '18 at 01:04
  • It is marked as duplicated, because you are trying to split a string "100.100.40.160/29" by a delimiter "/". The linked SO question contains plenty of answers that would solve your task; one of them being: `address=$(echo ipblock | cut -d "/" -f 1); block=$(echo ipblock | cut -d "/" -f 2)` – M M Jun 15 '22 at 05:11

1 Answers1

0

I kept research the boards here and found something similar at https://stackoverflow.com/a/4170409/2186005 and it works well.

#!/bin/bash

ipblock="100.100.40.160/29"
block="$(basename $ipblock)"
ipaddress="${ipblock%/*}"

echo "block=\"$block\""
echo "address=\"$ipaddress\""

when I ran the script, I received the results I was initially looking for.

root@hills #/home # sh script.sh
block="29"
address="100.100.40.160"
Vituvo
  • 1,008
  • 1
  • 9
  • 29