編寫 dconf 更改腳本

編寫 dconf 更改腳本

我為大學的 Ubuntu 機器編寫了一個簡單的腳本,它設定了一些 dconf 覆蓋來設定主題、背景等。

用於此目的的腳本部分:

#Set login background and remove dots
echo "Setting lightdm dconf settings"
xhost +SI:localuser:lightdm
su lightdm -c "gsettings set com.canonical.unity-greeter background '/background.png'"
su lightdm -c "gsettings set com.canonical.unity-greeter draw-grid false"

如果直接輸入到以 lightdm 使用者身分登入的終端機中,這些相同的命令將起作用,例如:

sudo su lightdm -s /bin/bash
gsettings set com.canonical.unity-greeter background '/background.png'
gsettings set com.canonical.unity-greeter draw-grid false

將壁紙設置得很好

關於為什麼命令有效但腳本無效的任何想法?

謝謝

答案1

這是我解決這個問題的方法,而不是運行“sudo bash”或在您的情況下運行“sudo -c”(這會使您的腳本在更改用戶時暫停或根本不起作用)我運行三個小腳本。

(不要忘記使用“chmod +x script_name.sh”使每個腳本可執行)

第一個腳本是我的 install.sh:

sudo cp third_script.sh /tmp         # Copy third_script to tmp for easier access
sudo chmod 0755 /tmp/third_script.sh # This needs to be executable by lightdm
sudo bash second_script.sh           # Runs next file
sudo rm /tmp/third_script.sh         # Now we can remove the third script

然後在 secondary_script.sh 中,我將 lightdm 新增至 xhost 並以 lightdm 使用者身分執行第三個腳本檔案:

#!/bin/bash
echo "$USER"                         # To see if root is running the script
xhost +SI:localuser:lightdm
su lightdm -s /tmp/third_script.sh

第三個腳本是神奇發生的地方:

#!/bin/bash
echo "$USER"                         # To see if lightdm is running the script
gsettings set com.canonical.unity-greeter background '/background.png'
gsettings set com.canonical.unity-greeter draw-grid false

這對我有用!讓我知道是否有更簡單的方法。

相關內容