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.

5
  • The question appears clear to me…? Commented Nov 21, 2018 at 6:47
  • modified my question to make it clear. Commented Nov 21, 2018 at 17:05
  • Do you literally want the *s in the output? Commented Nov 21, 2018 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. Commented Nov 22, 2018 at 2:11
  • You are right normally, but it doesn't render like that here because you were in a code block. Commented Nov 22, 2018 at 3:38

2 Answers 2

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
  • +1 pure awk is much cleaner. (FWIW I think the * was just for emphasis, but the question is quite unclear.) Commented Nov 21, 2018 at 10:41
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
7
  • 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? Commented Nov 21, 2018 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. Commented Nov 21, 2018 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 Commented Nov 21, 2018 at 16:42
  • not sure why I couldn't add tag @Sparhawk in the previous Commented Nov 21, 2018 at 16:52
  • @samky2005 answer is updated Commented Nov 21, 2018 at 21:38

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.