Running UCI command inside for loop

Hi,
I am trying to run the uci command under a for loop like below:

for iface in `cat /etc/iface_list`
do
        var=$(uci -q get network.$iface.auto)
        if [ $var -eq 1 ] 2>/dev/null
        then
                echo "status is UP"
          fi
done

But its not taking the uci command properly, and giving error like below:
-----debug log------

.auto'-q get 'network.bond0
+ var=
+ '[' -eq 1 ]

Any help is appreciated. Thanks

try

if [[ $var -eq 1 ]] 

There is nothing in var even. the problem is uci command is not getting executed properly.

The script is correct. Can you share the contents of /etc/iface_list as well as the output of uci show network | grep auto ?

You script executed fine for me and returned the value of auto for each interface that had it set. For all other interfaces an error was returned: uci: Entry not found.

Your /etc/iface_list file contains Windows newlines (\r\n) while Linux uses just line feeds (\n) as EOL designation.

This causes each word in your for loop to contain a trailing, invisible \r so you're not actually querying network.bond0.auto but network.bond0\r.auto which causes $var to be always empty.

Solution: convert your /etc/iface_list file to UNIX line endings.

3 Likes

Thank you so much jow. That was a correct catch.
after converting the file for unix , it worked.
i was kinda puzzled for a few hours but thanks to you :slight_smile:

If your problem is solved, please consider marking this topic as [Solved]. See How to mark a topic as [Solved] for a short how-to.

1 Like