How to execute a bash script from within my python project - bash

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.

Related

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

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.

Making python script executable using anaconda

I've written a couple of scripts that I want to run automatically on my computer, so I've added the line #! Applications/anaconda/bin/python to the beginning of each script to made it executable. Oddly enough, one of the programs worked perfectly after adding that line and then running chmod +x \path\to\program.py in the terminal. But for the second program I followed the same process but when I try and run the script from the terminal using \path\to\program.py I get an error which reads:
Applications/anaconda/bin/python^M: bad interpreter: No such file or directory. I understand that this means that I have a carriage return on the end of the line that's throwing it off, but I don't understand how to get rid of it. Here are the snippets from my two scripts, this one works:
#! /Applications/anaconda/bin/python
import time
from selenium import webdriver
import easygui
.
.
.
And this one doesn't:
#! Applications/anaconda/bin/python
from datetime import datetime
import calendar
.
.
.
After doing some more digging. I found that the problem was caused because I had edited the script using both a windows computer and a mac. All that was needed to fix the problem was to copy and paste the code to a new .py file and then it worked like a charm. There are also more technical work arounds here: PyCharm. /usr/bin/python^M: bad interpreter
Your second script is missing a "/" at the beginning.
This is, replace #! Applications/anaconda/bin/python with #! /Applications/anaconda/bin/python.
You should try opening your file using vim and run in command mode
:set fileformat=unix

Python Redirection in the Shell

I have had the following problem which both Google and the Python documentation cannot aid me.
Essentially, I want to be able to perform redirection at the command-line level using the subprocess library as in the following (using a shell script) so I can perform operations in that directory:
Example:
#!/usr/local/bin/python
from subprocess import call
call("cd", "/usr/local/bin")
However, it doesn't change to that directory. Does anyone know what I am missing? I will be most appreciative of any insight that anyone can give on this.
Thanks!
The shell started by subprocess.call() changes directory. And then it exits. Your script does not change directories. Try os.chdir() instead.

Tcl script cannot be executed from bash shell script

I have a relatively strange problem with bash shell scripts and tcl scripts, invoked from within the shell scripts.
In perspective, I have a shell script that generates some other shell scripts, as well as tcl scripts. Some of the generated shell scripts invoke tcl scripts with tclsh command.
The files are created and stored in a directory, also created by the initial shell scripts, that generates the files and the folders where these are to be stored.
The problem is with the generated shell scripts, which invoke tclsh to run a tcl script. Even if the files are generated and the shell scripts have the permissions to be executed, the response from the shell is that the tcl file embedded in the shell script cannot be found.
However, the file exists and I can open it with both vi and gedit, RHEL 9.0 or Centos 5.7 platforms. But, when I take the same shell script out of the created directory, this error does not appear. Can you please suggest any idea? I checked also directory permissions, but they seem ok. I also checked the shell script for extra characters, but I did not find anything.
It's hard to tell what exactly is going wrong from your description; you leave out all the information actually required to diagnose the problem precisely. However…
You have a tclsh on your PATH, but your script isn't running despite being chmodded to be executable? That means that there's a problem with your #! line. Current best practice is that you use something like this:
#!/usr/bin/env tclsh
That will search your PATH for tclsh and use that, and it's so much easier than any of the alternative contortions.
The other thing that might be causing a problem is if your Tcl program contains:
package require Tcl 8.5
And yet the version of Tcl used by the tclsh on the path is 8.4. I've seen this a number of times, and if that's your problem you need to make sure that the right Tcl rpm is installed and to update your #! line to this:
#!/usr/bin/env tclsh8.5
Similarly for Tcl 8.6, but in that case you might need to build your own from source as well and install that in a suitable location. (Tcl 8.6 is still only really for people who are specialists.)
(The issue is that RHEL — and Centos too, which tracks RHEL — is very conservative when it comes to Tcl. The reasons for this aren't really germane to this answer though.)
Could the problem be as simple as the fact that you don't have "." in your PATH? What if instead of calling your script like "myscript.tcl" you call it like "./myscript.tcl" or "/absolute/path/to/myscript.tcl"?

Activating a VirtualEnv using a shell script doesn't seem to work

I tried activating a VirtualEnv through a shell script like the one below but it doesn't seem to work,
#!/bin/sh
source ~/.virtualenvs/pinax-env/bin/activate
I get the following error
$ sh virtualenv_activate.sh
virtualenv_activate.sh: 2: source: not found
but if I enter the same command on terminal it seems to work
$ source ~/.virtualenvs/pinax-env/bin/activate
(pinax-env)gautam#Aspirebuntu:$
So I changed the shell script to
#!/bin/bash
source ~/.virtualenvs/pinax-env/bin/activate
as suggested and used
$ bash virtualenv_activate.sh
gautam#Aspirebuntu:$
to run the script .
That doesn't throw an error but neither does that activate the virtual env
So any suggestion on how to solve this problem ?
PS : I am using Ubuntu 11.04
TLDR
Must run the .sh script with source instead of the script solely
source your-script.sh
and not
your-script.sh
Details
sh is not the same as bash (although some systems simply link sh to bash, so running sh actually runs bash). You can think of sh as a watered down version of bash. One thing that bash has that sh does not is the "source" command. This is why you're getting that error... source runs fine in your bash shell. But when you start your script using sh, you run the script in an shell in a subprocess. Since that script is running in sh, "source" is not found.
The solution is to run the script in bash instead. Change the first line to...
#!/bin/bash
Then run with...
./virtualenv_activate.sh
...or...
/bin/bash virtualenv_activate.sh
Edit:
If you want the activation of the virtualenv to change the shell that you call the script from, you need to use the "source" or "dot operator". This ensures that the script is run in the current shell (and therefore changes the current environment)...
source virtualenv_activate.sh
...or...
. virtualenv_activate.sh
As a side note, this is why virtualenv always says you need to use "source" to run it's activate script.
source is an builtin shell command in bash, and is not available in sh. If i remember correctly then virtual env does a lot of path and environment variables manipulation. Even running it as bash virtualenv_blah.sh wont work since this will simply create the environment inside the sub-shell.
Try . virtualenv_activate.sh or source virtualenv_activate.sh this basically gets the script to run in your current environment and all the environment variables modified by virtualenv's activate will be available.
HTH.
Edit: Here is a link that might help - http://ss64.com/bash/period.html
On Mac OS X your proposals seems not working.
I have done it this way. I'am not very happy with solution, but share it anyway here and hope, that maybe somebody will suggest the better one:
In activate.sh I have
echo 'source /Users/andi/.virtualenvs/data_science/bin/activate'
I give execution permissions by: chmod +x activate.sh
And I execute this way:
`./activate.sh`
Notice that there are paranthesis in form of ASCII code 96 = ` ( Grave accent )
For me best way work as below.
Create start-my-py-software.sh and pest below code
#!/bin/bash
source "/home/snippetbucket.com/source/AIML-Server-CloudPlatform/bin/activate"
python --version
python /home/snippetbucket.com/source/AIML-Server-CloudPlatform/main.py
Give file permission to run like below.
chmod +x start-my-py-software.sh
Now run like below
.start-my-py-software.sh
and that's it, start my python based server or any other code.
ubuntu #18.0
In my case, Ubuntu 16.04, the methods above didn't worked well or it needs much works.
I just made a link of 'activate' script file and copy it to home folder(or $PATH accessible folder) and renamed it simple one like 'actai'.
Then in a terminal, just call 'source actai'. It worked!

Resources