我有一个脚本( Nuno写的)来wget
一个特定站点的图片,并设置我的壁纸。然而,我似乎无法修改它以使其能够从这个站点:wget
:铬层中获取一个图像
源中的图像具有如下所示的url:图像
我似乎无法使用grep
隔离url,因为它很贪婪。我意识到grep -P
可以工作,如果我在终端中手动输入它,它就能工作。但是在脚本中,当赋值变量时,它不起作用。变量可以用grep
设置,但不能用grep -P
设置。
#!/bin/bash
# * Name: earthwall.sh
# * Description: Downloads random image from earthview.withgoogle.com and sets as wallpaper on OSX
# * Author: Nuno Serro
# * Date: 09/07/2015 22:24:11 WEST
# * License: This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
# * Copyright (c) 2015, Nuno Serro
#PID=$(pgrep gnome-session)
#PID=$(pgrep -f 'gnome-session' | head -n1)
#export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ)
mkdir -p ~/Pictures/globewall
cd ~/Pictures/globewall
# Get page index
wget -q https://clients3.google.com/cast/chromecast/home -O ~/Pictures/globewall/.index.html 2> /dev/null
if [ $? -ne 0 ]; then
echo "Failed to get index from google chromecast"
exit 1
fi
# Set image url, name and location
image_url=`cat ~/Pictures/globewall/.index.html | grep lh3.googleusercontent.com | grep -shoP 'https:[\\][\/][\\][\/]lh3(.*?)-mv' -m 1 -o | head -1 | sed 's/\\//g' | sed 's/u003d/=/g'`
image_name= wallpaper.jpg
# Get image
wget -q $image_url -O ~/Pictures/globewall/$image_name 2> /dev/null
if [ $? -ne 0 ]; then
echo "Failed to get image from www.googleusercontent.com"
exit 1
fi
# Change wallpaper
sleep 1
/usr/bin/gsettings set org.gnome.desktop.background picture-options 'zoom'
/usr/bin/gsettings set org.gnome.desktop.background picture-uri "file://~/Pictures/globewall/$image_name"
/usr/bin/gsettings set org.gnome.desktop.screensaver picture-uri "file://~/Pictures/globewall/$image_name"
echo "Wallpaper changed to $image_name"
exit 0
我卡住了。
发布于 2019-01-08 19:55:47
你的剧本需要改造一下。
#Set image url, name and location
行后面的两行因各种原因而中断(例如不正确的grep语法、不正确的赋值)。此外,您的脚本不会双引号文件名,这可能会破坏您的脚本取决于情况。
为了更可靠地工作,我重写了脚本的核心,如下所示:
#!/bin/bash
fn_basedir=~/Pictures/globewall/
fn_index='.index.html'
fn_image='wallpaper.jpg'
mkdir -p "$fn_basedir"
# Get page index
wget -q "https://clients3.google.com/cast/chromecast/home" -O "${fn_basedir}${fn_index}"
if [ $? -ne 0 ]; then
echo "Failed to get index from google chromecast"
exit 1
fi
# Set image url
image_url=$(grep -oP 'https:\\/\\/lh3(.*?)-mv' "${fn_basedir}${fn_index}" | sed -e 's/\\//g' -e 's/u003d/=/g' | head -1)
# Get image
wget -q "$image_url" -O "${fn_basedir}${fn_image}"
if [ $? -ne 0 ]; then
echo "Failed to get image from www.googleusercontent.com"
exit 1
fi
# Change wallpaper
sleep 1
/usr/bin/gsettings set org.gnome.desktop.background picture-options 'zoom'
/usr/bin/gsettings set org.gnome.desktop.background picture-uri "file://${fn_basedir}${fn_image}"
/usr/bin/gsettings set org.gnome.desktop.screensaver picture-uri "file://${fn_basedir}${fn_image}"
echo "Wallpaper changed to ${fn_image}"
exit 0
太棒的照片了!
https://unix.stackexchange.com/questions/493308
复制相似问题