Question: How to get the network status (online/offline) and attribute it to a variable in shell script?

Answer: 
If you use network manager for your network connection, you could query it via dbus somewhat like this:

```
dbus-send --type=method_call --print-reply --system --dest="org.freedesktop.NetworkManager" "/org/freedesktop/NetworkManager" org.freedesktop.NetworkManager.state

```

(somewhat deprecated but still works, for newer interfaces you'd just query a different address)

If that returns a "3" you are "connected" as described in <http://projects.gnome.org/NetworkManager/developers/spec.html#type-NM_STATE> (but it says nothing about internet connectivity).

If you really want to know whether you have internet connectivity, just ping a remote server like google.com and see if that worked or not.
You say you simply want the online/offline status of an interface, and aren't concerned with speed or link-type.

Try [ethtool](http://linux.about.com/od/commands/l/blcmdl8_ethtool.htm), as root:

```
# ethtool eth0 |grep "Link detected"
  Link detected: yes
# ethtool eth1 |grep "Link detected"
  Link detected: no

```

`ifconfig` can also show you the online/offline status, and this command is usually available to any user on the system. 

```
$ { /sbin/ifconfig eth0 | grep MTU: | grep -w UP >/dev/null 2>&1 ; }; echo $?
0
# This could return some false errors
$ { /sbin/ifconfig eth1 | grep MTU: | grep -w UP >/dev/null 2>&1 ; }; echo $?
1

```