1

Background

I am managing a cellular interface. I have a bash script that is testing the interface. In question is the file located at /sys/class/net/wwan1/carrier where wwan1 is an unconfigured interface.

This is obviously a special file, but I am unsure how to correctly test it in a bash script. See below...

Consider the following:

file /sys/class/net/wwan0/carrier # a good interface
/sys/class/net/wwan0/carrier: ASCII text

file /sys/class/net/wwan1/carrier # an unconfigured interface
/sys/class/net/wwan1/carrier: ERROR: cannot read `/sys/class/net/wwan1/carrier' (Invalid argument)

Some cat data:

$ cat /sys/class/net/wwan0/carrier # good interface
1

$ cat /sys/class/net/wwan1/carrier # unconfigured
cat: /sys/class/net/wwan1/carrier: Invalid argument

This error from cat is the problem.

The file located at /sys/class/net/wwan1/carrier is reported by ls however:

$ cd /sys/class/net/wwan1
$ ls -al
total 0
drwxr-xr-x 4 root root    0 Dec 27 17:36 .
drwxr-xr-x 3 root root    0 Dec 27 17:36 ..
-r--r--r-- 1 root root 4096 Dec 28 20:24 addr_assign_type
-r--r--r-- 1 root root 4096 Dec 28 20:24 address
-r--r--r-- 1 root root 4096 Dec 28 20:24 addr_len
-r--r--r-- 1 root root 4096 Dec 28 20:24 broadcast
-r--r--r-- 1 root root 4096 Dec 28 18:57 carrier
lrwxrwxrwx 1 root root    0 Dec 28 20:24 device -> ../../../1-1:1.10
-r--r--r-- 1 root root 4096 Dec 28 20:24 dev_id
-r--r--r-- 1 root root 4096 Dec 28 20:24 dormant
-r--r--r-- 1 root root 4096 Dec 28 20:24 duplex
-rw-r--r-- 1 root root 4096 Dec 28 20:24 flags
-rw-r--r-- 1 root root 4096 Dec 28 20:24 ifalias
-r--r--r-- 1 root root 4096 Dec 28 20:24 ifindex
-r--r--r-- 1 root root 4096 Dec 28 20:24 iflink
-r--r--r-- 1 root root 4096 Dec 28 20:24 link_mode
-rw-r--r-- 1 root root 4096 Dec 28 20:24 mtu
-rw-r--r-- 1 root root 4096 Dec 28 20:24 netdev_group
-r--r--r-- 1 root root 4096 Dec 28 18:57 operstate
drwxr-xr-x 2 root root    0 Dec 28 20:24 power
-r--r--r-- 1 root root 4096 Dec 28 20:24 speed
drwxr-xr-x 2 root root    0 Dec 28 20:24 statistics
lrwxrwxrwx 1 root root    0 Dec 27 17:36 subsystem -> ../../../../../../../../../../class/net
-rw-r--r-- 1 root root 4096 Dec 28 20:24 tx_queue_len
-r--r--r-- 1 root root 4096 Dec 28 20:24 type
-rw-r--r-- 1 root root 4096 Dec 27 17:36 uevent

The Problem

I am testing a variety of possible interface configurations and problems in a bash script. One of which is that the interface is unconfigured. I am probing /sys because I have found the output of ifconfig and ip to be unreliable.

My Question

How can I test the value of the file /sys/class/net/wwan1/carrier in a bash if statement (the file itself being unreadable but present, i.e. this is an unconfigured interface? Here's what I have tried:

if [[ -e "/sys/class/net/wwan1/carrier" ]]; then
    echo "Good"
else
    echo "Bad"
fi

I have also used the tests -b, -c, -f all of which fail to be able to distinguish between the 'readable' state of this file in /sys/class/net/wwan1/ - the 'bad' file, and /sys/class/net/wwan0/ - the 'good' file.

Edit

The -r and -s tests reports good in both cases, which is a problem since cat will fail in the first case.

Edit stat output

$ stat /sys/class/net/wwan0/carrier
  File: `/sys/class/net/wwan0/carrier'
  Size: 4096            Blocks: 0          IO Block: 4096   regular file
Device: fh/15d  Inode: 5657        Links: 1
Access: (0444/-r--r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-12-28 17:01:06.976418548 +0000
Modify: 2015-12-28 17:01:06.976418548 +0000
Change: 2015-12-28 17:01:06.976418548 +0000
 Birth: -

and

$ stat /sys/class/net/wwan1/carrier
  File: `/sys/class/net/wwan1/carrier'
  Size: 4096            Blocks: 0          IO Block: 4096   regular file
Device: fh/15d  Inode: 6181        Links: 1
Access: (0444/-r--r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-12-28 18:57:37.876419704 +0000
Modify: 2015-12-28 18:57:37.876419704 +0000
Change: 2015-12-28 18:57:37.876419704 +0000
 Birth: -
5
  • @drewbenn Yes it is non-zero on a failed read. I think I may end up just going with that if I cannot distinguish between a valid using test – 111--- Dec 28 '15 at 21:06
  • @jimmij that is correct. I want to know can I see this with test or not – 111--- Dec 28 '15 at 21:08
  • @jimmij see edits – 111--- Dec 28 '15 at 21:27
  • /sys being a virtual file system, I'd say there's no way you can deduce what state carrier is in unless you actually read the file, not just file metadata. And if you do try to read it, you might as well use cat or file and their exit codes. – muru Dec 28 '15 at 21:33
  • You should just cat the file and check the exit status , if it is 0 then the interface is good , otherwise bad. – Ijaz Ahmad Khan Dec 28 '15 at 21:36
1

I have experimented with my WiFi interface which also have the carrier pseudo file :/sys/class/net/wlan0/carrier. (note: I have used nmtui to enable/disable Wireless networks).

It seems there are three states:

  • ./carrier not readable (for instance when the interface is disabled in Network Manager).
  • ./carrier contain "1" (when the interface is activated and it is connected to a WiFi network)
  • ./carrier contain "0" (when the interface is activated and it is not connected to a WiFi network)

  if ! cat /sys/class/net/wlan0/carrier > /dev/null; then
    echo "Not connected to any network"
  elif [ "$(head -c1 /sys/class/net/wlan0/carrier)" -eq 1 ]; then
    echo "Connected to a wireless network"
  elif [ "$(head -c1 /sys/class/net/wlan0/carrier)" -eq 0 ]; then
    echo "Not connected to a wireless network"
  else
    echo "Unknown/Unhandled state"
  fi

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.