Home > Blockchain >  A shell script initiated by Python's os.system fails to run, but the script do run when called
A shell script initiated by Python's os.system fails to run, but the script do run when called

Time:09-14

I have a Python3 script that needs to call a shell script with some parameters. When I call this shell script directly form the terminal - it works. The shell script call from terminal:

source $HW/scripts/gen.sh -top $TOP -proj opy_fem -clean

But when I try to call the shell script exactly the same way from Python 3 using os.system (or os.popen - same result), the shell script fails to run. Python call to the shell script:

os.system("source $HW/scripts/gen.sh -top $TOP -proj opy_fem -clean")

Get the next errors:

/project/users/alona/top_fabric_verif_env/logic/hw/scripts/gen.sh: line 18: syntax error near unexpected token `('
/project/users/alona/top_fabric_verif_env/logic/hw/scripts/gen.sh: line 18: `foreach i ( $* )'

Could you please shed light on why the same shell script fails to run from Python? Thank you for any help

CodePudding user response:

foreach is a C-shell command. csh (and derivates like tcsh) are not standard system shells in Unix/Linux.

If you need to use a specific shell, for instance the C-shell:

os.system('/bin/csh -c "put the command here"')

This will execute the /bin/csh in the standard shell, but starting two shells instead of one creates an additional overhead. A better solution is:

subprocess.run(['/bin/csh', '-c', 'put the command here'])

Note that using the shell's source ... command does not make much sense when the shell exits after the command.

  • Related