Script with If statement

Do anyone know if I can in a Script for OpenWRT write statement code like in C language?

If (something==1)
{Do this}
Else if (something==2)
{do this instead}

best way is using bash

1 Like

Keep in mind OpenWRT doesn't use bash, but ash. It doesn't support many of the fancy features bash offers. I got bit by the fact that I wrote and tested my script on a system with bash, and then realized ash doesn't support arrays at all (or at least it didn't when I tried it out).

1 Like
root@OpenWrt:~# cat /tmp/script.scr

#!/bin/sh
something=$1

if [ "$something" == 1 ]; then

        echo "It's equal to 1"

elif [ "$something" == 2 ]; then

        echo "It's equal to 2"

else
        echo "No match"

fi

root@OpenWrt:~# /tmp/script.scr 1
It's equal to 1
root@OpenWrt:~# /tmp/script.scr 2
It's equal to 2
root@OpenWrt:~# /tmp/script.scr 3
No match
root@OpenWrt:~#

5 Likes

That's easy to remediate: use #!/bin/sh at the start of your script and it should be run by a POSIX compatible shell (Debian defaults to dash e.g.). That alone should filter out a lot of the bashims that bite you when it comes to portability.

2 Likes

I had in mind to have different config settings for different devices in my config script.

But activate the version I want with something like
wrt3200acm=1
In the beginning and have the unique settings in blocks.

1 Like

Ok a stupid question but what is the formal function of $ in Linux?

1 Like

https://pubs.opengroup.org/onlinepubs/9699919799/

1 Like

bash and python work fine for me...

https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion

The ‘$’ character introduces parameter expansion, command substitution, or arithmetic expansion.

1 Like

yes.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.