使用 bash 脚本添加一些别名并从脚本中获取 .bashrc 文件

使用 bash 脚本添加一些别名并从脚本中获取 .bashrc 文件

您好,我正在尝试自动化一些安装后任务并将它们记录在我的 github 帐户上。这就是为什么我正在编写一个脚本来安装一些附加数据包并向.bashrc文件添加几个新别名,然后我想获取该.bashrc文件。这是脚本本身:

#!/bin/bash

# This script was inspired by the post of Remy Sharp: https://remysharp.com/2018/08/23/cli-improved

bashrc_loc="$HOME/.bashrc"

package_installation() {
        # Updating, upgrading and installing some additional packages
        sudo apt update
        sudo apt-get upgrade -y
        sudo apt-get install ncdu \
                tldr \
                ag \
                bat \
                prettyping \
                fzf \
                csvkit -y
        sudo apt-get autoremove -y
}

alias_add() {
        # Adding the alias in the ~/.bashrc file
        sed -i "\$aalias help='tldr'" "$bashrc_loc"
        sed -i "\$aalias du='ncdu --color dark -rr -x --exclude .git --exclude node_modules'" "$bashrc_loc"
        sed -i "\$aalias top='sudo htop'" "$bashrc_loc"
        sed -i "\$aalias preview=\"fzf --height 40% --preview 'if file -i {}|grep -q binary; then file -b {}; else bat --color \"always\" --line-range :40 {}; fi'\"" "$bashrc_loc"
        sed -i "\$aexport FZF_DEFAULT_OPTS=\"--bind='ctrl-o:execute(code {})+abort'\"" "$bashrc_loc"
        sed -i "\$aalias ping='prettyping --nolegend'" "$bashrc_loc"
        sed -i "\$aalias cat='bat'" "$bashrc_loc"
        source "$bashrc_loc"
}

所以我想知道是否可以source .bashrc在脚本中添加,因为它正在尝试运行该文件,如果我将此行添加到底部,shellcheck 会报告^-- SC1090: Can't follow non-constant source. Use a directive to specify location.

相关内容