I am trying to run a sed command within a script to edit a file.
I am trying to put the value of MYUSER into the sshd_config file.
Instead of putting the value of the variable, MYUSER, it puts in the string ${MYUSER}.
Anyone know a good solution to this?
cat ${SSHD_CONFIG} | sed '/^DenyUsers/s/$/ ${MYUSER}/g' > ${SSHD_CONFIG}
Thank you
cat ${SSHD_CONFIG} | sed '/^DenyUsers/s/$/ ${MYUSER}/g' >
Firstly this is a usless use of cat (UUoC). There is no need for cat with a sed command:
sed '/^DenyUsers/s/$/ ${MYUSER}/g' ${SSHD_CONFIG} > some_other_file
or
sed '/^DenyUsers/s/$/ ${MYUSER}/g' < ${SSHD_CONFIG} > some_other_file
If the version of sed you are using supports the -i option then you are very much in luck:
sed -i '/^DenyUsers/s/$/ '${MYUSER}'/g' ${SSHD_CONFIG}
if not:
sed to a new file and copy to ${SSHD_CONFIG}
printf "s/\(^DenyUsers.*\)/\\\1 ${MYUSER}\nwq\n" | ex ${SSHD_CONFIG}
perl -pi -e '$_ =~ s/(DenyUsers.*)/\1 '${MYUSER}'/' ${SSHD_CONFIG}