[Solved] Shell Script problem in running command

I have created a small shell script with the help of internet searches. I am still very new to scripting. So my script looks like this

#!/bin/sh

# pppoe-wan connect check
logger "Starting internet check script..."

IP=""

internet_func ()
{
    IP="$(ip addr show pppoe-wan | grep -oE '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b')"
    logger "function says:" $IP
}

logger "trying to run the IP function..."
internet_func

if [$IP != ""]
then
{
    logger "Network is connected - no need to reset."
}
elif [$IP = ""]
then
{
    logger "Network is down. Resetting the network, hold on!"
    /etc/init.d/network restart
}
fi
logger "Finished internet check script."

Well my script does not work well at the moment and I dont know what else do i need to do in there to make it work. I have already done chmod +x script.sh to make it runnable. I can run the script just fine but the internet_func doesn't really work though. The variable IP doesn't get the value from the command that it is supposed to.

For example if i stop the pppoe-wan interface then the command returns "pppoe-wan does not exist." but it should return an empty string because it did not find the IP. To explain the script, I want to only get the IP from the command and if the command returns the IP the network is connected but if it returns an empty string then the network is down.
Can someone help me figure out how can i make it work?

Update:
I kinda updated the script a little bit and now the If-else part doesn't work as expected. The IP gets two values xx.xx.xx.xx xx.xx.xx.xx but the if else statement never gets executed. Any pointers?

Output from log:

Wed Feb 28 13:49:56 2018 user.notice root: Starting internet check script... 
Wed Feb 28 13:49:56 2018 user.notice root: trying to run the IP function... 
Wed Feb 28 13:49:56 2018 user.notice root: function says: 39.40.204.13 182.176.1.48
Wed Feb 28 13:49:56 2018 user.notice root: Finished internet check script.

The output in CLI:

root@AhmarRouter:/data# ./adslpppoe.sh
./adslpppoe.sh: line 28: [39.40.204.13: not found
./adslpppoe.sh: line 28: [39.40.204.13: not found

The shell doesn't know anything about your alias pppoe-wan.

I think you will want to use eth0 (or whatever the WAN is using)

But pppoe-wan is an interface which connects to internet. eth0 is a static IP interface to access the bridged modem. It will always show the IP because it is static.

That's why I said whatever the WAN is using.

I am able to get the IP from the command into the IP variable but the if-else statement seems wrong. Can you check the updated post?

I would add an else statement before the fi

else
       logger "None of the conditions were met"
fi

This looks like a pretty straight-forward tutorial site...

https://www.tutorialspoint.com/unix/unix-what-is-shell.htm

1 Like

I was able to do it with a little more help from internet. It appears i was using the variables without quotes in the if statement so that was causing the errors. To match the strings you need to use quotes.

if [ "$Name" == "$Name" }
then
       # do something
fi

Thanks.