rpi_ayu
Posts: 30
Joined: Tue Sep 24, 2013 10:55 am
Location: Bhopal
Contact: Website

shell script throwing error on rpi but works fine on ubuntu

Mon Oct 07, 2013 12:49 pm

strange thing to say but same script which is working fine for ubuntu but throwing error on rpi the content of script is as given below. in this script just trying to find ip of working interface (interface which is up)

Code: Select all


    #!/bin/bash
     
     
    wifiip=$(ip addr \
    | grep inet | grep wlan0 \
    | awk -F" " '{print $2}' \
    | sed -e 's/\/.*$//')
     
    checketh0=$(ip addr |grep eth0 |grep DOWN &> /dev/null ; echo $?)
     
    if [ "$checketh0" == "0" ] ; then
        eth0ip="not connected"
      else
        eth0ip=$(ip addr \
        | grep inet | grep eth0 \
        | awk -F" " '{print $2}' \
        | sed -e 's/\/.*$//')
    fi
     
    checketh1=$(ip addr |grep eth1 |grep DOWN &> /dev/null ; echo $?)
     
    if [ "$checketh1" == "0" ] ; then
        eth1ip="not connected"
      else
        eth1ip=$(ip addr \
        | grep inet | grep eth1 \
        | awk -F" " '{print $2}' \
        | sed -e 's/\/.*$//')
    fi
     
    # report findings, only returning devices with IPs
    echo "wlan0: $wifiip" | grep [0-9]$
    echo "eth0: $eth0ip" | grep [0-9]$
    echo "eth1: $eth1ip" | grep [0-9]$
works fine on ubuntu o/p shows itself
ayush@ayush-desktop:~$ sh ip.sh
wlan0: 192.168.1.82

but when same script is when i am running on rpi
pi@raspberrypi ~ $ sh ip.sh
ip.sh: 13: [: 0
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000: unexpected operator
ip.sh: 24: [: 0: unexpected operator

i know its silly question to ask but i really need it solution for this .........Thanks in advance..

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: shell script throwing error on rpi but works fine on ubu

Mon Oct 07, 2013 1:05 pm

My suggestion for debugging would be to run the commands

Code: Select all

ip addr |grep eth0 |grep DOWN &> /dev/null ; echo $?
and

Code: Select all

ip addr |grep eth1 |grep DOWN &> /dev/null ; echo $?
on both the Ubuntu and RPi systems and look for differences in the output that is returned.
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

chilinski
Posts: 16
Joined: Mon Sep 16, 2013 8:08 pm

Re: shell script throwing error on rpi but works fine on ubu

Mon Oct 07, 2013 1:30 pm

The hostname command will also give you the ip addresses.

For example: hostname -I (that's a capital i). It won't tell you which ones are active and it won't give you the nic (e.g. eth0), but it works for basic stuff.

ripat
Posts: 191
Joined: Tue Jul 31, 2012 11:51 am
Location: Belgium

Re: shell script throwing error on rpi but works fine on ubu

Mon Oct 07, 2013 6:18 pm

As your are using iproute, you can also simply use:

Code: Select all

ip route | awk '/src/{print $3": "$9}'
Using Linux command line usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

User avatar
BluDragon
Posts: 83
Joined: Thu Sep 05, 2013 7:32 am
Location: 127.0.0.1

Re: shell script throwing error on rpi but works fine on ubu

Tue Oct 08, 2013 8:40 am

:idea:
instead of using

Code: Select all

checketh0=$(ip addr |grep eth0 |grep DOWN &> /dev/null ; echo $?)
use spaces like;

Code: Select all

checketh0=$(ip addr | grep eth0 | grep DOWN &> /dev/null ; echo $?)
"Whenever a separation is made between liberty and justice, neither, in my opinion, is safe." - Edmund Burke
"Men make counterfeit money; in many more cases, money makes counterfeit men." - Sydney J. Harris

User avatar
jojopi
Posts: 3110
Joined: Tue Oct 11, 2011 8:38 pm

Re: shell script throwing error on rpi but works fine on ubu

Tue Oct 08, 2013 9:19 am

rpi_ayu wrote:pi@raspberrypi ~ $ sh ip.sh
ip.sh: 24: [: 0: unexpected operator
"&>" and "[ x == y ]" are bash extensions. You must run the script as "bash ip.sh", not "sh ip.sh".

Or, preferably, change all the "&> /dev/null" to ">/dev/null 2>&1", and all the "if [ x == y ]" to "if [ x = y ]", which are the standard ways to express these operations.

On Ubuntu, sh is probably the same as bash. But in Debian/Raspbian, sh is dash, which implements only standard POSIX behaviour.
BluDragon wrote::idea:
If you are going to guess what the problem might be, you could at least take the time to check whether your suggestion makes any difference at all. It does not.

User avatar
BluDragon
Posts: 83
Joined: Thu Sep 05, 2013 7:32 am
Location: 127.0.0.1

Re: shell script throwing error on rpi but works fine on ubu

Wed Oct 09, 2013 12:16 am

jojopi wrote:
BluDragon wrote::idea:
If you are going to guess what the problem might be, you could at least take the time to check whether your suggestion makes any difference at all. It does not.
Image
i don't have my raspberry pi setup right meow. it was just a suggestion anyway.

use this instead of using sh or bash,

Code: Select all

chmod +x script
./script
if you still can't get it to work, try using each line in terminal. you will see the problem if you do this. sometimes compilers & command interpreters don't give you completely accurate errors.
"Whenever a separation is made between liberty and justice, neither, in my opinion, is safe." - Edmund Burke
"Men make counterfeit money; in many more cases, money makes counterfeit men." - Sydney J. Harris

Return to “General discussion”