-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?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
TforV
  • 135
  • 7

2 Answers2

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
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Cor. Another day, another command I had assumed to be part of POSIX I learn is actually a bashism. – Konrad Rudolph Jun 16 '21 at 13:50
  • @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 Jun 17 '21 at 08:31
  • @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. – Konrad Rudolph Jun 17 '21 at 09:17
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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    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. – Sebastian Szczepaniak Jun 16 '21 at 13:18
  • That’s definitely true, and Python’s `execvp` call differs from the POSIX API’s (see https://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. – Konrad Rudolph Jun 16 '21 at 13:21