为 Ubuntu 添加永久环境变量

为 Ubuntu 添加永久环境变量

我使用 Ubuntu 18.04,并使用 petalinux、Vivado 等软件。例如,每次我想打开 vivado 时,我都会打开终端并输入:

source /opt/XIlinx/Vivado/2020.2/settings64.sh

这仅适用于该特定终端。

我知道如果我编辑 .profile 文件,我可以让它变得多余。所以我添加了这一行:

export PATH="$PATH:/opt/XIlinx/Vivado/2020.2/settings64.sh"

注销、登录,但如果不获取 settings64.sh 文件,我仍然无法打开 vivado。我做错了什么?

这是 petalinux 源文件 - settings.sh:

#
# Setup script for PetaLinux development environment.
#
# Copyright (c) 1995-2013 Xilinx, Inc.  All rights reserved.
#

SETTINGS_FILE='settings.sh'
# The right location will be replaced by the installer
XIL_SCRIPT_LOC="./"

if [ $# != 0 ]; then
    # The first argument is the location of PetaLinux Installation
    # Don't detect the installation location
    XIL_SCRIPT_LOC="$1"
else
    #  XIL_SCRIPT_LOC should point to script location
    if [ "$0" == "ksh" ]; then
        XIL_SCRIPT_LOC_TMP_UNI=`readlink -f ${XIL_ARG_}`
    else
        XIL_SCRIPT_LOC_TMP_UNI=$BASH_SOURCE
    fi
    XIL_SCRIPT_LOC_TMP_UNI=${XIL_SCRIPT_LOC_TMP_UNI%/*}
    if [ "$XIL_SCRIPT_LOC_TMP_UNI" != "" ]; then
        if [ "$XIL_SCRIPT_LOC_TMP_UNI" == "settings.sh" ]; then
            XIL_SCRIPT_LOC_TMP_UNI="./"
        fi
        XIL_SCRIPT_LOC_TMP_UNI=`readlink -f ${XIL_SCRIPT_LOC_TMP_UNI}`
        if [ $? == 0 ]; then
            XIL_SCRIPT_LOC=${XIL_SCRIPT_LOC_TMP_UNI}
        fi
    fi
    unset XIL_SCRIPT_LOC_TMP_UNI
fi

export PETALINUX=`readlink -f "${XIL_SCRIPT_LOC}"`

if echo "${PETALINUX}" | grep -q ' '; then
    echo "********************************************************"
    echo "WARNING: PetaLinux SDK installation path contains spaces"
    echo "WARNING: You are STRONGLY recommend to fix this".
    echo "********************************************************"
fi
export PETALINUX_VER=2020.2

export XSCT_TOOLCHAIN="${PETALINUX}/tools/xsct"

# Figure out host system architecture
# for now, only linux-i386 supported

#
# Add toolchains to user's search path
#
PATH="${XSCT_TOOLCHAIN}/gnu/aarch32/lin/gcc-arm-none-eabi/bin:${PATH}"
PATH="${XSCT_TOOLCHAIN}/gnu/aarch64/lin/aarch64-none/bin:${PATH}"
PATH="${XSCT_TOOLCHAIN}/gnu/armr5/lin/gcc-arm-none-eabi/bin:${PATH}"
PATH="${XSCT_TOOLCHAIN}/gnu/microblaze/lin/bin:${PATH}"

#
# Add required binary tools to the user's search path
#
PATH="${PETALINUX}/tools/xsct/petalinux/bin:${PETALINUX}/tools/common/petalinux/bin:${PATH}"

#
# Check for "." or ".\" in the path - it's broken
#
echo "${PATH}" | tr ":" "\n" | grep '^\./*$' > /dev/null && 
    echo "WARNING: '.' detected in PATH - fixing it." 1>&2
PATH=`echo ${PATH} | tr ":" "\n" | grep -v '^\./*$' | tr "\n" ":"`

# Strip any trailing or multi-colons - they are interpreted as '.'
PATH=$(echo ${PATH} | sed -e 's/:*$//g' -e 's/::*/:/g')

export PATH


echo PetaLinux environment set to \'${PETALINUX}\'

for s in /bin/sh sh; do
    if ! $s --version 2>/dev/null | grep -q "^GNU bash"; then
        echo "WARNING: $s is not bash! "
        echo "bash is PetaLinux recommended shell. Please set your default shell to bash."
        break
    fi
done

if ! echo $SHELL | grep -q "bash"; then
    echo "WARNING: $SHELL is not bash! "
    echo "/bin/bash is Petalinux recommended SHELL variable. Please set your SHELL variable to /bin/bash."
fi

"${PETALINUX}"/tools/common/petalinux//utils/petalinux-env-check

答案1

我在干嘛啊

您正在将可执行脚本的路径名定义为目录路径。/opt/XIlinx/Vivado/2020.2/settings64.sh不是目录的路径,而是脚本的路径settings64.sh。 必须获取该脚本的源代码(即,必须在当前 shell 中执行每个命令)才能创建应用程序所需的设置。 您知道这一点,因为您知道该命令source /opt/XIlinx/Vivado/2020.2/settings64.sh有效。

因此,要自动执行该命令,只需将文件添加到文件中即可~/.bashrc。每当您打开终端时,都会自动读取并执行此文件。这意味着这些设置可用于从该终端启动的任何应用程序。

为了让这些设置在您登录后立即可用,请将该行添加到~/.profile。每当您登录系统时,都会自动读取并执行该文件。这意味着这些设置可用于任何应用程序,包括您使用菜单系统启动的应用程序。

这些设置特定于您运行的程序。因此,将它们设置为通用设置不会对系统的任何其他方面有益。因此,我更倾向于为该程序设置一个包装器脚本,这样每次运行该脚本时,都会设置适当的设置并启动应用程序。

相关内容