DEV Community

Discussion on: Make Your Tmux Status Bar 100% Better With Bash

Collapse
vlasales profile image
Vlastimil Pospichal

~/.tmux/left_status.sh

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
Enter fullscreen mode Exit fullscreen mode
Collapse
brandonwallace profile image
brandon_wallace Author

Thanks for the code example Vlastimil.

Collapse
brandonwallace profile image
brandon_wallace Author • Edited on

@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 
Enter fullscreen mode Exit fullscreen mode

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.