|
e.g. cd var/backup/files && tar cf `date "+%Y-%m-%d"`_files.tar /some_path/news/ /some_path/media/ && ls -tp | grep -v "/$" | tail -n +21 | xargs -I {} rm -- {}I would now start to use import shlex
cur_cmd: list[str] = []
for item in shlex.split(long_cmd):
if item in ("&&", ";"):
assemble_sh_cmd(cur_cmd)
cur_cmd = [item]
continue
elif item in ("|", "||"):
assemble_sh_cmd(cur_cmd, is_pipe=True)
cur_cmd = [item]
continue
cur_cmd.append(item)Is there not a much better way to handle such cases? |
Answered by
amoffat
Sep 18, 2023
Replies: 1 comment 1 reply
|
The short answer is no. Acting like a full shell is outside of the scope of the project, even though it does include some shell utilities. Sh is primarily for command execution. I would convert the trivial parts to Python and execute the non-trivial parts with sh. The example that you gave is all trivial, as Python can list directories, do regex comparisons, remove files, and tar files. If parts of the command called binaries that are not portable to Python, that's where you would use sh. |
1 reply
Answer selected by
sla-te
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The short answer is no. Acting like a full shell is outside of the scope of the project, even though it does include some shell utilities. Sh is primarily for command execution.
I would convert the trivial parts to Python and execute the non-trivial parts with sh. The example that you gave is all trivial, as Python can list directories, do regex comparisons, remove files, and tar files. If parts of the command called binaries that are not portable to Python, that's where you would use sh.