Making Sure Your Script is Ready for Execution
- Give your script a shebang line
First things first, it is important that you include a shebang line in your script on Unix-like systems. I recommend, for your script's portability, that you use #!/usr/bin/env bash
.
A Note on File Extensions:
I would recommend that you remove the .sh
extension from your script. If you are using the Bourne Again Shell (bash
) to execute your script, then using the .sh
extension is misleading. Simply put, the Bourne Shell (sh
) is different than the Bourne Again Shell (bash
) - so don't use a file extension that suggests you are using a different shell than you actually are!
It's not the end of the world if you don't do change your file extension - your script will still be executed as a bash
script if you have the proper bash
shebang line. Still, it is good practice to either use no file extension (Script_Test
-- strongly preferred) or the .bash
file extension (Script_Test.bash
).
- Give your script the proper file permissions
For your purposes, maybe it is only important to give the current user permissions to read and execute the script. In that case, use chmod u+x Script_Test.sh
. What is important here is that the correct user (u+
) / group (g+
) has permissions to execute the script.
- Make sure that your script's path is in the
$PATH
environment variable
Executing your bash
script in a Python script
Once you've followed these steps, your Python script should work as you've called it in your question:
import subprocess
from subprocess import call
your_call = call("Test_Script.sh")
If you would rather not move your script into the $PATH
environment variable, just make sure that you refer to the script's full path (which is the current directory, ./
, in your case):
import subprocess
from subprocess import call
your_call = call("./Test_Script.sh")
Lastly, if your script does not have a shebang line, you will need to specify an additional parameter in your call
function:
import subprocess
from subprocess import call
your_call = call("./Test_Script.sh", shell=True)
However, I would not recommend this last approach. See the Python 2.7.12 documentation for the subprocess
package...
Warning: Using shell=True
can be a security hazard. See the warning under Frequently Used Arguments for details.
Please check out @zenpoy's explanation to a similar StackOverflow question if you are still having problems.
Happy coding!