0
#!/usr/bin/env python3

from subprocess import run
from sys import modules

def main():
  doas()

def doas():
  ch = input("Ready to proceed with setting up doas?[Y/n]?")
  if ch == 'y':
    run("sudo passwd", shell=True)#If you would prefer to access the root account with su or by logging in directly, you must set a root password with "sudo passwd".
    run("sudo apt install -y doas", shell=True) 
    run("""echo -e "permit $(whoami) as root 
permit root as $(whoami)
permit nopass root as $(whoami)"|sudo tee /etc/doas.conf""", shell=True) and run("doas apt -y purge sudo", shell=True)
    print("doas set up.")
  else:
    print("Run me again when you are ready.")

The problem with this code is that it places the option -e in doas.conf, like so:

-e permit jim as root
permit root as jim
permit nopass root as jim

while I need doas.conf to look like so:

permit jim as root
permit root as jim
permit nopass root as jim

I know that the easiest quick fix is simply not to use -e with echo. Is there any other way I can resolve this issue?

Why does -e end up written into doas.conf if it is not enclosed within double quotes? When I run

echo -e "permit $(whoami) as root 
permit root as $(whoami)
permit nopass root as $(whoami)"|tee doas.conf

from my shell (Bash), then all works fine and I get the desired result. Why is it different from Python? I'm on Ubuntu 22.04 LTS, and my echo seems to support -e, e.g. echo -e "a\nb\nc" it prints them letters with newlines.

John Smith
  • 561
  • 2
  • 14

1 Answers1

0

Because different implementations of echo either support or don't support options like -e and -n, and either process or don't process C-style backslash-escapes like \n and \t.

A standard echo does not take options:

The echo utility shall not recognize the "--" argument in the manner specified by Guideline 10 of XBD Utility Syntax Guidelines; "--" shall be recognized as a string operand.

Implementations shall not support any options.

Use printf instead. See: Why is printf better than echo?


Quotes (double or single) have nothing to do with it, as they are processed by the shell before the command runs and gets a chance to process its options. In all of

echo -e foo
echo "-e" foo
echo '-e' foo

the echo command sees the two arguments -e and foo.

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
  • I'm on Ubuntu 22.04 LTS, and my `echo` seems to support `-e`, e.g. `echo -e "a\nb\nc"` it prints them letters with newlines. – John Smith May 22 '23 at 08:24
  • @JohnSmith, well. The one that your Python script starts obviously doesn't. The point is that there's no single `echo`, but that every implementation is potentially different. So, don't use `echo` if you care about the output being what you want, there's a better and more compatible alternative available in `printf`. – ilkkachu May 22 '23 at 08:37
  • 2
    @JohnSmith, if you want to go there, you probably have not one, but three `echo` implementations on Ubuntu: the one in Bash, the one in Dash, and the one in `/bin/echo`. And yes, they're all different. – ilkkachu May 22 '23 at 08:39
  • 2
    @ilkkachu four, with busybox usually being installed by default too. – muru May 22 '23 at 09:54