一个获得相对路径的脚本
小例子:
$ getrelpath d:/HelloWorld/b d:/HelloWorld/a/bbb/c.jpg
../a/bbb/c.jpg
源文件:
#!/usr/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;let k=$k+1;done)
str1=$(expr “$dirname“ : ‘\(‘”$pathdir“‘\)‘)
str2=$(expr “$filename“ : ‘\(‘”$pathdir“‘\)‘)
if [ "$str1" != "$str2" -o -z "$str1" ]
then
let i=$i-1
break
fi
let 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;let k=$k+1;done)
str1=$(echo $(expr “$dirname“ : “$pathdir“‘\(.*\)‘)/ | sed ‘s@/*$@/@;s@[^/]\+@\.\.@g‘)
str2=$(expr “$filename“ : “$pathdir“‘\(.*\)‘)
echo $str1$str2 | sed ‘s@^/@\./@‘
exit 0
从国家地理网站下载每日图片
#!/usr/bin/bash
# picngs: Download today’s picture from National Geographics website
# Michael.Fantasia@Gmail.com
BADDATE=66
WEBFAILURE=67
SEDFAILURE=68
picdir=${2:-“d:/Pictures/经典照片“}
cd ~/temp
rm -f 00
if [ $# -eq 0 ]
then
mfdate=$(date +%Y.%m.%d)
wget -q “http://lava.nationalgeographic.com/pod/“ -O 00
else
if date -d $1 +%Y%m%d
then
mfdate=$(date -d $1 +%Y.%m.%d)
wget -q “http://lava.nationalgeographic.com/cgi-bin/pod/wallpaper.cgi?day=$(date -d $1 +%d)&month=$(date -d $1 +%m)&year=$(date -d $1 +%y)“ -O 00
else
echo “Unknown date format.“ >&2
exit $BADDATE
fi
fi
if [ $? -ne 0 ]
then
echo “Error in downloading picture.“ >&2
exit $WEBFAILURE
fi
findname=$(sed -n ‘s/.*\(sm\|lg\)_\([^>]*jpg\).*/lg_\2/p‘ 00 | sed -n ‘1 p‘)
if [ -z "findname" ]
then
echo “No picture found.“ >&2
exit $SEDFAILURE
fi
picname=$mfdate-$(sed -n ‘/<title>/ s/.*: \+\(.*\)<\/title>/\1/p‘ 00 | sed -e ‘s/[,"]//g‘ -e ‘s/ \+$//‘ -e ‘s/ /_/g‘ -e “s/’/'/g“ | tr ‘[:upper:]‘ ‘[:lower:]‘)
wget -q -c “http://lava.nationalgeographic.com/pod/pictures/$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)/
imdisplay $picdir/$(date -d $mfdate +%Y)/$picname.jpg &
fi
echo “Done.“
rm -f 00
exit 0