我怎样才能使这个脚本更有效率?

我怎样才能使这个脚本更有效率?

我正在编写一个脚本,它将使用 gnome-tweak-tool 系统地安装 Numix 主题。

我想确保我不会重新安装已安装的项目,因此我使用了which [name of item] > /dev/null.

这是我当前的脚本:

function installNumix() {
    echo "Checking if Numix is installed ..."
    if ! which gnome-tweak-tool > /dev/null; then
        if ! which numix-gtk-theme > /dev/null; then
            if ! which numix-icon-theme-circle > /dev/null; then
                echo "Installing Numix ..."
                sudo add-apt-repository ppa:numix/ppa
                sudo apt-get update
                sudo apt-get install numix-gtk-theme numix-icon-theme-circle -y
                sudo apt-get install gnome-tweak-tool -y
                echo "Configuring Numix:"
                echo "===================================================================="
                echo "Please use the 'tweak-tool' to change your theme to 'Numix'."
                echo "[GTK+]: Numix."
                echo "[icons]: Numix-Circle."
                echo "===================================================================="
                gnome-tweak-tool
                echo "Numix has been manually configured."
                source ~/.profile
                changeBackground backgrounds/background.png
                changeProfilePicture $(whoami) profile_pictures/profile_picture.png
                echo "The Numix has been installed."
                sleep 5
            fi
        fi
    else
        echo "Numix has already been installed."
        sleep 5
    fi
}

我的.profile文件:

#Change desktop background f(x)
#Ex. changeBackground  /path/to/image.png
function changeBackground() {
    FILE="file://$(readlink -f "$1")"
    fileName="${FILE##*/}" # baseName + fileExtension

    echo "Changing desktop background to: '$fileName' ..."
    dconf write "/org/gnome/desktop/background/picture-uri" "'$FILE'"
    echo "Desktop background has been changed."
    sleep 5
}

#Change profile picture f(x)
#Ex. changeProfilePicture username /path/to/image.png
function changeProfilePicture() {
    FILE="$(readlink -f "$2")"
    fileName="${FILE##*/}" # baseName + fileExtension

    echo "Checking if 'imagemagick' is installed ..."
    if ! command brew ls --versions imagemagick >/dev/null 2>&1; then
        echo "Installing 'imagemagick' ..."
        brew install imagemagick -y
        echo "'Imagemagick' has been installed."
        sleep 5
    else
        echo "'Imagemagick' has already been installed."
        sleep 5
    fi

    echo "Changing profile picture to: '$fileName' ..."
    sudo mkdir -p '/var/lib/AccountsService/icons/'"$1"
    sudo convert "$2" -set filename:f '/var/lib/AccountsService/icons/'"$1/%t" -resize 96x96 '%[filename:f].png'
    echo "Profile picture has been changed."
    sleep 5
}

答案1

gnome-tweak-tool您可以使用 来设置脚本中的 gtk 和窗口管理器主题以及图标主题,而不是让用户手动使用gsettings。例如

gsettings set org.gnome.desktop.interface gtk-theme Numix
gsettings set org.gnome.desktop.wm.preferences theme Numix
gsettings set org.gnome.desktop.interface icon-theme Numix-Circle

顺便说一句,除非 和numix-gtk-themenumix-icon-theme-circlePATH 目录中某处的可执行文件,否则which在它们上运行将不会执行您想要的操作。

相反,检查特定文件或目录是否存在。例如

if [ ! -d /usr/share/themes/Numix ] ; then ... fi

我没有安装 Numix 主题,所以我不知道这是否是正确的目录 - 使用dpkg -L numix-gtk-themedpkg -L numix-icon-theme-circle找出要搜索的正确目录。

或者,不必费心检查软件包是否已安装。赶紧跑:

apt-get -y install numix-gtk-theme numix-icon-theme-circle gnome-tweak-tool

(可以选择将 stdout 和 stderr 重定向到 /dev/null)

如果已经安装了这些软件包的最新版本,apt-get则不会执行任何操作。否则,它将安装或升级它们。

最后,使用sudo add-apt-repository -y ppa:numix/ppa时不要提示用户。如果存储库已添加,不会造成任何损害 - 它会注释掉文件中以前的条目/etc/sources.list.d/numix-ubuntu-ppa-yakkety.list并将 ppa 添加到文件的开头。

相关内容