How to call a shell script function/variable from python?

前端 未结 6 1136
庸人自扰
庸人自扰 2021-01-19 01:56

Is there any way to call a shell script and use the functions/variable defined in the script from python?

The script is unix_shell.sh

#!/bin/bash
fun         


        
相关标签:
6条回答
  • 2021-01-19 01:59

    Yes, in a similar way to how you would call it from another bash script:

    import subprocess
    subprocess.check_output(['bash', '-c', 'source unix_shell.sh && foo'])
    
    0 讨论(0)
  • 2021-01-19 02:02

    You could separate each function into their own bash file. Then use Python to pass the right parameters to each separate bash file.

    This may be easier than just re-writing the bash functions in Python yourself.

    You can then call these functions using

    import subprocess
    subprocess.call(['bash', 'function1.sh'])
    subprocess.call(['bash', 'function2.sh'])
    # etc. etc.
    

    You can use subprocess to pass parameters too.

    0 讨论(0)
  • 2021-01-19 02:07

    No, that's not possible. You can execute a shell script, pass parameters on the command line, and it could print data out, which you could parse from Python.

    But that's not really calling the function. That's still executing bash with options and getting a string back on stdio.

    That might do what you want. But it's probably not the right way to do it. Bash can not do that many things that Python can not. Implement the function in Python instead.

    0 讨论(0)
  • 2021-01-19 02:22

    I do not know to much about python, but if You use export -f foo after the shell script function definition, then if You start a sub bash, the function could be called. Without export, You need to run the shell script as . script.sh inside the sub bash started in python, but it will run everything in it and will define all the functions and all variables.

    0 讨论(0)
  • 2021-01-19 02:24

    With the help of above answer and this answer, I come up with this:

    import subprocess
    command = 'bash -c "source ~/.fileContainingTheFunction && theFunction"'
    stdout = subprocess.getoutput(command)
    print(stdout)
    

    I'm using Python 3.6.5 in Ubuntu 18.04 LTS.

    0 讨论(0)
  • 2021-01-19 02:25

    This can be done with subprocess. (At least this was what I was trying to do when I searched for this)

    Like so:

    output = subprocess.check_output(['bash', '-c', 'source utility_functions.sh; get_new_value 5'])
    

    where utility_functions.sh looks like this:

    #!/bin/bash
    function get_new_value
    {
        let "new_value=$1 * $1"
        echo $new_value
    }
    

    Here's how it looks in action...

    >>> import subprocess
    >>> output = subprocess.check_output(['bash', '-c', 'source utility_functions.sh; get_new_value 5'])
    >>> print(output)
    b'25\n'
    
    0 讨论(0)
提交回复
热议问题