Bash
我怎樣才能使這個腳本更有效率?
我正在編寫一個腳本,它將使用 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 }
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
順便說一句,除非PATH 目錄
numix-gtk-theme
中numix-icon-theme-circle
的某處是執行檔,否則which
在它們上執行不會做你想做的事。而是檢查特定文件或目錄是否存在。例如
if [ ! -d /usr/share/themes/Numix ] ; then ... fi
我沒有安裝 Numix 主題,所以我不知道這是否是正確的目錄 - 使用
dpkg -L numix-gtk-theme
和dpkg -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 添加到文件的開頭。