D Xie D Xie - 1 year ago 56
Bash Question

OpenWrt Bash shell for loop recognize slash

Bash in OpenWrt, under

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

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?



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.