-2

I have to open a .sh file with a python GUI developed with PyQt5. So I implemented the command:

def function_openSH(self):
    subprocess.call('chmod u+x ./script_open.sh', shell=True)
    subprocess.call('./script_open.sh', shell=True)

It gives me: ./script_open.sh: 5: source: not found. Why?

10
  • 1
    @TforV What’s inside script_open.sh? That’s where the error occurs. Can you run the file manually on the command line? Commented Jun 16, 2021 at 13:09
  • 1
    ./ means "this directory". It is likely that the subprocess you start has a different idea of what that means. Commented Jun 16, 2021 at 13:12
  • 1
    @BoarGules No, see my comment reply to S3DEV. Commented Jun 16, 2021 at 13:12
  • Dear all, the script works but it gives the same error: very strange Commented Jun 16, 2021 at 13:15
  • @KonradRudolph yes I can but that's not the problem Commented Jun 16, 2021 at 13:17

2 Answers 2

3

subprocess with shell=True specifically uses /bin/sh. If this is not a symlink to bash, then you may be using a POSIX compliant shell (such as ), then the source command is not available:

$ cat > foo.sh
echo hello world

$ bash -c 'source ./foo.sh'
hello world

$ /bin/sh -c 'source ./foo.sh'
/bin/sh: 1: source: not found

$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Aug  7  2020 /bin/sh -> dash

$ python
Python 2.7.16 (default, Oct 10 2019, 22:02:15) 
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call('source ./foo.sh', shell=True)
/bin/sh: 1: source: not found
127

Since you explicitly made your script executable, use shell=False


Or, use the POSIX sh . command instead of the bash-specific source.

>>> subprocess.call('. ./foo.sh', shell=True)
hello world
0
Sign up to request clarification or add additional context in comments.

3 Comments

Cor. Another day, another command I had assumed to be part of POSIX I learn is actually a bashism.
@glennjackman: I'm pretty sure that the OP knowingly does use a pure POSIX shell: He tagged his question simply with shell, and never mentions a different shell (such as ksh,...) anywhere.
@user1934428 Exceedingly unlikely. If OP knowingly used a pure POSIX shell they’d have known to add the shebang line. Furthermore, they clearly do use a different shell, since their script uses bashisms.
1

My guess would be that you did not specify which shell to use e.g. #!/bin/bash or that you don't define the explicit path of the .sh file.

2 Comments

Yeah, thats true, but I kinda remember there were issues with .sh when not specifying which shell to use, were /bin/sh was used. That issue produced a ton of broken scripts in Ubuntu.
That’s definitely true, and Python’s execvp call differs from the POSIX API’s (see stackoverflow.com/q/62893701/1968). But this isn’t relevant here: OP’s code works with a script without shebang line if all it does is invoke source on an existing file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.