日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux 非登录shell自动,Linux登录shell和非登录(交互式shell)环境变量配置

發布時間:2025/3/20 linux 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux 非登录shell自动,Linux登录shell和非登录(交互式shell)环境变量配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用Jenkins執行shell腳本的時候, 碰到command not found. 比如java mvn, 這些環境變量配置在/etc/profile

中, 但jenkins執行的時候并沒有加載.

這是因為jenkins執行的shell是非登錄交互式shell, 并不會加載/etc/profile.

交互式shell會加載.bashrc, 進而會加載/etc/bashrc, 而/etc/bashrc會加載/etc/profile.d/*.sh.

因此, 自定義的變量應該定義在/etc/profile.d/*.sh

1.登錄shell

所謂登錄shell,指的是當用戶登錄系統時所取的那個shell,登錄shell屬于交互式shell。

登錄shell將查找4個不同的啟動文件來處理其中的命令。 bash shell處理文件的順序如下:

1:/etc/profile

2:/etc/profile.d等待配置文件

3:$HOME/.bash_profile 會加載$HOME/.bashrc和/etc/bashrc

4:$HOME/.bash_login

5:$HOME/.profile

2. 交互式非登錄shell

如果啟動了一個bash shell而沒有登入系統(如在CLI提示符中鍵入bash),

則啟動了一個交互式非登錄shell.

$HOME/.bashrc

交互式非登錄shell執行~/.bashrc文件中的命令.在每次執行shell腳本時,都會重新讀取這個文件,所以是最完整的。

但是萬事都不是一樣的,debain系列的是不同的,如ubuntu

/etc/profile–>/etc/environment–>$HOME/.profile.要配置java等變量時,都/etc/environment中

.bashrc

# .bashrc

# User specific aliases and functions

alias rm='rm -i'

alias cp='cp -i'

alias mv='mv -i'

# Source global definitions

if [ -f /etc/bashrc ]; then

. /etc/bashrc

fi

登錄shell的初始化文件(比如.bash_profile)通常會運行這個文件。這樣,登錄shell和非登錄shell都可以使用.bashrc中的命令。

/etc/bashrc

[root@localhost ~]# cat /etc/bashrc

# /etc/bashrc

# System wide functions and aliases

# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you

# are doing. It's much better to create a custom.sh shell script in

# /etc/profile.d/ to make custom changes to your environment, as this

# will prevent the need for merging in future updates.

# are we an interactive shell?

if [ "$PS1" ]; then

if [ -z "$PROMPT_COMMAND" ]; then

case $TERM in

xterm*|vte*)

if [ -e /etc/sysconfig/bash-prompt-xterm ]; then

PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm

elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then

PROMPT_COMMAND="__vte_prompt_command"

else

PROMPT_COMMAND='printf " 33]0;%s@%s:%s 07" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'

fi

;;

screen*)

if [ -e /etc/sysconfig/bash-prompt-screen ]; then

PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen

else

PROMPT_COMMAND='printf " 33k%s@%s:%s 33\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'

fi

;;

*)

[ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default

;;

esac

fi

# Turn on parallel history

shopt -s histappend

history -a

# Turn on checkwinsize

shopt -s checkwinsize

[ "$PS1" = "\s-\v\$ " ] && PS1="[u@h W]\$ "

# You might want to have e.g. tty in prompt (e.g. more virtual machines)

# and console windows

# If you want to do so, just add e.g.

# if [ "$PS1" ]; then

# PS1="[u@h:l W]\$ "

# fi

# to your custom modification shell script in /etc/profile.d/ directory

fi

if ! shopt -q login_shell ; then # We're not a login shell

# Need to redefine pathmunge, it get's undefined at the end of /etc/profile

pathmunge () {

case ":${PATH}:" in

*:"$1":*)

;;

*)

if [ "$2" = "after" ] ; then

PATH=$PATH:$1

else

PATH=$1:$PATH

fi

esac

}

# By default, we want umask to get set. This sets it for non-login shell.

# Current threshold for system reserved uid/gids is 200

# You could check uidgid reservation validity in

# /usr/share/doc/setup-*/uidgid file

if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then

umask 002

else

umask 022

fi

SHELL=/bin/bash

# Only display echos from profile.d scripts if we are no login shell

# and interactive - otherwise just process them to set envvars

for i in /etc/profile.d/*.sh; do

if [ -r "$i" ]; then

if [ "$PS1" ]; then

. "$i"

else

. "$i" >/dev/null

fi

fi

done

unset i

unset -f pathmunge

fi

# vim:ts=4:sw=4

參考

總結

以上是生活随笔為你收集整理的linux 非登录shell自动,Linux登录shell和非登录(交互式shell)环境变量配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。