我有一个脚本(努诺编写的),用于处理wget
来自某个特定网站的图像并设置我的壁纸。但是我似乎无法修改它以便能够wget
从该站点获取图像:铬铸
源中的图像具有如下 url:图像
我似乎无法隔离使用的网址,grep
因为它很贪婪。我意识到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 <http://www.gnu.org/licenses/>.
#
# * 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
我被困住了。
答案1
您的脚本需要进行一些修改。
该行后面的两行#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
顺便说一句,很棒的图片!