How can I load a module from inside a bash script? - bash

I'm trying to get a script running. Essentially I need to load some modules and then execute a python script. So something like this...
#!/usr/bin/env bash
module use /PATH/TO/MODULEFILE
module load MY_MODULE
python my_script.py
The problem that I get is that every time I try to execute the script I get this error - "module: command not found". I don't understand how to change the script to fix this. I am able to run the module command from the shell. I also ran "whereis -b module" and I just got "module:".
What do I need to do to get this module loaded from inside the script? Thanks

You can enable the module command from your python script by sourcing the python.py initialization script provided in the init directory of the Modules installation.
Following piece of python code defines module function, then enables a modulepath and load a modulefile:
#!/usr/bin/python
import os
exec(open('/PATH/TO/MODULES/init/python.py').read())
module('use', '/PATH/TO/MODULEFILE')
module('load', 'MY_MODULE')

If you want to enable the module within your bash script, it could be as simple as using a different command to run the script. For me, running a script script.sh as . script.sh or as ./script.sh will correctly load the module, but running it as sh script.sh will lead to the error you described above.

Related

How to run SDKMAN on shell script

I am trying to run sdkman on a shell script that I call run.sh. This is what the inside of the shell script looks like:
sdk use java 8.0.302-open
When I run the command in a terminal, it works. But when I run it in a shell script, I get this error:
run.sh: 1: sdk: not found
Anyone knows how to fix this?
I fixed it for me; Although this may not work for others.
I placed #!/bin/bash at the top of the shell script, and then added this after it:
source "$HOME/.sdkman/bin/sdkman-init.sh"
Then my shell script was able to be ran using:
./run.sh
sdkman was able to work this time.

Running Python Function from Bash

I have created a function configure_propeties() in configure.py that I'm trying to call in a bash script using the following command:
python3.6 -c "import configure; print configure.configure_properties()"
After executing the the bash script I get the following warning:
warning
Both the script and configure.py are in the same directory. Not sure what's happening it doesn't seem to fail on the import. Has anyone seen this issue before?
print "blabla" its only available in python 2, with python 3 you have to use parentheses print("blabla"), it would be better to set your python script inside .py file, just make sure the python executable is in your PATH environment variable then add in your script
python3 path/to/the/python_script.py
In the bash script file bashFile.sh, put this :
#!/bin/bash
python3 path/to/the/python_script.py
then you can execute your bash file bashFile.sh :
bash path/to/bashFile.sh
Use parentheses for print, as is necessary for Python 3

Python (3.5.2, Anaconda custom [64-bit]) Error Loading Shared Libraries (libintlc.so.5)

I'm running a bash script using qstat. It runs fine going line-by-line in an interactive shell (i.e. /bin/bash) and if I run it as bash test_script.sh. However, when I submit it via qstat as qstat test_script.sh, I get an error along the lines of:
python: error while loading shared libraries: libintlc.so.5: cannot open shared object file: No such file or directory
The file is set up with the required PBS directives and the bash is as follows:
source ~/.bashrc
module load /path/to/anaconda/python/version
cd ~/path/to/files/
python python_file.py > python_logfile.out
I'm running out of things to check here, so any advice would be very much appreciated! Thanks!

How to execute a bash script from within my python project

I am writing some automation to control a simulator. I am also using WebDriverAgent along with openatx python bindings in pycharm. There are some things that are fast using cURL in a bash script and some methods that are stronger in the python solution. I want to use a mix of both. I have imported bashSupport into pycharm and would like to execute a bash script from within my file system of the project.
I have tried subprocess import and os but it doesn't seem to be executing my script. Here is an example:
import subprocess
subprocess.call(['launch_wda.sh'])
with device.session('com.apple.mobilesafari') as app:
print app.orientation
app(label="Address").tap()
app(label="Address").set_text("facebook.com \n")
the launch_wda.sh is in my file structure of the project. Is my syntax incorrect or am I missing something else?
Try these changes:
Add ./ before script name. As this could be some path related issue.
Try adding shell=True if you are not under Posix system and you do not have something like this #!/bin/sh as a first line of your bash script.
Last but not the least, check the permissions for the bash script.
Hope this helps.

Using Bash to run 2 python scripts at the same time

I want to run two Python Scripts at the same time. I found that using Bash you can do that. So i wrote the next code
#! /usr/bin/env bash
import camera_centroid
import testsss
python camera_centroid.py &
python testsss.py &
When i run it i get a SyntaxError: invalid syntax
Why?
Looks like you have mixed between python and bash,
you don't need the import in the bash script.
#!/usr/bin/env bash
python camera_centroid.py &
python testsss.py &
wait # wait for jobs to be done
make sure you adding execute permissions to the scripts
chmod +x testsss.py camera_centroid.py
and finally run the script ./your_file.sh
When you write:
import foo
in a shell script, you are not importing the python module, instead, you are calling the $(which import) command/alias/function.
For example, if you have ImageMgick installed, very likely, you are making screenshot for window(s).
If you want to import python module, those import foo should be in your python files.

Resources