Not sure if this is the right place to ask but I couldn't really think of any others.
I have a script that searches for a string in a file. If it finds the string, it replaces the string with a variable in the script. The variable in the script can (but doesn't mean it will) contain multiple words separated by a space. If there is more than one word in the variable, I would like to add each word to the file, after the first one is added. I will show code and give an example of my expected output:
Code: Select all
IP_ADDR=$( grep -o -E '192\.168\.2\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\b' fake_ips | tr '\n' ' '|sed -e 's/[[:space:]]*$//' )
SSHD_CONFIG="sshd_config.test"
X=19
function update_sshd_config() {
EXPANDED_IP_ADDR="$(echo ${IP_ADDR// /$'\n'})"
for IP_ADDRESSES in ${EXPANDED_IP_ADDR}
do
grep -q -F "ListenAddress ${IP_ADDRESSES}" ${SSHD_CONFIG} || sed -i "0,/#ListenAddress .*/s//ListenAddress ${IP_ADDRESSES}/" ${SSHD_CONFIG}
X=$((${X}+1))
done
}
update_sshd_config
This script of mine does not work properly. It replaces #ListenAddress 0.0.0.0 with the first IP address, but then it replaces #ListenAddress :: with the second and stops. This is not what I want. I want to replace #ListenAddress 0.0.0.0 with the first IP address, and then append ListenAddress <second IP> etc on new lines afterwards.
Any ideas how to accomplish this? If there is a better place to ask, I am okay with suggestions.