Overcoming the os.system () restriction in Python 2.3

I had a problem converting one of my corporate scripts from csh to Python. The csh script calls the aliased command, but when I call the same aliased command through os.system (), it does not work.

So if foo- the aliased command:

CSH script (this works, foo runs):

foo <argument>

Python (this does not work, foo error messages are an unknown command):

os.system("foo <argument>")

I believe that when running a python script, some kind of context switch should happen, which causes Python to not have access to the aliases created in the shell. After reading the documentation, it seems that it is os.systemdeprecated in favor subprocess, and that it subprocesshas a parameter shellthat could help me ... the problem is that I am stuck using Python 2.3 and is subprocessnot available until version 2.4.

I had the idea of ​​replicating an alias in python. So, for example, let's say it foowas a pseudonym cp.

CSH:

alias foo cp
foo file1 file2

Python:

os.system("alias foo cp")
os.system("foo file1 file2")

There are no errors ...

sh: line 0: alias: foo: not found
sh: line 0: alias: cp: not found
sh: foo: command not found

My last endeavor is to take a few lines of code that they use fooand put them in my own CSH script that will call Python. But if there is a way to do this work without resorting to it, I would like to know about it.

!

+3
2

, os.system csh? C, Unix /bin/sh. csh, , , bash .

BTW: , , os.system, os.system, . , , , . , /bin/sh , csh.

, foo, - :

os.system("/bin/csh -i -c 'foo arg1 arg2'")

-i, csh .

+8

"foo" Python, os.system:

cmd = "foo file1 file2"
foo_alias = "cp"

cmd = re.sub("^foo ", foo_alias + " ", cmd)
os.system(cmd)

foo ( ..), .

0

Source: https://habr.com/ru/post/1754854/


All Articles