首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在未在集成终端外壳中加载的`~/..profile`‘中声明的函数有问题

在未在集成终端外壳中加载的`~/..profile`‘中声明的函数有问题
EN

Stack Overflow用户
提问于 2022-04-27 18:56:32
回答 1查看 262关注 0票数 1

我有一个.profile,它在运行common-debian.sh之前在容器构建过程中复制到/home/vscode

这一点很重要,因为在容器映像生成过程中,如果文件已经存在,common-delian.sh将跳过创建默认的.profile。

在这个概要中,我有一些bash函数和一些导出的变量。

其中一个变量依赖于执行一个声明的函数,在我的shell中,我可以看到正确的变量和值被设置。

当在vscode中打开一个新的集成终端时,就会出现这个问题。“新终端”命令

但是,我的函数在shell中都无法执行。通过typeset -F确认

我可以运行source ~/.profile,然后可以在shell中使用这些函数。

有什么地方可以让我打开日志来确定这里发生了什么吗?

安装的详细信息

vscode主机安装是Mac M1

代码语言:javascript
复制
VERSION='0.202.6'
CONTENTS_URL='https://github.com/microsoft/vscode-dev-containers/tree/main/containers/debian/history/0.202.6.md'
DEFINITION_ID='debian'
VARIANT='bullseye'
GIT_REPOSITORY='https://github.com/microsoft/vscode-dev-containers/'
GIT_REPOSITORY_RELEASE='v0.224.0'
BUILD_TIMESTAMP='Fri, 25 Feb 2022 10:48:36 GMT'

示例函数声明

代码语言:javascript
复制
kconfig() {
 ls $HOME/.kube/configs/* | tr '\n' ':'
}
declare -f kconfig

示例变量使用

代码语言:javascript
复制
export KUBECONFIG="$(kconfig)"

在获取.profile之前,我可以确认,这个变量是设置的,并且是有效的,应该只来自.profile声明。

供参考的主文件夹

代码语言:javascript
复制
vscode ➜ /workspace (master ✗) $ ls -al ~
total 76
drwxr-xr-x  1 vscode vscode  4096 Apr 27 18:59 .
drwxr-xr-x  1 root   root    4096 Feb 25 10:55 ..
drwxr-xr-x  1    501 dialout  160 Mar  8 22:31 .aws
-rw-r--r--  1 vscode vscode   220 Aug  4  2021 .bash_logout
-rw-r--r--  1 vscode vscode  4665 Feb 25 10:55 .bashrc
drwxr-xr-x  3 vscode vscode  4096 Apr 27 18:59 .cache
drwxr-xr-x  1    501 dialout  192 Apr 25 21:41 .config
drwxr-xr-x  1    501 dialout  160 Apr 27 14:20 .creds
-rw-r--r--  1 vscode vscode   500 Apr 27 18:59 .gitconfig
drwx------  2 vscode vscode  4096 Apr 27 18:59 .gnupg
drwxr-xr-x  1    501 dialout  160 Mar  9 21:59 .kube
drwxr-xr-x 12 vscode vscode  4096 Feb 25 10:55 .oh-my-zsh
-rw-r--r--  1 vscode vscode  2381 Apr 27 19:19 .profile
drwxr-xr-x  1 vscode root    4096 Apr 27 18:59 .vscode-server
drwxr-xr-x  3 vscode root    4096 Apr 27 18:26 .vscode-server-insiders
-rw-r--r--  1 vscode vscode  3897 Feb 25 10:55 .zshrc

编辑如下:

现在我正在复制这个文件,因为~/.bash_profile仍然存在一个.profile,如果在构建容器映像时该文件不存在,vscode common-debian.sh就会创建这个文件。

代码语言:javascript
复制
vscode ➜ /workspace (master ✗) $ ls -al ~
total 80
drwxr-xr-x  1 vscode vscode  4096 Apr 27 20:08 .
drwxr-xr-x  1 root   root    4096 Feb 25 10:55 ..
drwxr-xr-x  1    501 dialout  160 Mar  8 22:31 .aws
-rw-r--r--  1 vscode vscode   220 Aug  4  2021 .bash_logout
-rw-r--r--  1 vscode vscode  2671 Apr 27 19:57 .bash_profile
-rw-r--r--  1 vscode vscode  4665 Feb 25 10:55 .bashrc
drwxr-xr-x  3 vscode vscode  4096 Apr 27 20:08 .cache
drwxr-xr-x  1    501 dialout  192 Apr 25 21:41 .config
drwxr-xr-x  1    501 dialout  160 Apr 27 14:20 .creds
-rw-r--r--  1 vscode vscode   500 Apr 27 20:08 .gitconfig
drwx------  2 vscode vscode  4096 Apr 27 20:08 .gnupg
drwxr-xr-x  1    501 dialout  160 Mar  9 21:59 .kube
drwxr-xr-x 12 vscode vscode  4096 Feb 25 10:55 .oh-my-zsh
-rw-r--r--  1 vscode vscode   807 Aug  4  2021 .profile
drwxr-xr-x  1 vscode root    4096 Apr 27 20:08 .vscode-server
drwxr-xr-x  3 vscode root    4096 Apr 27 20:07 .vscode-server-insiders
-rw-r--r--  1 vscode vscode  3897 Feb 25 10:55 .zshrc

还在/etc/profile.d/中添加了以下内容

代码语言:javascript
复制
# This file is intended to be copied into /etc/profile.d
project_profile="$HOME/.bash_profile"
if [ -r $project_profile ]; then
  source $project_profile
else
  echo "File not found or not readable: $project_profile"
fi

所有这些都没有对生成的新shell产生影响,可能忽略vscode,docker exec -it {container} /bin/bash仍然具有相同的结果。

我还尝试用一个空的外壳配置文件创建一个空终端配置文件,从那个终端窗口启动vscode。认为遗传的env引起了问题。

还设置以下用户设置"terminal.integrated.inheritEnv": false,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-27 19:19:09

来自bash的手册页(在调用部分):

当bash非交互地启动时,为了运行一个shell脚本,它在环境中查找变量BASH_ENV,如果它出现在那里就展开它的值,并使用扩展的值作为文件的名称来读取和执行。Bash的行为就好像执行了以下命令:

if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi

但是,PATH变量的值不用于搜索文件名。

在您的用例中,即shell脚本中,没有读取启动文件。但是,您可以通过将BASH_ENV的值设置为要在启动时读取的文件来更改这一点。或者,您的脚本可以在运行时获取您喜欢的任何内容,例如source .profile

从答复注释中提取的

最初的招贴画指出,这一问题是用vscode中的这一方法解决的:

代码语言:javascript
复制
"terminal.integrated.defaultProfile.linux": "bash",
"terminal.integrated.profiles.linux": {
  "bash": {
    "path": "/bin/bash",
    "icon": "terminal-bash",
    "args": [ "-l" ]
  },
}

(使其成为登录shell的-l arg很可能是最重要的部分?)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72033963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档