0

Trying to use sed within eval statement?

#!/bin/bash
declare COMMAND="sudo sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config"
theResponse=$(${COMMAND} 2>&1)
echo "${theResponse}"

I cannot seem to figure out why the above script generates the following error...

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

The sed command works fine via the shell.

1 Answers1

1

Variables are not good for storing commands. Use functions for that (when necessary). Here is how your script could be written more idiomatically

#!/bin/bash
fixSshd() {
    sudo sed -i 's/^PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config
}

fixSshd

If your script is going to be run by the root user then remove sudo.

If you want to capture error output from the command, replace the last time with this

response=$(fixSshd 2>&1)
echo "Response is: $response"
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • I can't use functions, the script as actually executing 100s of commands based on a data file that will change often. I can't have a function for every possible command. So there must be a way to execute strings as commands, that allows use of sed, no? – Dachshund Digital Apr 18 '21 at 14:16
  • No, that's not reliable - as you've already found out. Variables are for data. Functions are for command sequences. Maybe edit your question to explain what you're trying to do, rather than just how you want to do it, and see if that triggers any futher responses for you. – Chris Davies Apr 18 '21 at 14:49
  • @DachshundDigital you're using the wrong language for the job. a shell wrapper around hundreds of invocations of sed is one of the worst ways of doing this (the only worse way would be trying to do it with a shell while-read loop). perl or awk or even python would be much better (as in thousands or tens of thousands of times faster, with no hassles about quoting/escaping metacharacters). The algorithm goes something like: 1. read data file into array of pre-compiled regular expressions. 2. for each file that needs to be modified, apply each regular expression in order. – cas Apr 19 '21 at 03:11
  • Yup... either using functions or different language. The 100s of commands is not limited to sed, it is pre-processor and executor for a simple system configuration tool. We can't use SaltStack or Ansible as well, which would have been easy to use. – Dachshund Digital Apr 23 '21 at 02:43
  • We have to for various reasons not add additional components to the core OS build, which I will not explain here, but the requirements of the project are set, so we are trying to test various options that we can leverage. Right now I have bash version working with some limited function prototypes (i.e. functional wrappers), but of course this gets out of hand as new commands are added to the solution. So, now we are exploring use of python3 since it is core to the OS image we are allowed to use/leverage. – Dachshund Digital Apr 23 '21 at 02:44