0

The goal of this is to delete the existing connection name and create the new connections "ABC" "DEF" "GHI" and map to existing device(excluding lo).

#!/bin/bash

conname=$(nmcli con  | awk ' $1 !~ "NAME|lo"{print $1}')
ifname=$(nmcli dev  | awk ' $1 !~ "DEVICE|lo"{print $1}')

echo $conname
echo $ifname

The above returns following values:

test3 test4 test5
enp0s3 enp0s8 enp0s9

How can I produce following command lines by using previous output by awk

nmcli con del test3
nmcli con del test4
nmcli con del test5

The following is to create new connection names "ABC", "DEF", "GHI" which are mapped to interfaces "enp0s3" "enp0s8" "enp0s9". These interfaces were identified by awk. New connection names are fixed names.

nmcli con add type ethernet con-name ABC ifname enp0s3
nmcli con add type ethernet con-name DEF ifname enp0s8
nmcli con add type ethernet con-name GHI ifname enp0s9

I hope this is clearer.

  • The question appears clear to me…? – Sparhawk Nov 21 '18 at 6:47
  • modified my question to make it clear. – samky2005 Nov 21 '18 at 17:05
  • Do you literally want the *s in the output? – Sparhawk Nov 21 '18 at 20:54
  • No. I meant to emphasize by making Italic but for some reasons, it didn't. Need to read carefully Help. I thought that was the way to do that. – samky2005 Nov 22 '18 at 2:11
  • You are right normally, but it doesn't render like that here because you were in a code block. – Sparhawk Nov 22 '18 at 3:38
0

Just pipe the commands through a while loop.

$ nmcli con | awk ' $1 !~ "NAME|lo"{print $1}' | while IFS= read -r line; do echo nmcli con del "$line"; done
nmcli con del test3
nmcli con del test4
nmcli con del test5

For the second command, we create and array of the ABC, DEF, GHI, strings first, then read from this incrementally.

$ declare -a arr
$ arr=(ABC DEF GHI)
$ i=0
$ nmcli dev | awk ' $1 !~ "DEVICE|lo"{print $1}' | while IFS= read -r line; do echo nmcli con add type ethernet con-name ${arr[$i]} ifname "$line"; ((i+=1)); done
nmcli con add type ethernet con-name ABC ifname enp0s3
nmcli con add type ethernet con-name DEF ifname enp0s8
nmcli con add type ethernet con-name GHI ifname enp0s9
  • thank you very much for the tip. I had one error there. Actually "ABC" is not used all 3 lines. Each line has different name i.e. "ABC" "DEF" "GHI". Still I can use through a while loop? – samky2005 Nov 21 '18 at 8:12
  • Please edit your question to include this pertinent information, including when each string should be used, then please tag me with @Sparhawk in a comment. – Sparhawk Nov 21 '18 at 10:39
  • 3 con-name "ABC" "DEF" "GHI" to be used. They are fixed. And "ABC" maps to the first interface "enp0s3". Respectively "DEF" for "enp0s8", "GHI" for "enp0s9". nmcli con add type ethernet con-name ABC ifname enp0s3 nmcli con add type ethernet con-name DEF ifname enp0s8 nmcli con add type ethernet con-name GHI ifname enp0s9 – samky2005 Nov 21 '18 at 16:42
  • not sure why I couldn't add tag @Sparhawk in the previous – samky2005 Nov 21 '18 at 16:52
  • @samky2005 answer is updated – Sparhawk Nov 21 '18 at 21:38
2

And - as awk is used anyhow - why not

$ nmcli con | awk '$1 !~ "NAME|lo" {print "nmcli con del *" $1 "*"}' 
nmcli con del *test3*
nmcli con del *test4*
nmcli con del *test5*
$ nmcli dev | awk '$1 !~ "DEVICE|lo" {print "nmcli con add type ethernet con-name ABC ifname", $1}'

If you tell us how a "different name i.e. "ABC" "DEF" "GHI" " can be assigned, this could be incorporated as well.

  • +1 pure awk is much cleaner. (FWIW I think the * was just for emphasis, but the question is quite unclear.) – Sparhawk Nov 21 '18 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.