D Xie D Xie - 7 months ago 21
Bash Question

OpenWrt Bash shell for loop recognize slash

Bash in OpenWrt, under

/etc
i have a file foreign.txt, with the content as below:
1.0.0.0/24‚Äč

When i use
for i in ``cat /etc/foreign.txt``; do ipset add redir $i;done
,the / is treated as', shows '24 numerror.

But when i try
for i in ``cat /etc/foreign.txt``; do echo $i;done
it shows the correct /.

How can the shell treat it correctly in the ipset command?

Thanks.

Answer

There are multiple special characters after /24 in your question. See output of this command:

wget -qO - 'http://stackoverflow.com/revisions/36fdcac2-c6a6-4dd8-aad4-75af2d16827e/view-source' | cat -v

I suggest to use:

tr -cd '0-9./\n' </etc/foreign.txt | while read -r i; do echo ipset add redir "$i"; done

If everything looks okay remove echo.


This tr command removes every character from /etc/foreign.txt but 0 to 9, dot, slash and newline.