为什么我在这个脚本中遇到错误?

为什么我在这个脚本中遇到错误?

我目前正在尝试实现这个脚本,这是问题。现在我已经创建了脚本change_wallpaper_reddit.sh,为了方便您使用,该脚本如下所示。

# Reference: https://askubuntu.com/a/911958/566421

# Set the script home directory:
SHOME=Daily-Reddit-Wallpaper

# Set the output folder in the home directory to save the Wallpapers to:
DIR=Pictures/Wallpapers

# Set the --time parameter value
TIME=now

# Check if the Desktop Environment is changed:
LAST=$(cat "$HOME/$SHOME/last-desktop-environment.log")
if [ "$1" != "$LAST" ]
then
    # Get the name of the last saved wallpaper image:
    IMG=$(ls -Art $HOME/$DIR | tail -n 1)
    rm $HOME/$DIR/$IMG
fi

# Desktop Environment cases:
if [ -z ${1+x} ] || [ "$1" = "gnome" ] || [ "$1" = "unity" ]
then
    # Set the necessary environment variables - PID=$(pgrep gnome-session -u $USER) - UBUNTU/UNITY/GNOME:
    export GNOME_DESKTOP_SESSION_ID=true
    export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep gnome-session -n)/environ | cut -d= -f2-)

    # Run the script:
    ~/$SHOME/change_wallpaper_reddit.py --time $TIME --output ~/$DIR 

elif [ "$1" = "kde" ]
then
    # Set the necessary environment variables - KUBUNTU/PLASMA/KDE:
    export KDE_FULL_SESSION=true
    export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep startkde -n)/environ | cut -d= -f2-)

    # Run the script:
    $HOME/$SHOME/change_wallpaper_reddit.py --time $TIME --output $DIR

elif [ "$1" = "mate" ]
then
    # Set the necessary environment variables - Ubuntu MATE/MATE:
    export DESKTOP_SESSION=mate
    export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep mate-session -n)/environ | cut -d= -f2-)

    # Run the script:
    $HOME/$SHOME/change_wallpaper_reddit.py --time $TIME --output $DIR

elif [ "$1" = "lxde" ]
then
    # Set the necessary environment variables - type 'echo $DISPLAY` to find your current display - LUBUNTU/LXDE:
    export DESKTOP_SESSION=Lubuntu
    export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep lxsession -n)/environ | cut -d= -f2-)
    export DISPLAY=:1.0

    # Run the script:
    $HOME/$SHOME/change_wallpaper_reddit.py --time $TIME --output $DIR

elif [ "$1" = "xfce4" ]
then
    # Set the necessary environment variables - XUBUNTU/XFCE4:
    export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep xfce4-session -n)/environ|cut -d= -f2-)

    # Run the script:
    $HOME/$SHOME/change_wallpaper_reddit.py --time $TIME --output $DIR

    # Get the name of the last saved wallpaper image:
    IMG=$(ls -Art $HOME/$DIR | tail -n 1)

    # Since 'change_wallpaper_reddit.py' doesn't work properly with xfce4 we shall set the background manually:
    xfconf-query --channel xfce4-desktop --property /backdrop/screen0/monitor0/workspace0/last-image --set $HOME/$DIR/$IMG

    # Property list:      xfconf-query --channel xfce4-desktop --list
    # Current settings:   xfconf-query -c xfce4-desktop -p /backdrop -lv
    # Set 'zoomed' style: xfconf-query --channel xfce4-desktop --property /backdrop/screen0/monitor0/workspace0/image-style --set 5
    # References:         https://askubuntu.com/q/380550/566421 and https://askubuntu.com/q/414422/566421

else
    echo "Wrong argument. It must be:"
    echo "  - empty (default) = gnome = unity"
    echo "  - kde"
    echo "  - lxde"
    echo "  - mate"
    echo "  - xfce4"
fi

# Save the current value of the Desktop Environment variable:
echo "$1" > "$HOME/$SHOME/last-desktop-environment.log"

我在我的 中创建了一个新的作业sudo crontab,如下所示:

0 * * * * /home/sharan/Daily-Reddit-Wallpaper/change_wallpaper_reddit.sh gnome > /home/sharan/Daily-Reddit-Wallpaper/cron.log 2>&1

但是,该脚本不起作用并在文件中出现以下错误cron.log

cat: /root/Daily-Reddit-Wallpaper/last-desktop-environment.log: No such file or directory
ls: cannot access '/root/Pictures/Wallpapers': No such file or directory
rm: cannot remove '/root/Pictures/Wallpapers/': No such file or directory
/home/sharan/Daily-Reddit-Wallpaper/change_wallpaper_reddit.sh: 29: /home/sharan/Daily-Reddit-Wallpaper/change_wallpaper_reddit.sh: /root/Daily-Reddit-Wallpaper/change_wallpaper_reddit.py: not found
/home/sharan/Daily-Reddit-Wallpaper/change_wallpaper_reddit.sh: 88: /home/sharan/Daily-Reddit-Wallpaper/change_wallpaper_reddit.sh: cannot create /root/Daily-Reddit-Wallpaper/last-desktop-environment.log: Directory nonexistent

我不太明白这里出了什么问题。

答案1

你以 root 身份运行 cron 任务,但希望在用户的主目录中工作。问题出现在以下行中:

LAST=$(cat "$HOME/$SHOME/last-desktop-environment.log")

其中$HOME被翻译成/root/$SHOMEDaily-Reddit-Wallpaper然后cat抱怨没有文件/root/Daily-Reddit-Wallpaler/last-desktop-environment.log

相关内容