March 19th, 2007 § § permalink
一个获得相对路径的脚本
小例子:
$ getrelpath d:/HelloWorld/b d:/HelloWorld/a/bbb/c.jpg
../a/bbb/c.jpg
源文件:
#!/bin/bash
# get relative pathname of a file from a given dir
# Michael.Fantasia@Gmail.com
if [ "$#" -ne "2" ]
then
echo "Usage: $(basename $0) dirname filename" >&2
exit 1
fi
dirname=$(echo $1/ | sed 's@/*$@/@')
if [ -d "$2" ]
then
filename=$(echo $2/ | sed 's@/*$@/@')
else
filename=$2
fi
path='[^/]*/'
i=1
while [ "$i" -ne "0" ]
do
pathdir=$(k=0;while [ "$k" -lt "$i" ];do echo -n $path;((k=k+1));done)
str1=$(expr "$dirname" : '('"$pathdir"')')
str2=$(expr "$filename" : '('"$pathdir"')')
if [ "$str1" != "$str2" -o -z "$str1" ]
then
((i=i-1))
break
fi
((i=i+1))
done
if [ "$i" -eq "0" ]
then
echo $2
exit 0
fi
pathdir=$(k=0;while [ "$k" -lt "$i" ];do echo -n $path;((k=k+1));done)
str1=$(echo $(expr "$dirname" : "$pathdir"'(.*)')/ | sed 's@/*$@/@;s@[^/]+@..@g')
str2=$(expr "$filename" : "$pathdir"'(.*)')
echo $str1$str2 | sed 's@^/@./@'
exit 0
March 10th, 2007 § § permalink
从国家地理网站下载每日图片
#!/bin/bash
# picngs: Download today's picture from National Geographics website
# Michael.Fantasia@Gmail.com
# arg1: datefmt
# arg2: picdir
# export LC_ALL=C
BADDATE=66
WEBFAILURE=67
SEDFAILURE=68
pntmp=$(basename "$0")_$$
picdir=${2:-"${HOME}/Pictures/Photography"}
cd /tmp
if [ $# -eq 0 ]
then
mfdate=$(date +%Y.%m.%d)
wget -q "http://photography.nationalgeographic.com/photography/photo-of-the-day/" -O $pntmp
else
if date -d "$1" +%Y-%m-%d 2>/dev/null
then
mfdate=$(date -d $1 +%Y.%m.%d)
mfdate2=$(date -d $1 +"%B %e, %Y" | sed 's/ +/ /g')
wget -q "http://photography.nationalgeographic.com/photography/photo-of-the-day/archive/?page=1&month=$(date -d $1 +%m)&year=$(date -d $1 +%Y)" -O "$pntmp"_search
if [ $? -ne 0 ]
then
echo "Error in finding picture." >&2
exit $WEBFAILURE
fi
papername=$(sed -n '/'"$mfdate2"'/,/</div>/p' "$pntmp"_search | sed 's/</n/g' | grep '^a href' | grep 'photo-of-the-day' | head -n1 | cut -d'"' -f2)
wget -q "http://photography.nationalgeographic.com$papername" -O $pntmp
else
echo "Unknown date format." >&2
exit $BADDATE
fi
fi
picname=$mfdate-$(sed -n 's/.*<h1>([^<]*)</h1>.*/1/p' $pntmp | sed 's/['"'"',"]//g;s/ +$//;s/ +/_/g;s///_/g;'"s/'/'/g" | tr '[:upper:]' '[:lower:]')
findname=$(sed 's/</div>/n/g' $pntmp | grep 'Download Wallpaper' | sed 's/</n/g' | grep '^a href' | head -n1 | cut -d'"' -f2)
if [ "$findname" = "" ]
then
#findname=$(sed -n '/primary_photo/,/</div>/p' $pntmp | sed 's/</n/g' | grep '^img src' | grep 'ngeo' | head -n1 | cut -d'"' -f2)
findname=$(sed -n '/primary_photo/,/</div>/p' $pntmp | sed 's/</n/g' | grep '^img src' | cut -d'"' -f2)
fi
if [ -z "$findname" ]
then
echo "No picture found." >&2
exit $SEDFAILURE
fi
wget -q -c "$findname" -O $picname.jpg
if [ $? -ne 0 ]
then
echo "Error in downloading picture." >&2
exit $WEBFAILURE
else
mfdate=$(echo $mfdate | sed 's/.//g')
mkdir -p $picdir/$(date -d $mfdate +%Y)
mv $picname.jpg $picdir/$(date -d $mfdate +%Y)/
open $picdir/$(date -d $mfdate +%Y)/$picname.jpg
fi
echo "Done."
growlnotify --image "$picdir/$(date -d $mfdate +%Y)/$picname.jpg" -m "National Geographic Picture of the Day $mfdate downloaded."
exit 0