0

I am writing some piece of program which is started by exe.sh. The exe.sh runs all the necessary .sh and .py scripts.

The curent problem I am having is that I do not know how to access some functions which are created by shell script in python.

Here is what I am trying to do:

#shell.sh
function foo
{
    echo Foo function called
}
function bar
{
    echo Bar function called
}

And then from exe.sh:

./shell.sh
python main.py

And in main.py

import os
os.system("foo")
os.system("bar")

The problem is that I cannot access foo and bar functions with this code. What do I have to do for foo and bar to be accessible by python?

4
  • 1
    Does this answer your question? stackoverflow.com/questions/34556609/… Commented Nov 22, 2020 at 17:33
  • No. I get an error that foo/bar did not return non-zero result (subprocess exception). Commented Nov 22, 2020 at 19:09
  • Is there any reason why you can't reimplement the foo and bar shell function in python? A shell function is scoped to the shell session where you defined it. You can export a function so you can use it in a subshell. But this approach seems to me as something that is likely to cause more problems than it solves. Commented Nov 22, 2020 at 21:23
  • In a way yes, because I need some shell functions rather than calling instruction per instruction in python. Commented Nov 24, 2020 at 17:46

1 Answer 1

0

a file to hold the functions::

cat hi.env 
function foo
{
    echo Foo function called
}
function bar
{
    echo Bar function called
}

shell function accessed from python

python3.7

import subprocess
command = 'env -i sh -c "source /home/ec2-user/hi.env && foo"'
print( subprocess.getoutput(command))
Foo function called
command = 'env -i sh -c "source /home/ec2-user/hi.env && bar"'
print( subprocess.getoutput(command))
Bar function called

Load the env and execute the function in sh. new here, so not sure if the formats are proper. :)

Sign up to request clarification or add additional context in comments.

5 Comments

Hi, I am not sure I understand first command: "cat hi.env". This throws me an error. Can you explain what this is about?
hi.env is a file which contain the function foo and bar . it is similar to ur shell.sh. Including the path it is "/home/ec2-user/hi.env" which we load in python env and call the foo and bar function. You can see Foo function call and bar function called. which are actually the output of the print statement. Sorry to confuse you with a new file name
Okay, this does not seem to work. :( I am getting sh: 1: source: not found Should I change something in how I execute scripts?
If I execute this env -i sh -c "source /home/ec2-user/hi.env && foo" in terminal, it works, otherwise, if I execute it from python, it does not. :O
you might need to put the exact path of the file. if it fails, you can u show what exactly u r trying. >>> import subprocess >>> command = 'env -i sh -c "source /home/ec2-user/hi.sh && foo"' >>> subprocess.getoutput(command) 'foo execute'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.