-
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? |
Beta Was this translation helpful? Give feedback.
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. |
Beta Was this translation helpful? Give feedback.
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.