So basically, I'm looking for a way to make the below bash script work without an issue. Each of the commands actually works when tested individually, it's only in the actual script it has a problem. The code is below:
#!/bin/bash
answer="$(/usr/bin/grep "PermitRootLogin yes" /etc/ssh/sshd_config)"
if [ $answer ]; then
(perl -pi -e \'s/\"PermitRootLogin yes\"/\"PermitRootLogin no\"/g\' /etc/ssh/sshd_config)
else
echo "Root login is already not permitted, no values changed."
fi
Here's what happens when I try to run it:
test.sh: line 9: [: PermitRootLogin: unary operator expected
Root login is already not permitted, no values changed.
So basically, rather than it doing the find/replace as I want it to on the file via the perl command, it's trying to interpret it as a separate instruction.
All it is attempting to do is look at the /etc/ssh/sshd_config file and then change the relevant entry from yes to no if the "answer" is found to be yes.
I'm basically looking for a way to make this work. I've looked at a lot of bash examples on this site but none of them seem to cover this specific case.
Also, the reason I'm using perl is that sed works very oddly on Solaris 11, or isn't installed on extremely old versions, and perl is installed on every version from 8 forward.
Any ideas?
[test command. FIxing that will show problems with the Perl. There's no obvious reason for using a sub-shell. And the quoting is bizarre and won't work. You need something like:perl -pi -e 's/"PermitRootLogin yes"/"PermitRootLogin no"/g' /etc/ssh/sshd_config(which assumes that the double quotes appear in the config file; if they don't, simply remove the double quotes from the command line).