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

Very simple .sh problem (RT-N16 LED Load Script)

Discussion in 'Tomato Firmware' started by philess, Mar 6, 2013.

  1. philess

    philess Networkin' Nut Member

    Good morning guys,

    last night this was driving me crazy and i still cannot figure out whats wrong today.
    Simply i want this script to be working:

    Code:
    #!/bin/sh
    gpio="gpio"
     
    amber=3
    white=2
     
    delay=3
     
    meltdown=400
    overload=100
    highload=70
    medload=30
     
    while sleep $delay;do
      set -- $(cat /proc/loadavg)
      load="${1%.*}${1#*.}"
     
      if [ $load -gt $meltdown ];then
        $gpio disable $amber
        usleep 50000
        $gpio disable $white
        usleep 50000
        reboot
      elif [ $load -gt $overload ];then
        $gpio disable $amber
        usleep 50000
      elif [ $load -gt $highload ];then
        $gpio disable $amber
        usleep 12500
        $gpio enable $amber
        usleep 12500
        $gpio disable $amber
        usleep 12500
        $gpio enable $amber
        usleep 12500
        $gpio disable $amber
        usleep 12500
        $gpio enable $amber
        usleep 12500
      elif [ $load -gt $medload ];then
        $gpio enable $amber
        $gpio disable $white
        usleep 25000
        $gpio enable $white
        usleep 25000
        $gpio disable $white
        usleep 25000
        $gpio enable $white
        usleep 25000
      else
        $gpio disable $white
        usleep 50000
        $gpio enable $white
        usleep 50000
      fi
    done
    Source: http://www.dd-wrt.com/wiki/index.php/LED_Scripts

    Of course i have changed the important parts, i even cut it down to the very basics:

    Code:
    #!bin/sh
    highload=70
    medload=30
    while sleep 3;do
    set -- $(cat /proc/loadavg)
    load="${1%.*}${1#*.}"
    if [ $load -gt $highload ];then
    gpio disable 1
    elif [ $load -gt $medload ];then
    gpio enable 1
    usleep 12500
    gpio disable 1
    usleep 12500
    gpio enable 1
    usleep 12500
    else
    gpio enable 1
    usleep 50000
    fi
    done
    No matter what, i always get syntax errors. In the above version:
    Code:
    root@router:/opt/tomato# sh ledload.sh
    ledload.sh: line 9: syntax error: unexpected "elif" (expecting "then")
    I am sure i am overlooking something incredibly stupid, but i cant figure it out.
    It is not that LED displaying the CPU load is important at all, but i just drives me nuts
    that it is not working at all.
     
  2. shibby20

    shibby20 Network Guru Member

    try

     
  3. Monk E. Boy

    Monk E. Boy Network Guru Member

    Good catch shibby, I knew there had to be something wrong with the thens but at a glance couldn't figure it out.

    DD-WRT's examples have a space between ; and then and your script removed those spaces.
     
  4. koitsu

    koitsu Network Guru Member

    I can't reproduce this problem with stock Busybox /bin/sh using tomato-K26USB-1.28.0501.3MIPSR2Toastman-RT-N-Ext.trx. This is Busybox v1.18.5 (to find out the version, run the command /bin/busybox and look at the first line of output).

    My guess is that your "sh" program (because you're running something through sh manually rather than letting the hashbang line take care of it (and you have a typo in one of your hashbang lines anyway!)) may be referring to an alternate (and broken) shell that you've installed per Optware or some other nonsense, since "sh script.blah" then relies on $PATH to decide what "sh" is, rather than just rely on the fully-qualified path in the hashbang line.

    My initial guess is that whatever shell this is (and it may be Busybox, but possibly an old version with a bug?) is barfing over the lack of space between the semi-colon and the word "then".
     
  5. darkknight93

    darkknight93 Networkin' Nut Member

    just one simple thought: do you Transfer your .sh via WinSCP or other applications? Sometimes they Transfer scripts in binary mode - the .sh gets corrupted. so happened to me.
    if you're using an application, try to Change the Transfer mode to text/automatic.
     
    koitsu likes this.
  6. RMerlin

    RMerlin Network Guru Member

    Most important is, make sure you save the file in UNIX encoding if you create it from a Windows machine, otherwise it won't work properly under Linux.

    I recommend using Notepad++ for that kind of thing.
     
    mpegmaster likes this.
  7. Monk E. Boy

    Monk E. Boy Network Guru Member

    Personally I normally ssh into a router, fire up vi, switch to insert mode, then paste in the script. On large pastes it sometimes drops characters from the end though.
     

Share This Page