help with a unix shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with a unix shell script
# 1  
Old 01-10-2009
help with a unix shell script

hi,

i have just started learning shell scripting...



i use fedora10 with linux kernel 2.6.27.9-159.fc10.i686

i m trying to make a shell script to check for my ppp0 connection every 1min

the logic i use is pretty simple...

i check if there is any ppp0 connection , using ifconfig....if ppp0 is active then i sleep for 60 seconds and recheck...if at any time ppp0 is dropped (because of my goddamn ISP!! Smilie ) then ifconfig will not mention it....and if it does not mention it...then i ll use the "ifup ppp0" command and try and reconnect ...

here is the script

Code:
i=0
for((i=2;i<100;i++))
do
if [ ifconfig | grep "ppp0" | wc -l -eq 1 ] then
echo already connected
fi
if [ ifconfig | grep "ppp0" | wc -l -eq 0 ] then 
echo not connected
echo trying to bring up ppp0
 ifup ppp0
fi
echo current ifconfig 
ifconfig ppp0
sleep 60
i=$($i-1)
done

when i run the script...i get the following error

Code:
./ipcheckup.sh: line 6: syntax error near unexpected token `fi'
./ipcheckup.sh: line 6: `fi'

i dont understand why?
# 2  
Old 01-10-2009
Quote:
Originally Posted by c_d
hi,
Code:
i=0
for((i=2;i<100;i++))


That is non-standard syntax. More portable is:

Code:
i=2
while [ $i -lt 100 ]
do
    : ....
    i=$(( $i + 1 ))
done

Quote:
Code:
do
if [ ifconfig | grep "ppp0" | wc -l -eq 1 ] then


You don't need the test command (a.k.a. '[') and wc, and you must separate then from if <list>:

Code:
if ifconfig | grep "ppp0"; then

# 3  
Old 01-10-2009
verifing connectivity with your isp

While your script will verify that your ppp0 connection is active, there is a better way to do it. In some cases ppp0 will still show up in the output of ifconfig but your network connectivity is non-existant.

What you should be checking is the ability to ping an ip#, popular choices for the ip# would be your ISP's nameserver. if the ping fails shutdown your ppp0 and restart it.

while true ; do
ping -c 1 nameserver_ip# || echo "restart ppp0" ;
sleep 60 ;
done


James Dickens
uadmin.blogspot.com
# 4  
Old 01-10-2009
thanks guys...i modified the script as recommended...Smilie

here is a modified version

Code:
while true
do
    if ifconfig | grep "ppp0" ; then 
         echo already connected...
         echo current ipaddress
         ifconfig ppp0 | head -2
         echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         echo verifying connection...
         ping -c1 google.com > op
         cat op | head -1
         cat op | tail -2
         echo 
         echo
   else
         echo not connected
         echo trying to bring up ppp0
         ifup ppp0
   fi
   echo 
   echo 
   echo
   sleep 60
done

now....i m glad that its working...but, i dont understand why wc did not work out...

Last edited by c_d; 01-10-2009 at 11:39 PM..
# 5  
Old 01-11-2009
ok...i need help again...

this is my current code

Code:
while true
do
    if ifconfig | grep "ppp0" ; then 
        echo already connected...
        echo current ipaddress
        ifconfig ppp0 | head -2
        echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        echo verifying connection...
        ping -c1 google.com > op
        cat op | head -1
        cat op | tail -2
        echo 
        echo
    else
        echo not connected
        echo trying to bring up ppp0
        ifup ppp0
    fi
    echo 
    echo 
    echo
    i=60
    while [ $i -ne 0 ]
    do
        echo -e "next check-up in $i sec \r"
        i=$(($i-1))
        sleep 1
    done
done

the output comes out like this...

Code:
[c_d@localhost scripts]$ ./ipcheckup.sh 
ppp0      Link encap:Point-to-Point Protocol  
already connected...
current ipaddress
ppp0      Link encap:Point-to-Point Protocol  
          inet addr:<my addy>  P-t-P:<my addy>  Mask:<my addy>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
verifying connection...
PING google.com (74.125.45.100) 56(84) bytes of data.
1 packets transmitted, 1 received, 0% packet loss, time 340ms
rtt min/avg/max/mdev = 294.700/294.700/294.700/0.000 ms





next check-up in 60 sec 
next check-up in 59 sec 
next check-up in 58 sec 
^Z
[25]+  Stopped                 ./ipcheckup.sh

i thought \r was for carriage return....why isn't it working?

i want it to print "next check-up in 60 sec" and then overwrite that sentence with "next check-up in 59 sec" ...
i dont want it to list every second...60 through 0.

Last edited by c_d; 01-11-2009 at 12:08 AM.. Reason: removed my ip address...:D
# 6  
Old 01-11-2009
Quote:
Originally Posted by c_d
Code:
    i=60
    while [ $i -ne 0 ]
    do
        echo -e "next check-up in $i sec \r"

Code:
printf "next check-up in $i sec \r"

Quote:
Code:
        i=$(($i-1))
        sleep 1
    done
done

the output comes out like this...

Code:
...
next check-up in 60 sec 
next check-up in 59 sec 
next check-up in 58 sec

i thought \r was for carriage return....why isn't it working?

i want it to print "next check-up in 60 sec" and then overwrite that sentence with "next check-up in 59 sec" ...
i dont want it to list every second...60 through 0.

echo prints a newline at the end. Use printf.
# 7  
Old 01-11-2009
done....sorry to bother you....Smilie

it was supposed to be echo -en instead of echo -e

....searched the forum and found the answer...
Smilie
great forum you got here...

EDIT:

didn't notice your post cfajohnson,your post showed up after i posted mine... thanks for an alternative...gonna try and see that too...and try to notice the difference between the two...
thanks again...

and echo -en also works....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass Oracle sql script as argument to UNIX shell script?

Hi all, $ echo $SHELL /bin/bash Requirement - How to pass oracle sql script as argument to unix shell script? $ ./output.sh users.sql Below are the shell scripts and the oracle sql file in the same folder. Shell Script $ cat output.sh #!/bin/bash .... (7 Replies)
Discussion started by: a1_win
7 Replies

2. Shell Programming and Scripting

Batch script to execute shell script in UNIX server

Hi team, My requirement is to transfer pdf files from windows machine to unix server and then from that unix server we should sftp to another server. I have completed the first part i.e From windows to using to unix server with the help of psftp.exe code: psftp user@host -pw password <... (1 Reply)
Discussion started by: bhupeshchavan
1 Replies

3. Shell Programming and Scripting

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX?

Please share the doc asap as very urgently required. (1 Reply)
Discussion started by: 24ajay
1 Replies

4. Shell Programming and Scripting

Dos batch script to execute unix shell script

Can anyone help me with a dos batch script to execute a shell script residing in an unix server. I am not able to use ssh. Thanks in advance (2 Replies)
Discussion started by: Shri123
2 Replies

5. Shell Programming and Scripting

FTP from windows to unix server using unix shell script

Hi, Is it possible to ftp a huge zip file from windows to unix server using unix shell scripting? If so what command i need to use. thanks in advance. (1 Reply)
Discussion started by: Shri123
1 Replies

6. Shell Programming and Scripting

(Urgent):Creating flat file using sql script and sqlplus from UNIX Shell Script

Hi, I need help urgently for following issue. Pls help me to resolve this issue. I am calling sql script file(file1.sql) from UNIX Shell Script(script1.ksh) using sql plus and trying to create flat file that contains all records returned from SQL query in SQL script(file1.sql) I given... (6 Replies)
Discussion started by: praka
6 Replies

7. Shell Programming and Scripting

help me in sending parameters from sqlplus script to unix shell script

Can anybody help me out in sending parameters from sql*plus script to unix shell script without using flat files.. Initially in a shell script i will call sql*plus and after getting some value from some tables, i want that variable value in unix shell script. How can i do this? Please tell me... (2 Replies)
Discussion started by: Hara
2 Replies

8. Shell Programming and Scripting

check in unix shell script so that no one is able to run the script manually

I want to create an automated script which is called by another maually executed script. The condition is that the no one should be able to manually execute the automated script. The automated script can be on the same machine or it can be on a remote machine. Can any one suggest a check in the... (1 Reply)
Discussion started by: adi_bang76
1 Replies

9. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies

10. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies
Login or Register to Ask a Question