利用环境变量代替别名

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

@ -63,27 +63,18 @@ fi
chmod +x "$path/linuxtool/run.sh" &>/dev/null
update_alias() {
local file="$1"
if [[ -e "$file" ]]; then
sed -i '/alias tool.*/d' "$file"
echo "alias tool='$path/linuxtool/run.sh'" >> "$file"
fi
}
# 创建软链接到 /usr/bin 目录
echo "正在创建软链接..."
ln -sf "$path/linuxtool/run.sh" "/usr/bin/tool"
chmod +x "/usr/bin/tool"
shopt -s expand_aliases
alias tool="$path/linuxtool/run.sh"
export -f tool &> /dev/null
for file in "${HOME}/.bashrc" "${HOME}/.profile" "${HOME}/.zshrc" "/etc/profile"; do
update_alias "$file"
done
# 将工具路径写入环境变量配置
echo "tool='$path/linuxtool/run.sh'" >>/etc/profile
echo "工具箱已经安装成功"
echo "位置:${path}/linuxtool"
echo "命令tool"
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
kill $PPID &>/dev/null
fi

View File

@ -3,22 +3,13 @@ declare tool_path=$(cat /etc/profile | grep "tool=" | awk -F "=" '{print $2}' |
tool_path=$(dirname $tool_path)
rm -rf $tool_path
remove_alias() {
local file="$1"
if [[ -e "$file" ]]; then
sed -i '/alias tool.*/d' "$file"
source "$file" &> /dev/null
# 删除软链接
if [[ -L "/usr/bin/tool" ]]; then
rm -f "/usr/bin/tool"
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 可获取一键安装脚本"
kill $PPID &>/dev/null