先看一下下面这一段脚本:
[bash]
wh=`xrandr |grep current|awk -F"current" '{print $2}'|awk -F"," '{print $1}'|sed 's/\ //g'`
w=`echo ${wh%%'x'*}`
h=`echo ${wh##*'x'}`
choiced=0
f_wall=0
echo ' '
while [ $choiced = 0 ]; do
echo '***********************************'
echo '** 将要搜索可用壁纸 **'
echo '** 请选择 壁纸搜索方式 **'
echo '***********************************'
WML_LIST=('搜索长宽比和你的屏幕一致的' '搜索所有壁纸,包括长宽比不一致的')
TOTAL_WMS=1
for ((i=0; i<=$TOTAL_WMS; i++))
do
echo "** $i.${WML_LIST[$i]} "
done
echo ' '
read -n 1 f_wall
echo ' '
echo "你选择了:${WML_LIST[$f_wall]} )"
echo ' '
done
echo '**********************************'
echo '** 按任意键 搜寻可用壁纸 **'
echo '** 请耐心等待 **'
echo '**********************************'
echo ' '
read -n 1 choice_wml
echo ' '
m=0
other_wall=('/usr/share/wallpapers/bluebird-2010-08-1920x1200-xubuntu.png' \
'/usr/share/wallpapers/greybird-wall-1920x1200.png' \
'/usr/share/wallpapers/kde-default.png' \
'/usr/share/backgrounds/gnome/TwoWings.jpg' \
'/usr/share/backgrounds/gnome/Wood.jpg' \
'/usr/share/backgrounds/gnome/YellowFlower.jpg' \
'/usr/share/xfce4/backdrops/ubuntustudio-olis.jpg' \
'/usr/share/xfce4/backdrops/xfce-stripes.png' \
'/usr/share/xfce4/backdrops/xubuntu-precise-right.png')
for ((i=0; i<179; i++))
do
if [ -f ${other_wall[$i]} ] ; then
if [ $f_wall == "0" ]; then
wh1=`identify ${other_wall[$i]}`
wh2=`echo "$wh1" | awk '{ print $3; }'`
w2=`echo ${wh2%%'x'*}`
h2=`echo ${wh2##*'x'}`
if [ $[ $w*1000/$w2 ] -eq $[ $h*1000/$h2 ] ] ; then
echo "wallpapers:${other_wall[$i]}">>~/.icon-DE/desk.rc
echo "发现壁纸:${other_wall[$i]}"
m=$[ $m+1 ]
elif [ $[ $w*100/$h ] -eq 125 ] || [ $[ $w*100/$h ] -eq 133 ] ; then
if [ $[ $w2*100/$h2 ] -eq 125 ] || [ $[ $w2*100/$h2 ] -eq 133 ] ; then
echo "wallpapers:${other_wall[$i]}">>~/.icon-DE/desk.rc
echo "发现壁纸:${other_wall[$i]}"
m=$[ $m+1 ]
fi
else
if [ $[ $w2*100/$h2 ] -gt 133 ] ; then
echo "wallpapers:${other_wall[$i]}">>~/.icon-DE/desk.rc
echo "发现壁纸:${other_wall[$i]}"
m=$[ $m+1 ]
fi
fi
else
echo "wallpapers:${other_wall[$i]}">>~/.icon-DE/desk.rc
echo "发现壁纸:${other_wall[$i]}"
m=$[ $m+1 ]
fi
fi
done
[/bash]
脚本说明:
先用xrandr读出你的屏幕分辨率,将长宽存入变量w、h中。
里面是文件列表,现在不是动态生成的,是直接写到shell里面的。他列出的是“/usr/share/wallpapers/”、“/usr/share/backgrounds/”、“/usr/share/backgrounds/gnome/”、“/usr/share/xfce4/backdrops/”这几个目录下面的文件。other_wall=(...
然后是一个循环,判断图片文件是否可访问,并用“identify”命令读出图片的尺寸大小,将长宽存入w2、h2中,用于比较图片是否适合做桌面壁纸。适合的写进配置文件。
现在的缺陷:不是动态生成数组,循环次数也不是动态的。
问题:
动态生成数组的那部分shell脚本,怎么写?
求高手解决!!
自己解决了。数组部分写成下面样子。
[bash]
x=0
for filename in /usr/share/wallpapers/*.jpg ;
do
other_wall[$x]=$filename
let "x+=1"
done
for filename in /usr/share/wallpapers/*.png ;
do
other_wall[$x]=$filename
let "x+=1"
done
for filename in /usr/share/backgrounds/*.jpg ;
do
other_wall[$x]=$filename
let "x+=1"
done
for filename in /usr/share/backgrounds/*.png ;
do
other_wall[$x]=$filename
let "x+=1"
done
for filename in /usr/share/xfce4/backdrops/*.jpg ;
do
other_wall[$x]=$filename
let "x+=1"
done
for filename in /usr/share/xfce4/backdrops/*.png ;
do
other_wall[$x]=$filename
let "x+=1"
done
for filename in /usr/share/wallpapers/*.JPG ;
do
other_wall[$x]=$filename
let "x+=1"
done
for filename in /usr/share/wallpapers/*.PNG ;
do
other_wall[$x]=$filename
let "x+=1"
done
for filename in /usr/share/backgrounds/*.JPG ;
do
other_wall[$x]=$filename
let "x+=1"
done
for filename in /usr/share/backgrounds/*.PNG ;
do
other_wall[$x]=$filename
let "x+=1"
done
for filename in /usr/share/xfce4/backdrops/*.JPG ;
do
other_wall[$x]=$filename
let "x+=1"
done
for filename in /usr/share/xfce4/backdrops/*.PNG ;
do
other_wall[$x]=$filename
let "x+=1"
done
[/bash]