Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug while using terminal zsh #3

Open
LetMeFly666 opened this issue May 7, 2024 · 3 comments
Open

Bug while using terminal zsh #3

LetMeFly666 opened this issue May 7, 2024 · 3 comments

Comments

@LetMeFly666
Copy link

When I use bash, this script will run correctly.

But when I use zsh, the PATH will be misly separated.

Then the terminal will show prompt_status:5:command not found:wc always.

I think I could make a Pull Request to let the script support both bash and zsh terminal.

Need I to do that? Looking forward to your response.

@vamWu
Copy link

vamWu commented Aug 9, 2024

When I use bash, this script will run correctly.

But when I use zsh, the PATH will be misly separated.

Then the terminal will show prompt_status:5:command not found:wc always.

I think I could make a Pull Request to let the script support both bash and zsh terminal.

Need I to do that? Looking forward to your response.

Hello,I also encountered this problem in zsh, and since I'm not good at this part of programming, I was confused about how to modify the script.
Would you like to tell me how to modify it, or send me a modified script? Thank you very much!

@LetMeFly666
Copy link
Author

LetMeFly666 commented Aug 11, 2024

When I use bash, this script will run correctly.
But when I use zsh, the PATH will be misly separated.
Then the terminal will show prompt_status:5:command not found:wc always.
I think I could make a Pull Request to let the script support both bash and zsh terminal.
Need I to do that? Looking forward to your response.

Hello,I also encountered this problem in zsh, and since I'm not good at this part of programming, I was confused about how to modify the script. Would you like to tell me how to modify it, or send me a modified script? Thank you very much!

Hi, here is my modified script, which can only support zsh now as I remembered:

#!/usr/bin/env bash
# LetMeFly666: this script from https://github.com/phohenecker/switch-cuda
#              and changed from ↑

# Copyright (c) 2018 Patrick Hohenecker
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# author:   Patrick Hohenecker <[email protected]>
# version:  2018.1
# date:     May 15, 2018


set -e


# ensure that the script has been sourced rather than just executed
if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then
    echo "Please use 'source' to execute switch-cuda.sh!"
    exit 1
fi

INSTALL_FOLDER="/usr/local"  # the location to look for CUDA installations at
TARGET_VERSION=${1}          # the target CUDA version to switch to (if provided)

# if no version to switch to has been provided, then just print all available CUDA installations
if [[ -z ${TARGET_VERSION} ]]; then
    echo "The following CUDA installations have been found (in '${INSTALL_FOLDER}'):"
    ls -l "${INSTALL_FOLDER}" | egrep -o "cuda-[0-9]+\\.[0-9]+$" | while read -r line; do
        echo "* ${line}"
    done
    set +e
    return
# otherwise, check whether there is an installation of the requested CUDA version
elif [[ ! -d "${INSTALL_FOLDER}/cuda-${TARGET_VERSION}" ]]; then
    echo "No installation of CUDA ${TARGET_VERSION} has been found!"
    set +e
    return
fi

# the path of the installation to use
cuda_path="${INSTALL_FOLDER}/cuda-${TARGET_VERSION}"

# filter out those CUDA entries from the PATH that are not needed anymore
# path_elements=(${PATH//:/ })
path_elements=(${(s/:/)PATH})
new_path="${cuda_path}/bin"

for p in "${path_elements[@]}"; do
    # echo $p
    if [[ ! ${p} =~ ^${INSTALL_FOLDER}/cuda ]]; then
        new_path="${new_path}:${p}"
    fi
done


# filter out those CUDA entries from the LD_LIBRARY_PATH that are not needed anymore
# ld_path_elements=(${LD_LIBRARY_PATH//:/ })
ld_path_elements=(${(s/:/)LD_LIBRARY_PATH})
new_ld_path="${cuda_path}/lib64:${cuda_path}/extras/CUPTI/lib64"
for p in "${ld_path_elements[@]}"; do
    echo $p
    if [[ ! ${p} =~ ^${INSTALL_FOLDER}/cuda ]]; then
        new_ld_path="${new_ld_path}:${p}"
    fi
done

# update environment variables
export CUDA_HOME="${cuda_path}"
export CUDA_ROOT="${cuda_path}"
export LD_LIBRARY_PATH="${new_ld_path}"
export PATH="${new_path}"

echo "Switched to CUDA ${TARGET_VERSION}."

set +e
return

It can also be modified to support both bash and zsh if needed, fell free to @ me.

@vamWu
Copy link

vamWu commented Aug 14, 2024

Thank you very much for your sharing; it worked effectively after I ran it.

Today, I also used the LLM ChatGPT-4o and made modifications suitable for zsh. I tested it, and it worked as well.

As for the specific differences between your script and mine, I haven't compared them in detail. I’m sharing this with you, hoping it might provide inspiration or any other form of help.

#!/usr/bin/env zsh

set -e

# ensure that the script has been sourced rather than just executed
if [[ "${(%):-%N}" != "$0" ]]; then
    echo "Please use 'source' to execute switch-cuda.sh!"
    return 1
fi

INSTALL_FOLDER="/usr/local"  # the location to look for CUDA installations at
TARGET_VERSION=${1}          # the target CUDA version to switch to (if provided)

# if no version to switch to has been provided, then just print all available CUDA installations
if [[ -z ${TARGET_VERSION} ]]; then
    echo "The following CUDA installations have been found (in '${INSTALL_FOLDER}'):"
    ls -l "${INSTALL_FOLDER}" | egrep -o "cuda-[0-9]+\\.[0-9]+$" | while read -r line; do
        echo "* ${line}"
    done
    set +e
    return 0
# otherwise, check whether there is an installation of the requested CUDA version
elif [[ ! -d "${INSTALL_FOLDER}/cuda-${TARGET_VERSION}" ]]; then
    echo "No installation of CUDA ${TARGET_VERSION} has been found!"
    set +e
    return 1
fi

# the path of the installation to use
cuda_path="${INSTALL_FOLDER}/cuda-${TARGET_VERSION}"

# filter out those CUDA entries from the PATH that are not needed anymore
path_elements=(${(s/:/)PATH})
new_path="${cuda_path}/bin"
for p in "${path_elements[@]}"; do
    if [[ ! ${p} == ${INSTALL_FOLDER}/cuda* ]]; then
        new_path="${new_path}:${p}"
    fi
done

# filter out those CUDA entries from the LD_LIBRARY_PATH that are not needed anymore
ld_path_elements=(${(s/:/)LD_LIBRARY_PATH})
new_ld_path="${cuda_path}/lib64:${cuda_path}/extras/CUPTI/lib64"
for p in "${ld_path_elements[@]}"; do
    if [[ ! ${p} == ${INSTALL_FOLDER}/cuda* ]]; then
        new_ld_path="${new_ld_path}:${p}"
    fi
done

# update environment variables
export CUDA_HOME="${cuda_path}"
export CUDA_ROOT="${cuda_path}"
export LD_LIBRARY_PATH="${new_ld_path}"
export PATH="${new_path}"

echo "Switched to CUDA ${TARGET_VERSION}."

set +e
return 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants