How can I load .bash_profile when calling Process.run? - dart

I'm trying to determine the location of the 'flutter' script on a user's computer using this Dart code:
Process.run('which', ['flutter'], runInShell: true).then((results) {
print('which returned code ${results.exitCode}\n StdOut: ${results.stdout}\n StdErr: ${results.stderr}');
}
The problem is that the PATH environment has been set in the user's .bash_profile file like this:
export PATH=$PATH:/Users/mr_pink/dev/flutter/bin
but apparently the .bash_profile script doesn't get loaded when I call Process.run. How can I make sure it is loaded so that the "which flutter" command uses the correct PATH variable?

After reading man bash I found the solution was to add -l to the bash command like this:
Process.run('bash', ['-l', '-c', 'which flutter'])

Related

xonsh "which" equivalent - how to test if a (subprocess mode) command is available?

I would like to test (from xonsh) if a command is available or not. If I try this from the xonsh command prompt:
which bash
Then it works:
user#server ~ $ which bash
/usr/bin/bash
But it does not work from xonsh script:
#!/usr/bin/env xonsh
$RAISE_SUBPROC_ERROR = True
try:
which bash
print("bash is available")
except:
print("bash is not available")
Because it results in this error:
NameError: name 'which' is not defined
I understand that which is a shell builtin. E.g. it is not an executable file. But it is available at the xnosh command prompt. Then why it is not available inside an xonsh script? The ultimate question is this: how can I test (from an xonsh script) if a (subprocess mode) command is available or not?
import shutil
print(shutil.which('bash'))
While nagylzs' answer led me to the right solution, I found it inadequate.
shutil.which defaults to os.environ['PATH']. On my machine, the default os.environ['PATH'] doesn't contain the active PATH recognized by xonsh.
~ $ os.environ['PATH']
'/usr/bin:/bin:/usr/sbin:/sbin'
I found I needed to pass $PATH to reliably resolve 'which' in the xonsh environment.
~ $ $PATH[:2]
['/opt/google-cloud-sdk/bin', '/Users/jaraco/.local/bin']
~ $ import shutil
~ $ shutil.which('brew', path=os.pathsep.join($PATH))
'/opt/homebrew/bin/brew'
The latest version of xonsh includes a built-in which command. Unfortunately, the version included will emit an error on stdout if the target isn't found, a behavior that is not great for non-interactive use.
As mentioned in another answer, which exists in the current version of xonsh (0.13.4 as of 15/12/2022) so your script would work. However, it outputs its own error message so it's necessary to redirect stderr to get rid of it.
Also, unless you redirect its stdout as well (using all>), it migh be a good idea to capture its output so the final version would look like this:
#!/usr/bin/env xonsh
$RAISE_SUBPROC_ERROR = True
try:
bash = $(which bash err> /dev/null)
print(f"bash is available: {bash}")
except:
print("bash is not available")

How to set environment variable in Mac 10.14.6 Mojave with 'Application Support' in pathfile?

I'm having trouble setting an environment variable that has a pathfile containing a space ' ' character.
Before you ask, I've already tried enclosing the whole pathfile within double quotes, single quotes, no quotes but escaping with backspace.
Could it be the something to do with the encoding? The variable would be:
export A_MEDIA="/Users/polo/Library/Application Support/Anki2/me/collection.media"
once I source ~/.bash_profile, I try cd $A_MEDIA (with or without quoting the name of the variable). The response is:
-bash: cd: /Users/polo/Library/Application: No such file or directory
It's as if bash didn't know how to interpret that space between 'Application' and 'Support'. It thinks the path goes from a folder named Application to a folder named Support. It just doesn't see them as a single folder name. Any help? Please?
Works for me so I strongly suspect what you showed us in your problem statement is not what you're actually doing. I normally use fish so this shows me setting the env var before starting bash to show that it correctly inherits the var and also setting and using it inside bash:
12:21 macbook opencv3 ~ > set -x A_MEDIA $HOME/Library/Application\ Support/Dock/
12:22 macbook opencv3 ~ > bash
running .bashrc
bash-5.0$ cd $A_MEDIA
bash: cd: too many arguments
bash-5.0$ cd "$A_MEDIA"
bash-5.0$ pwd
/Users/krader/Library/Application Support/Dock
bash-5.0$ export B_MEDIA="$HOME/Library/Application Support/Gitter"
bash-5.0$ cd $B_MEDIA
bash: cd: too many arguments
bash-5.0$ cd "$B_MEDIA"
bash-5.0$ pwd
/Users/krader/Library/Application Support/Gitter
bash-5.0$ exit
Note that in a POSIX shell like bash you should almost always use double-quotes around a var expansion so that if it contains whitespace the expanded value is not split on that whitespace.

Working bin/bas returns error when executed from run script Xcode

I have a working shell script that works with no problems but when I executed it with a run script i get the following error:
The script will scan all the proto files in a directory and convert it to Swift using ProtoBuf. After that I will move the swift files into an App folder. The script code is the following
#!/bin/bash
for protoFile in ./*.proto;
do
protoc --swift_out=. $protoFile
done
for file in ./*.swift;
do
mv $file ../Convert\ AV/Model/USBDongle/Proto/
done
Any ideas?
Thank you
I was calling the script from the directory it was located. When Xcode executes the the script that assumption is false. So I am getting the directory where the script is located and I do the logic with that path
#!/bin/bash
#Get Directory where the script is located
baseDirectory=$(dirname "$0")
echo "$baseDirectory"
for protoFile in "$baseDirectory"/*.proto
do
echo $protoFile
protoc --swift_out="$baseDirectory" -I "$baseDirectory" "$protoFile"
done
for protoFileSwift in "$baseDirectory"/*.swift;
do
echo $protoFile
mv "$protoFileSwift" "$baseDirectory"/../Convert\ AV/Model/USBDongle/Proto
done
* /* Makes like is a comment... *

Generated file in Crontab is empty

I need help with this one. An Artisan command is being run by crontab to analyze a logfile and generate a report into an HTML format.
Here is the line of code in the command that is being executed by crontab:
$today = date("d/M/Y");
exec('grep "$today" /path_to_logfile | goaccess -a > /path_of_generated_HTML_file');
exit();
Unfortunately, the file being generated by cron is empty but when you execute the same code directly in the terminal, it successfully generates an html file. I appreciate your help guys.
This is most likely because your PATH is configured incorrectly when running under crontab. For most implementations of crontab, you can simply specify the PATH on the first line on the crontab file opened with crontab -e:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
You could also set the path inside PHP if you so desire:
<?php
putenv('PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin');
$today = date("d/M/Y");
exec('grep "$today" /path_to_logfile | goaccess -a > /path_of_generated_HTML_file');
exit;

Sublime Text 2: custom PATH and PYTHONPATH

I'm using brew which installs python (2.7.2) in /usr/local/bin/
However, the default system python (2.7.1) is executed instead at /usr/bin/, which seems to be because it doesn't obey any of the bash PATH environment variables.
Also, it can't find my modules, as they are installed at /usr/local/lib/python:/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages.
I've been trying the following with Python.sublime-settings, but it doesn't work:
{
"path": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin",
"env": ["PYTHONPATH", "/usr/local/lib/python:/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages"],
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
How can I make Sublime Text obey these environment variables?
env needs to be a JSON object, or dictionary if you will, like this:
"env":
{
"PYTHONPATH":"/usr/local/lib/python:/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages"
},
I got it by setting my paths system wide by doing the following:
## PATH
export PATH=/usr/local/bin:/usr/local/share/python:$PATH
## PYTHON
export PYTHONPATH=/usr/local/lib/python:$PYTHONPATH
# make systemwide
launchctl setenv PATH $PATH
launchctl setenv PYTHONPATH $PYTHONPATH
Edit:
Damn, this doesn't work for python, just for PYTHONPATH, when I try it, it still gives the wrong python. Code used to check python binary location:
import sys, os
print os.path.dirname(sys.executable)
Edit2:
Fixed this by hardlinking to the right python binary in Python.sublime-build:
{
"cmd": ["/usr/local/bin/python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Edit 3:
Debugging PYTHONPATH variable issues can be made easier by also printing os.environ at the top of your script. Sublime Text 2 variable names apparently do NOT work for 'env'.
Sorry to bump an old post but if people land on this page looking for a way to make sublime2 use a custom $PATH so plugins (e.g a shell plugin) use your current systems $PATH this worked for me:
Create a file (plugin):
~/Library/Application Support/Sublime Text 2/Packages/User/Any_ol_name.py
Then paste this code in:
import os
# Tweak line below as needed for your $PATH
LOCAL = '/usr/local/bin:/usr/local/sbin'
# Sublime's default path is
# /usr/bin:/bin:/usr/sbin:/sbin
# it'll be prepended to your custom one
os.environ['PATH'] += ':'
os.environ['PATH'] += LOCAL
print 'PATH = ' + os.environ['PATH']
Post with the original code here..
This plugin will load when you start Sublime Text 2, I personnally used it to run shell commands like I would from terminal and to fix a few plugins that werent loading due to bad path variable.
This is a super old post but I landed here looking for this solution for Sublime Text 3. Just in case people land here as well, the quick solution is to go into Preferences.sublime-settings and add:
"additional_path_items": [
"/path/to/add/1",
"/path/to/add/2",
],

Resources