1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Scripting puzzle

Discussion in 'HyperWRT Firmware' started by Gate777, Jul 15, 2007.

  1. Gate777

    Gate777 Network Guru Member

    Tearing my hair out....

    I have a WRT54GSv4 running Thibor 15c.

    My startup script creates another script, called "splitter", in the /tmp/ folder which is then run periodically via cron.

    Before the splitter script does anything useful it needs to check that a ppp0 interface (internet connection) exists.

    The relevant sections of the "splitter" script go like this:

    Code:
    INTAL=ppp0
    INTS=`ifconfig|grep ppp0|awk '{print $1}'`
       echo "INTS=$INTS" >> splitlog 
    if [ "$INTS" == $INTAL ]; then
       echo "Intal connected" >> splitlog 
    
       <do something useful here>
     
    else
       echo "Intal not connected" >> splitlog 
    fi
    
    The output to the "splitlog" is as follows:

    Code:
    INTS=
    Intal not connected
    INTS=
    Intal not connected
    INTS=
    Intal not connected
    etc
    
    The frustrating thing is that ppp0 certainly does exist and in the GUI when I "Run Command":

    Code:
    INTS=`ifconfig|grep ppp0|awk '{print $1}'`;echo "INTS=$INTS"
    
    I get

    Code:
    # INTS=`ifconfig|grep ppp0|awk '{print $1}'`;echo "INTS=$INTS"
    
    INTS=ppp0
    
    So why am I getting different results despite using identical commands and syntax?

    Help!
     
  2. mstombs

    mstombs Network Guru Member

    You should check the file that you create in /tmp for correct syntax, I suspect there is an issue with the echo commands or 'here' command used to write the file and the quote symbols.
     
  3. Gate777

    Gate777 Network Guru Member

    The code in the first block in my post is dumped from the actual "splitter" file created in /tmp at startup and the run command I entered is cut and pasted from the same code.

    The first three lines are created in the startup script like this:

    Code:
    echo "INTAL=ppp0" >> splitter
    echo "INTS=\`ifconfig|grep ppp0|awk '{print \$1}'\`" >> splitter
    echo "   echo \"INTS=\$INTS\" >> splitlog " >> splitter
    
     
  4. Gate777

    Gate777 Network Guru Member

    If I run the "splitter" script directly from the "Run Command" it works, with output

    Code:
    INTS=ppp0 
    Intal connected
    
    I have also experimented by adding this to beginning of my script

    Code:
    ifconfig >> splitlog
    which comes up blank when the script is run via cron, but gives the expected results when the script is executed from the "Run Command".

    If there some reason why ifconfig would fail to function when executed by cron?

    Before creating the "splitter" script the startup script does this:

    Code:
    cd /tmp
    touch splitlog
    echo "#!/bin/sh" > splitter
    
    After creating the "splitter" script the startup script does this:

    Code:
     
    chmod +x splitter
    echo "*/2 * * * * root /tmp/splitter" > cron.d/check_split
    
    Any other ideas on what could be going wrong?
     
  5. Gate777

    Gate777 Network Guru Member

    Solved changing

    Code:
    INTS=`ifconfig|grep ppp0|awk '{print $1}'`
    
    to

    Code:
    INTS=`/sbin/ifconfig|grep ppp0|awk '{print $1}'`
    
    Which is strange since /sbin is the crontab path. Oh well... on with the show.
     
  6. mstombs

    mstombs Network Guru Member

    Good one - just to pass on to all a tip given to me elsewhere - you can use 'here' statements to make writing files from the startup script easier (avoiding echo and filename on each line), for example I now have this in part of my startup script

    Code:
    cat > ez.bat <<-EOF2
    #!/bin/sh
    cd /tmp
    if !(pidof ez-ipupdate > /dev/null) 
    then
    	/usr/sbin/ez-ipupdate -c /tmp/ez.conf -b /tmp/ez.cache
    fi
    fbuf=\$(cat /tmp/ez.cache)
    if [ "\$(nvram get ddns_cache)" != "\$fbuf" ]; then
    	nvram set ddns_cache=\$fbuf; nvram commit
    fi
    EOF2
    Apparently you can use any command (not just 'cat'), the EOF labels must be unique if you write more files, the "-" in "-EOF" strips leading tabs.
     

Share This Page