Reputation: 2368
I've a simple script to set several parameters in /etc/ssh/sshd_config :
#! /bin/bash
declare -a param=('Banner' 'ClientAliveInterval' 'ClientAliveCountMax' 'Ciphers' \
'PermitUserEnvironment' 'PermitEmptyPasswords' 'PermitRootLogin' \
'HostbasedAuthentication' 'IgnoreRhosts' 'MaxAuthTries' \
'X11Forwarding' 'LogLevel'\
)
declare -a val=('/etc/issue.net' '300' '0' 'aes128-ctr,aes192-ctr,aes256-ctr' \
'no' 'no' 'no' 'no' 'yes' '4' 'no' 'INFO' \
)
for (( i=0;i<12;i++ ))
do
#echo "${param[$i]} ${val[$i]}"
egrep "^[ #]*${param[$i]}.*" /etc/ssh/sshd_config &> /dev/null
if [ $? -eq 0 ];
then
sed -i "s|^[ #]*\$param[$i].*|${param[$i]} ${val[$i]}|1" /etc/ssh/sshd_config
else
echo "${param[$i]} ${val[$i]}" >> /etc/ssh/sshd_config
fi
done;
However the variable expansion in sed pattern match is not working as desired:
sed -i "s|^[ #]*\$param[$i].*|${param[$i]} ${val[$i]}|1" /etc/ssh/sshd_config
Can someone help me. My array expansion and everything in the script is fine though. I've checked the same with an echo
printout.
Upvotes: 2
Views: 158
Reputation: 785256
Not sure why you have $
escaped, and to access array element you need to use ${param[$i]}
.
You can use:
sed -i "s~^[ #]*${param[$i]}.*~${param[$i]} ${val[$i]}~1" /etc/ssh/sshd_config
btw ^[ #]*
will only match space or #
at line start.
Upvotes: 1