[更新] 优化sudo特权使用方式

This commit is contained in:
寻觅(樊旭东) 2024-06-04 11:19:32 +08:00
parent f704cae1a0
commit c8ac7b472c
3 changed files with 26 additions and 23 deletions

View File

@ -17,14 +17,14 @@
# 实例 `包管_更新 -n`; `包管_更新 -确认`
包管一更新() {
if [ -x "$(command -v oma)" ]; then
# sudo oma refresh
sudo oma update -y
# ${特权} oma refresh
${特权} oma update -y
elif [ -x "$(command -v apt)" ]; then
sudo apt-get update -qq
${特权} apt-get update -qq
elif [ -x "$(command -v yum)" ]; then
sudo yum update
${特权} yum update
elif [ -x "$(command -v pacman)"]; then
sudo pacman -Syu
${特权} pacman -Syu
fi
}
@ -34,14 +34,14 @@
if [ -x "$(command -v oma)" ]; then
# 解决oma找不到软件包返回值还是0的问题, 先搜索一下软件包
# 神奇的oma居然不走标准输出(2>&1, 将错误输出重定向到标准输出, 方便管道符处理), 坑人啊
sudo oma search $1 2>&1 | head -n 1 | grep -q '\[完整匹配\]' || return 1
sudo oma install -y $1
${特权} oma search $1 2>&1 | head -n 1 | grep -q '\[完整匹配\]' || return 1
${特权} oma install -y $1
elif [ -x "$(command -v apt)" ]; then
sudo apt -y install $1
${特权} apt -y install $1
elif [ -x "$(command -v yum)" ]; then
sudo yum install -y $1
${特权} yum install -y $1
elif [ -x "$(command -v pacman)" ]; then
sudo pacman -Sy --needed $1
${特权} pacman -Sy --needed $1
else
return 1
fi

View File

@ -15,7 +15,7 @@ readonly 言灵一版本="v1.0.1"
# 全局变量 #
###############################################################################
typeset -a 通用一包列表=('wget' 'curl' 'git' 'vim' 'ctag' 'sudo')
typeset -a 通用一包列表=('wget' 'curl' 'git' 'vim' 'ctag')
typeset -a 架构一包列表=()
typeset -a 系统一包列表=()
readonly CPU一架构="$(uname -m)"
@ -28,6 +28,12 @@ readonly 系统一发行版一名称=$(echo "${NAME}")
# 龙架构区分新旧世界的标志位
世界一类型="旧世界"
if command -v sudo >/dev/null 2>&1; then
特权="sudo"
else
特权=""
fi
###############################################################################
# 函数/方法 #
###############################################################################
@ -45,12 +51,12 @@ readonly 系统一发行版一名称=$(echo "${NAME}")
}
# 从git镜像仓库获取依赖
# *-* -> 位置参数 1 <- 拉`作者/仓库名`
# *-* -> 位置参数 1 <- 拉`作者/仓库名`
# *-* -> 位置参数 2 <- 保存地址, 保存到本地的哪个路径
获取一git(){
git镜像="https://gitcode.com/mirrors"
色彩一灰色
输出 2 "正在拉 $1 ..."
输出 2 "正在拉 $1 ..."
git clone --depth 1 -q ${git镜像}/$1.git $2
色彩一清除
}
@ -145,7 +151,7 @@ readonly 系统一发行版一名称=$(echo "${NAME}")
# 下载zsh扩展
色彩一灰色
配置一路径=${言灵一路径}/oh-my-zsh
sudo rm -vrf ${配置一路径}
${特权} rm -vrf ${配置一路径}
获取一git ohmyzsh/ohmyzsh ${配置一路径}
获取一git zsh-users/zsh-autosuggestions ${配置一路径}/custom/plugins/zsh-autosuggestions
获取一git zsh-users/zsh-syntax-highlighting ${配置一路径}/custom/plugins/zsh-syntax-highlighting
@ -155,7 +161,7 @@ readonly 系统一发行版一名称=$(echo "${NAME}")
fi
cp -v ${目录一根}/配置/zshrc $HOME/.zshrc
sed -i "s:export ZSH=/etc/oh-my-zsh:export ZSH=${配置一路径}:" $HOME/.zshrc
是否 "是否需要将zsh预设配置赋予所有新用户" && sudo cp $HOME/.zshrc /etc/skel/.zshrc
是否 "是否需要将zsh预设配置赋予所有新用户" && ${特权} cp $HOME/.zshrc /etc/skel/.zshrc
if 是否 "将Zsh设置为默认命令行"; then
chsh -s /usr/bin/zsh
输出 0 "已将zsh设置为默认命令行,此设置主要适用于ssh和tty如终端不生效则需要在可视化设置中手动配置启动时加载 '/usr/bin/zsh' "
@ -216,7 +222,7 @@ readonly 系统一发行版一名称=$(echo "${NAME}")
fi
cargo install "lsd"
fi
sudo ln -vs $HOME/.cargo/bin/lsd /usr/bin/lsd
${特权} ln -vs $HOME/.cargo/bin/lsd /usr/bin/lsd
色彩一清除
if 是否 "将lsd使用别名的方式替代ls"; then
别名='alias ls="lsd"'
@ -230,9 +236,9 @@ readonly 系统一发行版一名称=$(echo "${NAME}")
xmtop版本="v4.2.1"
输出 2 "开始下载 xmtop-${xmtop版本}-${世界一类型}"
xmtop_url="https://gitea.whlug.cn/xunmi/xmtop/releases/download/${xmtop版本}/xmtop_${世界一类型}"
sudo wget -t 3 -q --show-progress -c ${xmtop_url} || 输出 5 "无法下载程序包"
sudo mv -v xmtop_${世界一类型} /usr/bin/xmtop
sudo chmod 755 /usr/bin/xmtop
${特权} wget -t 3 -q --show-progress -c ${xmtop_url} || 输出 5 "无法下载程序包"
${特权} mv -v xmtop_${世界一类型} /usr/bin/xmtop
${特权} chmod 755 /usr/bin/xmtop
else
输出 2 "${主机一架构} 架构, 暂未适配top优化, 推荐手动安装gotop, btop等代替top"
fi
@ -246,7 +252,7 @@ readonly 系统一发行版一名称=$(echo "${NAME}")
git config --global core.editor "vim"
typeset -A 列表=(
["zsh"]="添加omyzsh, zsh自动补齐, 快速添加sudo, 便捷操作目录, 优化命令行提示符等"
["zsh"]="添加omyzsh, zsh自动补齐, 快速添加${特权}, 便捷操作目录, 优化命令行提示符等"
["vim"]="添加常用配置项(允许复制 添加行号 显式当前行等), 常用插件(目录 语法检测)"
["ls"]="使用lsd替代老旧的ls, (lsd是rust编写的兼容ls的目录查看工具, 其丰富的配色和图标可有效增加目录的可读性)"
#["cat"]="使用bat替代cat"

View File

@ -43,9 +43,6 @@ nnoremap <C-t> :NERDTreeToggle<CR>
nnoremap <C-f> :NERDTreeFind<CR>
" 如果目录是最后一个窗口则自动退出vim
autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif
" 防止缓冲区目录冲突
autocmd BufEnter * if winnr() == winnr('h') && bufname('#') =~ 'NERD_tree_\d\+' && bufname('%') !~ 'NERD_tree_\d\+' && winnr('$') > 1 |
\ let buf=bufnr() | buffer# | execute "normal! \<C-W>w" | execute 'buffer'.buf | endif
let g:NERDTreeFileLines = 1 " 显示文件行
" 标签生成器(ludovicchabant/vim-gutentags): 在特定目录下自动调用ctags生成标签