简体   繁体   中英

Environment Variables not set properly through script when ssh'ing into virtual machine

So I'm currently working on an LLVM compiler project and I'm running Ubuntu 14.04.2 LTS in VirtualBox on top of my MacBook Air.

I was given a bash script that sets up the required environment variables for the project. The script functions correctly if I source it from a terminal within the VM. However, I have port-forwarding enabled, that I'm using to ssh into the VM from my Mac's default terminal. But when I ssh into the VM and then try to run the script and set the environment variables, they aren't set properly.

Here is the script-

BASESCRIPT=$(readlink -f "$0")
BASE=$(dirname "$BASESCRIPT")
PROJ="cse231"

if [ -n "${VTENV}" ]; then
    if [ "${BASE}" = "${VTENV}" -a "${PROJ}" = "${VTENV_NAME}" ]; then
        echo "${VTENV_NAME} Virtual Environment is already active."
    else
        echo "There is already ${VTENV_NAME} Virtual Environment activated."
        echo "(at ${VTENV})"
        echo "Deactivate it first (using command 'deactivate_vtenv'), to activate"
        echo "test environment."
    fi
    return 1
fi

export VTENV="${BASE}"
export VTENV_NAME="${PROJ}"
export CSE231ROOT="${VTENV}"
export LLVMBIN="${VTENV}/llvm/build/Release+Asserts/bin"
export LLVMLIB="${VTENV}/llvm/build/Release+Asserts/lib"
export BENCHMARKS="${VTENV}/extra/benchmarks"
export INSTRUMENTATION="${VTENV}/extra/instrumentation"
export OUTPUTLOGS="${VTENV}/logs"

echo "Activating ${VTENV_NAME} Virtual Environment (at ${VTENV})."
echo ""
echo "To exit from this virtual environment, enter command 'deactivate_vtenv'."

export "VTENV_PATH_BACKUP"="${PATH}"
export "VTENV_PS1_BACKUP"="${PS1}"

deactivate_vtenv() {
    echo "Deactivating ${VTENV_NAME} Virtual Environment (at ${VTENV})."
    echo "Restoring previous environment settings."

    export "PATH"="${VTENV_PATH_BACKUP}"
    unset -v "VTENV_PATH_BACKUP"
    export "PS1"="${VTENV_PS1_BACKUP}"
    unset -v "VTENV_PS1_BACKUP"

    unset -v VTENV
    unset -v VTENV_NAME
    unset -v CSE231ROOT
    unset -v LLVMBIN
    unset -v LLVMLIB
    unset -v BENCHMARKS
    unset -v INSTRUMENTATION
    unset -v OUTPUTLOGS
    unset -f deactivate_vtenv

    if [ -n "$BASH" -o -n "$ZSH_VERSION" ]; then
        hash -r
    fi
}

export PATH="${VTENV}/llvm/build/Release+Asserts/bin:${PATH}"

export PS1="[${VTENV_NAME}]${PS1}"

if [ -n "$BASH" -o -n "$ZSH_VERSION" ]; then
    hash -r
fi

The problem I have is that the environment variable $BASE should be set as "/home/user/cse231-proj", which it is when I source the script from the VM. But when I source it after ssh'ing in from my Mac's terminal, the environment is simply set as "." and all corresponding environment variables are extended from that, fudging any further commands I want to run using the environment variables.

What's going on here and why can't the script read the correct absolute path? How can I go about fixing this? Any help would be appreciated. Thanks.

When you log on via SSH, $0 is -bash when you source the script (or something similar) because it's a login shell. readlink sees that as another command-line argument and fails, leaving BASESCRIPT set to an empty string in your example, which means dirname returns the . you see.

Don't source the script in your current shell, run it instead. Then $0 will be the name of the script, and readlink -f $0 will return its full canonical path, and dirname will then emit the path to the script like it's supposed to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM