OpenWrt Forum Archive

Topic: startup script

The content of this topic has been archived on 22 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi

      I want run a bash scrip at boot. However I have an error. What is wrong with my script ?


#!/bin/sh /etc/rc.common
# Example script

START=40

start() {

        file="ipcam.txt"

        while IFS=: read -r xf1 xf2 xf3
        do
                printf 'Loop: %s %s %s\n' "$xf1" "$xf2" "$xf3"
                f1=$xf1
                f2=$xf2
                f3=$xf3
        done < "$file"
        printf 'After: %s %s %s\n' "$f1" "$f2" "$f3"

        
        echo "script started"
        iptables -t nat -A PREROUTING -p tcp --dport $f2 -j DNAT --to-destination $f1:$f3

root@mylinkit:/etc/init.d# ./ipcam start
Loop: 192.168.0.159
Loop: 554
Loop: 554
After: 554
script start
iptables v1.4.21: invalid port/service `-j' specified
Try `iptables -h' or 'iptables --help' for more information.
root@mylinkit:/etc/init.d#

(Last edited by enkavak on 14 Jul 2017, 12:26)

enkavak wrote:

      I want run a bash scrip at boot. However I have an error. What is wrong with my script ?

#!/bin/sh /etc/rc.common
# Example script
...
/etc/rc.common: ./ipcam: line 16: syntax error: unexpected redirection

You are not running bash shell, but "ash" as the default user shell (in busybox). /bin/sh leads to that.
Bash is not installed by default and naturally you can't use bash-specific things like  "<<<" in sh.

EDIT: You apparently edited your message while I was writing this...
Your current error message about iptables might be caused by using a target that needs an additional iptables kmod modules to be installed.

And you might debug your command by having the script echo the exact command line parameters for iptables so that you see if your variable contents are what you expect.

(Last edited by hnyman on 14 Jul 2017, 12:31)

Hi

     when I send the string from command line, I get no error.

     

iptables -t nat -A PREROUTING -p tcp --dport 554 -j DNAT --to-destination 192.168.0.159:554

     there should be another thing with my script.

Thanks in advance

If "f2" and "f3" have not been initialized (as the "After: 554" message indicates), then your "iptables" command translates to

iptables -t nat -A PREROUTING -p tcp --dport -j DNAT --to-destination 554:

This is a basic shell-scripting issue, you will probably get more help out of the OpenWrt specific forums.

The discussion might have continued from here.