The nmcli -c no device
displays:
DEVICE TYPE STATE CONNECTION
wlp3s0 wifi connected My Test Connection
p2p-dev-wlp3s0 wifi-p2p disconnected --
enp4s0f1 ethernet unavailable --
lo loopback unmanaged --
In order to separate the info on wifi, I have this command:
wf_info="$(nmcli -c no device | grep "wifi[^-]" | awk '{print "wf_devc="$1, "wf_state="$3, "wf_conn="$4}')"
eval "$wf_info"
echo "$wf_devc" # returns wlp3s0
echo "$wf_state" # returns connected
echo "$wf_conn" # returns My (while should be My Test Connection)
The problem with the above command is that for wf_conn it gives me My
while I should be the full name My Test Connection
. How can I tell the command to read from the 4th column on and not just the 4th column for the wf_conn?
CodePudding user response:
You can "collect" the rest of the fields into a single variable and then print it:
read wf_devc wf_state wf_conn < <(nmcli -c no device | awk '/wifi[^-]/{r=""; for(i=4;i<=NF;i ){r=r (i==4 ? "":" ") $i}; print $1" "$3" "r}')
Note that grep
part is incorporated into awk
, /wifi[^-]/
will make sure only those lines will be printed that contains wifi
followed by a char other than a -
char.
The r=""; for(i=4;i<=NF;i ){r=r (i==4 ? "":" ") $i}
part inits an r
empty string and then all fields starting with Field 4 are concatenated using a space.
See the online demo:
#!/bin/bash
s='DEVICE TYPE STATE CONNECTION
wlp3s0 wifi connected My Test Connection
p2p-dev-wlp3s0 wifi-p2p disconnected --
enp4s0f1 ethernet unavailable --
lo loopback unmanaged --'
read wf_devc wf_state wf_conn < <(awk '
/wifi[^-]/{
r="";
for(i=4;i<=NF;i ){
r=r (i==4 ? "":" ") $i
};
print $1" "$3" "r
}' <<< "$s")
echo "wf_devc=$wf_devc wf_state=$wf_state wf_conn=$wf_conn"
Output:
wf_devc=wlp3s0 wf_state=connected wf_conn=My Test Connection