源代码
sh
#!/bin/bash
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# should match env name from YAML
ENV_NAME=rlgpu
pushd "${ROOT_DIR}/python"
# setup conda
CONDA_DIR="$(conda info --base)"
source "${CONDA_DIR}/etc/profile.d/conda.sh"
# deactivate the env, if it is active
ACTIVE_ENV_NAME="$(basename ${CONDA_PREFIX})"
if [ "${ENV_NAME}" = "${ACTIVE_ENV_NAME}" ]; then
conda deactivate
fi
# !!! this removes existing version of the env
conda remove -y -n "${ENV_NAME}" --all
# create the env from YAML
conda env create -f ./rlgpu_conda_env.yml
if [ $? -ne 0 ]; then
echo "*** Failed to create env"
exit 1
fi
# activate env
conda activate "${ENV_NAME}"
if [ $? -ne 0 ]; then
echo "*** Failed to activate env"
exit 1
fi
# double check that the correct env is active
ACTIVE_ENV_NAME="$(basename ${CONDA_PREFIX})"
if [ "${ENV_NAME}" != "${ACTIVE_ENV_NAME}" ]; then
echo "*** Env is not active, aborting"
exit 1
fi
# install isaacgym package
pip install -e .
popd
echo "SUCCESS"
逐行解读
sh
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
这段shell代码的主要目的是获取当前脚本的所在目录的绝对路径,并将其赋值给变量ROOT_DIR。下面是代码的解释:
-
dirname "${BASH_SOURCE[0]}"
:Bash内置的变量BASH_SOURCE[0]表示当前脚本所在的路径,dirname命令会取得该路径的父目录。 -
cd "$(dirname "${BASH_SOURCE[0]}")"
: cd命令用于切换当前工作目录,$(...)是命令替换的语法,将dirname命令的结果作为参数传递给cd命令。 -
pwd
:pwd命令用于打印当前工作目录的绝对路径。 -
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
:将cd命令和pwd命令的结果结合起来,并将结果赋值给变量ROOT_DIR。最终,ROOT_DIR将保存当前脚本的所在目录的绝对路径。