0

I have a bash script in which I have a function like this

configure()
{
    echo $1
    echo $2
    return 0
}

And I am calling this function directly like this from my python file

subprocess.Popen(
            ['bash', ' -c', '. test.sh; configure 1 2']) 

now I want to get return value from this function and in case there is some error I want to catch that as well.

TNN
  • 391
  • 1
  • 5
  • 12
  • You aren't passing `configure 1 2` to your script at all. You're passing that string _to bash_, but not telling bash to pass it on _to your script_. – Charles Duffy Nov 16 '20 at 21:08
  • Compare it to `['bash', ' -c', '. test.sh; configure 1 2']` -- both commands need to be included in the argument _immediately after `-c`_ to be considered part of the script bash is supposed to run. The next argument after that goes into `$0`, the one after that into `$1`, etc -- but the script you're telling bash to run ignores `$0`, `$1`, etc. entirely. – Charles Duffy Nov 16 '20 at 21:08
  • You should figure out how to run your bash script to get the output you want first, and only get Python involved after – that other guy Nov 16 '20 at 21:09
  • BTW, it should be `echo "$1"` _with the quotes_; see [I just assigned a variable, but `echo $variable` shows something else](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else). – Charles Duffy Nov 16 '20 at 21:10
  • If you're going to call `bash -c`, you might as well just pass the whole string to `subprocess.Popen()` and use the `shell=True` option. – Barmar Nov 16 '20 at 21:11
  • @Barmar, ...well, there's a reason not to do that if one really wants _bash_ and not `sh` as the shell. – Charles Duffy Nov 16 '20 at 21:12
  • @FawadRana, ...now that you changed your code to fix the bugs, show us why/how you still have a problem. – Charles Duffy Nov 16 '20 at 21:14
  • @CharlesDuffy I got it , but my main issue is I want to get output in my python script . how I can do this ? – TNN Nov 16 '20 at 21:14
  • @FawadRana, gotcha. If this is a new enough version of Python, I'd suggest switching to `subprocess.run` and using `capture_output=True`. – Charles Duffy Nov 16 '20 at 21:15
  • @CharlesDuffy can you please give a working example ? I am new to bash scripting . dont have much idea – TNN Nov 16 '20 at 21:16
  • There are working examples in the answers to the linked duplicates -- see the links up at the top of your question. (I already removed the duplicate associated with the bug you fixed from the list). – Charles Duffy Nov 16 '20 at 21:17
  • @CharlesDuffy they are with `subprocess.run` while I am doing `subproccess.Popen` – TNN Nov 16 '20 at 21:18
  • @FawadRana, correct. I suggest that you switch from `Popen` to `run`. – Charles Duffy Nov 16 '20 at 21:19
  • ... `Popen` itself doesn't (and can't) read output, because it's non-blocking. You can call `communicate()` on a Popen object (if you used `stdout=subprocess.PIPE` and/or `stderr=subprocess.PIPE`) to collect the output, but if you do, _that_ will then block (not returning until that output has actually been written by the subprocess for the parent process to be able to collect it). – Charles Duffy Nov 16 '20 at 21:20
  • (Similarly you can do other blocking calls, like `your_popen_object.stdout.read()`, but the end result is that either way you end up blocking, just like `subprocess.run` does, unless you're going to do the effort to spin off threads, or coalesce your file descriptors into a `select()`-style async framework or the like; it's a lot more code, and you need to have a good reason to do it to make it worthwhile. People who _have_ had such good reasons have asked questions on the site already, and those questions have been answered, so you can find them in the knowledge base). – Charles Duffy Nov 16 '20 at 21:23
  • @CharlesDuffy I have a large script and I want wherever I get error it should return in my python script so that I can show user – TNN Nov 16 '20 at 21:25
  • That's fine. You can do that with `subprocess.run`, _or_ with `subprocess.Popen` and `communicate()`; the linked answers apply. – Charles Duffy Nov 16 '20 at 21:29
  • @CharlesDuffy thanks till then, working fine but getting output in byte and when I convert it to string using decode('utf-8') its including special character etc and other so many things – TNN Nov 17 '20 at 10:23
  • Surely not for the sample program you provided here. We'd need a new question with a [mre] that demonstrates the problem to be able to speak to it. – Charles Duffy Nov 17 '20 at 14:10
  • ...that said, my first guess would be that the program you're running doesn't turn off terminal control sequences when writing to a non-TTY sink. If so, that's something you should be able to report to its author as a bug. (If it's well-behaved enough to honor the `TERM` environment you could set it to `dummy`, but a program that doesn't check `isatty()` may not honor `TERM` either). – Charles Duffy Nov 17 '20 at 14:10

0 Answers0