for file in /sys/class/net/*; do
iface=$(basename $file);
read status <$file/operstate;
test "$status" = "up" &&\
ip addr show $iface|\
awk '/inet /{print $2}'
done
@vlasales
I refactored my code and came up with this.
# Loop through the interfaces and check for the one that is up.for iface in /sys/class/net/*/operstate;do
if["$(echo$iface | awk-F'/''{print $5}')"!="lo"];then
if["$(cat$iface)"=="up"];then
interface=$(echo$iface | awk-F'/''{print $5}')printf"%s ""$(ip addr show $interface | awk'/inet /{print $2}')"fi
fi
done
But the code you wrote is shorter so I added it to my article. The old code had two loops but one loop is better than two. Thanks.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
~/.tmux/left_status.sh
Thanks for the code example Vlastimil.
@vlasales I refactored my code and came up with this.
But the code you wrote is shorter so I added it to my article. The old code had two loops but one loop is better than two. Thanks.