Grep
每日桌面腳本的wget圖像問題
我有一個腳本(Nuno 寫的)到
wget
一個特定站點的圖像並設置我的牆紙。但是我似乎無法修改它以能夠wget
從該站點獲取圖像:chromecast源中的圖像有一個這樣的 url:image
我似乎無法隔離使用的 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
我被困住了。
您的腳本需要進行一些改造。
該行後面的兩行
#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
很棒的圖片順便說一句!