-1

I try to use sed command inside a loop to replace IP address, but it is always giving me this error:

sed: -e expression #1, char 15: unterminateds' command`

even it is not missing the end slash.

Example format of the IP address: 26.236.16.233

Here is my code:

readarray thearray < /root/scripts/ipaddr.info
Filepath=/var/named/chroot/var/named/$Serverdmn

 for item in ${!thearray[@]}; do
    echo -e "IP: ${thearray[$item]}."
    echo -e "Change this IP? (y/n)"
    read Useranswer
    if [ $Useranswer = y ]; then
        echo -e "Please type the IP address:"
        read Firstipaddress

        oldipaddr=${thearray[$item]}
        new_oldipaddr=${oldipaddr%.*}.0

        newipaddr=$Firstipaddress
        new_ipaddr=${newipaddr%.*}.0            

        # do the change on all files
        sed -i "s/$oldipaddr/$Firstipaddress/g" /root/scripts/ipaddr.info
        sed -i "s/$oldipaddr/$Firstipaddress/g" /etc/application/config
        sed -i "s/$oldipaddr/$Firstipaddress/g" $Filepath
        sed -i "s/$new_oldipaddr/$new_ipaddr/g" $Filepath

    elif [ $Useranswer = n ]; then
                :
    fi
done

What will be the problem here and how to solve it?

  • 1
    I suggest that you print the values of $oldipaddr, $Firstipaddress, $new_oldipaddr, and $new_ipaddr before the calls to sed to see what you're passing to the application. Do those variables have any unexpected symbols? – Andy Dalton Dec 21 '15 at 18:18
  • No unexpected symbols on the value... – Ari Dec 21 '15 at 18:37
2

Your $oldipaddr (obtained from ${thearray[$item]}) values contain newlines read from /root/scripts/ipaddr.info, which are causing the sed commands to get split across lines, like

Please type the IP address:
1.2.3.4
sed -i s/26.236.16.233
/1.2.3.4/g /root/scripts/ipaddr.info

Try

readarray -t thearray < ipaddr.info

From help mapfile

      -t        Remove a trailing newline from each line read.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.