How to run command after source env shell inside bash script - bash

I am trying to run a command after setting up an environment. This command runs a python script which depends on the environment.
I have the following code:
#!/bin/bash
source ~/some/linux/env/shell
python test.py
However, the "python test.py" only runs after I exit the env shell.
I want to be able to run the "python test.py" inside this new shell env.

Firstly adding python running and interpreting directory.
#!/usr/bin/env python
And you have to give a code execution authority. You can give a permission below
chmod a+x test.py
Now you can run it from the command line.
./test.py

Related

Launch bash subshell and run commands in a script

I want to write a shell script that does the following:
Activate pipenv virtual environment
Runs mkdocs serve which starts a local dev server for my mkdocs documentation
If I do the naïve thing and put this in my script:
cd <my-docs-directory>
pipenv shell
mkdocs serve
it fails because pipenv shell "launches a subshell in the virtual environment". I need to pass the mkdocs serve command into the virtual shell (and preferably land in that same shell after running the script ).
Thanks in advance!
Answer
Philippe's answer works. Here's why.
pipenv run bash -c 'mkdocs serve ; exec bash --norc'
Pipenv allows you to run a command in the virtual environment without launching a shell:
$ pipenv run <insert command here>
bash -c <insert command here> allows you to pass a command to bash to execute
$ bash -c "echo hello"
hello
exec serves to replace current shell process with a command, so that parent goes a way and child owns pid. Here's a related question on AskUbuntu.
You can use this command :
pipenv run bash -c 'mkdocs serve ; exec bash --norc'

Activating conda env using shell script

Im trying to extract current working directory and activate conda environment using bash script. My shell script
app_path=$(dirname $(readlink -f $0))
source /conda/bin/activate $app_path;
This issue im facing is when I run the script as
sh script.sh
it is not activating the conda env
If i run with source script.sh
It is not able to recognise readlink command. How to get both of them working together?

Why this command "google-auth" works in the terminal but not from bash script?

I have installed libpam-google-authenticator and freeradius on server ubuntu 16.0405. Everything works good, except for if I use the command google-auth in bash script I get a error message "google-auth: command not found"
But the same works if I put it on terminal directly.
#!/bin/bash
google-auth
That is not a bash script.
To make it a bash script, your first line needs to include a "#" as follows:
#!/bin/bash
google-auth
Also, you need to ensure that the script is executable:
chmod +x yourscript.sh
Hopefully that will solve your problem.
As per the comments below, it seems like the command "google-auth" was an alias which wasn't being established in the child shell.

How can I run command inside virtualenv in shell script

I develop my own django-based project with pipenv.
Couple days ago I created simple bash script to speed up boring stuff.
All time I run terminal, go to the same directory and execute pipenv shell, after that I open up project in VScode code . and started python manage.py runserver for looking up my progress.
I try create script which do the same stuff but simplied, just run webber and here go (it comes from /usr/local/bin).
But I have one problem of these, I can't keep my pipenv shell running and execute python manage.py runserver at the same time. I mean when I stop Ctrl+C python server I don't receive my virtual enviroment (this happed in bash script ~ normally work fine).
However, server is start up, so it's virtual env.
I tried with pipenv run command but it doesn't get inside virtual env at all.
Script:
#!/bin/bash
cd ~/Documents/myprojects/Webber
code .
source $(pipenv --venv)/bin/activate
python manage.py runserver
My question is: How can I run command inside virtualenv in shell script and receive this subshell?
you can use the full path to the virtualenv folder instead of pipenv command. For example, if you created the virtual environment in you home directory called venv-webber:
source $HOME/venv-webber/bin/activate

Why Can't I Set Env Variables By Running A BASH Script From An Npm Script?

I have a nodejs javascript project, but I would like to set a bunch of environment variables locally. created a bash file that just exports some variables:
#!/usr/bin/env bash
export waka=flaka
export fat=booty
When I use the dot to source and run the file from the command line it works fine:
. ./env.sh
And I can see the variable has been set
echo $waka # prints "flaka"
But then I try to take this command and make it an npm script by adding it to my package.json
scripts: {
"set-env": ". ./env.sh",
...
}
and then run it:
npm run set-env
The script is run but the environment variables are not saved:
echo $waka # prints undefined (assuming you didn't already run it from command line)
So, I'm wondering why it doesn't save the envrionment variables as an npm script and if it's possible to run the bash script from an npm script in a way such that the environment variables will be saved for the rest of the command prompt session. Thanks!
npm is not a shell command; it runs in a separate process that forks another shell in order to run the command specified by set-env. env.sh is executed, but then that shell immediately exits, at which point the changes are gone (and then npm itself exits).

Resources