I am having a shell .myenv
which is calling another shell testenv.sh
.myenv
#!/usr/bin/env sh source ./testenv.sh
testenv.sh
#!/usr/bin/env bash if [[ "$OSTYPE" == "linux-gnu" ]]; then echo 'Linux detected' python_binaries_path='/usr/local/bin:/usr/bin:/bin' elif [[ "$OSTYPE" == "darwin"* ]]; then echo 'MacOS detected' python_binaries_path='/opt/local/bin:/usr/local/bin:/usr/bin:/bin' else echo "Cannot determine OS type: ${OSTYPE}" exit 1 fi python_binaries='python3.7 python3 python' python_path=$(PATH=${python_binaries_path} which ${python_binaries} | head -n 1) || true echo "python_path=$python_path" python_major_version=$(${python_path} -c 'import platform; major, minor, patch = platform.python_version_tuple(); print(major)')
echo "python_major_version=$python_major_version" echo "python_binaries_path=$python_binaries_path"
When I do source .myenv
, it's not able to detect the python_major_version
Linux detected
python_path=alias python='python3.7'
-bash: alias: -c: not found
-bash: alias: `import platform; major, minor, patch ': invalid alias name
python_major_version=
python_binaries_path=/usr/local/bin:/usr/bin:/bin
When I do sh testenv.sh
, it is able to detect the python_major_version
Linux detected
python_path=/usr/local/bin/python3.7
python_major_version=3
python_binaries_path=/usr/local/bin:/usr/bin:/bin
Below line is creating problem and I am running these scripts on Centos7.
python_major_version=$(${python_path} -c 'import platform; major, minor, patch = platform.python_version_tuple(); print(major)')
But this script is working fine in Google cloud shell which obviously as cloud shell runs in Debian environment
$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
I am looking for a solution or change to a script so that it can work in both Linux and cloud shell environment.
I feel alias is causing some problem. below is my .bashrc
file
# .bashrc
Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
Uncomment the following line if you don't like systemctl's auto-paging feature:
export SYSTEMD_PAGER=
User specific aliases and functions
alias python=python3.7
alias pip=pip3.7
.myenv
, and possibly from what shell. Can you add that to your question, please – Chris Davies Sep 21 '20 at 15:19