gsettings:找不到命令

Sar*_*ran 4 gnome wallpaper bash

我正在尝试创建一个脚本,该脚本将在运行时自动更改墙纸。

#!/bin/bash

cd ~/
rm -r ~/.wallpaper
mkdir .wallpaper
cd ~/.wallpaper

wget https://source.unsplash.com/random/1920x1080
USER=$(whoami)
PATH="file:///home/$USER/.wallpaper/1920x1080"
echo $PATH
gsettings set org.gnome.desktop.background picture-uri "$PATH"
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,./change_wallpaper.sh我正确地得到了回声,但是然后

./change_wallpaper.sh: line 12: gsettings: command not found

但是,当我从终端运行相同的命令时,它执行得很好并且墙纸正在改变。

当我运行whereis gsettings它告诉

gsettings: /usr/bin/gsettings /usr/share/man/man1/gsettings.1.gz

为什么gsettings: command not found从脚本执行时会显示?

L. *_*mes 5

你的脚本不会对每个人都有效。 您为用户设置的 home 变量对于HOME位置与/home/user. 例如,我个人空间的家庭位置是/home/user/l/j/ljames

而不是将路径设置为"file:///home/$USER/.wallpaper/1920x1080"您应该更正确地将其更改为"file:///$HOME/.wallpaper/1920x1080". 该变量$HOME已经扩展到用户的整个家庭空间。

如果进行以下更改,您的脚本将起作用:

#!/bin/bash

cd ~/
rm -r ~/.wallpaper
mkdir .wallpaper
cd ~/.wallpaper

wget https://source.unsplash.com/random/1920x1080
# USER=$(whoami) This line isn't neccesary.)    
path="file:///$HOME/.wallpaper/1920x1080"
echo $path
gsettings set org.gnome.desktop.background picture-uri "$path"
Run Code Online (Sandbox Code Playgroud)

一个更有效的例子是:

#!/bin/bash

[ ! -d ~/.wallpaper ] && mkdir ~/.wallpaper
cd ~/.wallpaper
wget -O 1920x1080 https://source.unsplash.com/random/
path="file:///$HOME/.wallpaper/1920x1080"
rm ~/.cache/wallpaper/*
gsettings set org.gnome.desktop.background picture-uri "$path"
Run Code Online (Sandbox Code Playgroud)

行的解释是:

Line #1:  Create wallpaper directory if it doesn't exist.
Line #2:  Move to the folder.
Line #3:  Overwrite the current file with the new wallpaper.
Line #4:  Set the pathname variable.
Line #5:  Removed the Wallpaper cache for image change detection.
Line #6:  Active the new wallpaper.
Run Code Online (Sandbox Code Playgroud)