利用环境变量代替别名

This commit is contained in:
lsy 2025-06-06 01:25:11 +08:00
parent 209f3f2185
commit 1774606938
2 changed files with 41 additions and 59 deletions

View File

@ -1,30 +1,30 @@
#!/bin/bash #!/bin/bash
if [[ $UID != 0 ]]; then if [[ $UID != 0 ]]; then
echo "请以root权限执行该脚本" echo "请以root权限执行该脚本"
exit exit
fi fi
if ! command -v git &> /dev/null; then if ! command -v git &>/dev/null; then
if [[ -f "/usr/bin/apt-get" ]];then if [[ -f "/usr/bin/apt-get" ]]; then
apt-get update -y apt-get update -y
apt-get install git -y apt-get install git -y
elif [[ -f "/usr/bin/apt" ]];then elif [[ -f "/usr/bin/apt" ]]; then
apt update -y apt update -y
apt install git -y apt install git -y
elif [[ -f "/usr/bin/pacman" ]];then elif [[ -f "/usr/bin/pacman" ]]; then
pacman -Syu --noconfirm pacman -Syu --noconfirm
pacman -Sy --noconfirm git pacman -Sy --noconfirm git
else else
echo "git未安装" echo "git未安装"
exit exit
fi fi
fi fi
declare path declare path
echo "请输入脚本的安装位置" echo "请输入脚本的安装位置"
read -p "默认 /var/script" path read -p "默认 /var/script" path
if [[ -z $path ]];then if [[ -z $path ]]; then
path="/var/script" path="/var/script"
fi fi
mkdir -p "$path" mkdir -p "$path"
@ -39,51 +39,42 @@ echo "请选择脚本的下载地址"
url_dick['github(default)']='https://github.com/lsy2246/linuxtool.git' url_dick['github(default)']='https://github.com/lsy2246/linuxtool.git'
url_dick['gitee']='https://gitee.com/lsy22/linuxtool.git' url_dick['gitee']='https://gitee.com/lsy22/linuxtool.git'
for i in "${!url_dick[@]}" ; do for i in "${!url_dick[@]}"; do
url_number=$(( url_number+1 )) url_number=$((url_number + 1))
url_arr[$url_number]=$i url_arr[$url_number]=$i
echo "${url_number}.${i}" echo "${url_number}.${i}"
done done
read -p "请输入:" url_pick read -p "请输入:" url_pick
if [[ $url_pick =~ [1-${#url_dick[@]}] ]]; then if [[ $url_pick =~ [1-${#url_dick[@]}] ]]; then
url=${url_dick[${url_arr[$url_pick]}]} url=${url_dick[${url_arr[$url_pick]}]}
else else
url='https://github.com/lsy2246/linuxtool.git' url='https://github.com/lsy2246/linuxtool.git'
fi fi
echo "正在下载脚本中" echo "正在下载脚本中"
git clone "$url" "$path/linuxtool" &> /dev/null git clone "$url" "$path/linuxtool" &>/dev/null
if ! [[ -d "${path}/linuxtool" ]];then if ! [[ -d "${path}/linuxtool" ]]; then
echo "脚本下载失败" echo "脚本下载失败"
exit exit
fi fi
chmod +x "$path/linuxtool/run.sh" &> /dev/null chmod +x "$path/linuxtool/run.sh" &>/dev/null
update_alias() { # 创建软链接到 /usr/bin 目录
local file="$1" echo "正在创建软链接..."
if [[ -e "$file" ]]; then ln -sf "$path/linuxtool/run.sh" "/usr/bin/tool"
sed -i '/alias tool.*/d' "$file" chmod +x "/usr/bin/tool"
echo "alias tool='$path/linuxtool/run.sh'" >> "$file"
fi
}
shopt -s expand_aliases # 将工具路径写入环境变量配置
alias tool="$path/linuxtool/run.sh" echo "tool='$path/linuxtool/run.sh'" >>/etc/profile
export -f tool &> /dev/null
for file in "${HOME}/.bashrc" "${HOME}/.profile" "${HOME}/.zshrc" "/etc/profile"; do
update_alias "$file"
done
echo "工具箱已经安装成功" echo "工具箱已经安装成功"
echo "位置:${path}/linuxtool" echo "位置:${path}/linuxtool"
echo "命令tool" echo "命令tool"
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
kill $PPID &> /dev/null kill $PPID &>/dev/null
fi fi

View File

@ -3,22 +3,13 @@ declare tool_path=$(cat /etc/profile | grep "tool=" | awk -F "=" '{print $2}' |
tool_path=$(dirname $tool_path) tool_path=$(dirname $tool_path)
rm -rf $tool_path rm -rf $tool_path
remove_alias() { # 删除软链接
local file="$1" if [[ -L "/usr/bin/tool" ]]; then
if [[ -e "$file" ]]; then rm -f "/usr/bin/tool"
sed -i '/alias tool.*/d' "$file" fi
source "$file" &> /dev/null
fi
}
for file in "${HOME}/.bashrc" "${HOME}/.profile" "${HOME}/.zshrc" "/etc/profile"; do
remove_alias "$file"
done
shopt -u expand_aliases
unalias tool &> /dev/null
# 从环境变量配置中删除工具路径
sed -i '/tool=.*/d' /etc/profile
echo "脚本已完整卸载,公众号 lsy22 可获取一键安装脚本" echo "脚本已完整卸载,公众号 lsy22 可获取一键安装脚本"
kill $PPID &> /dev/null kill $PPID &>/dev/null