[已解决]求一段shell脚本,分析壁纸文件的,求高手协助!!

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
ljj_jjl2008
论坛版主
帖子: 14256
注册时间: 2007-09-16 8:29

[已解决]求一段shell脚本,分析壁纸文件的,求高手协助!!

#1

帖子 ljj_jjl2008 » 2012-06-16 10:05

求一段shell脚本,分析壁纸文件的,求高手协助!!
先看一下下面这一段脚本:
[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中。
other_wall=(...
里面是文件列表,现在不是动态生成的,是直接写到shell里面的。他列出的是“/usr/share/wallpapers/”、“/usr/share/backgrounds/”、“/usr/share/backgrounds/gnome/”、“/usr/share/xfce4/backdrops/”这几个目录下面的文件。

然后是一个循环,判断图片文件是否可访问,并用“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]
cao627
帖子: 992
注册时间: 2007-12-05 10:57
系统: ubuntu14.04
来自: 金山

Re: [已解决]求一段shell脚本,分析壁纸文件的,求高手协助!!

#2

帖子 cao627 » 2012-06-16 11:29

代码: 全选

a=(`find $@  -name *.png`)
for ((i=0; i<${#a[@]}; i++))
do
echo ${a[i]}
done
脚本后输入存放图片文件的绝对路径作为脚本的参数
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

Re: [已解决]求一段shell脚本,分析壁纸文件的,求高手协助!!

#3

帖子 枫叶饭团 » 2012-06-16 12:19

囗囗囗囗 cet-4
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

Re: [已解决]求一段shell脚本,分析壁纸文件的,求高手协助!!

#4

帖子 YeLee » 2012-06-16 12:45

枫叶饭团 写了:囗囗囗囗 cet-4
◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
头像
naturalaw
帖子: 1360
注册时间: 2011-09-06 22:04

Re: [已解决]求一段shell脚本,分析壁纸文件的,求高手协助!!

#5

帖子 naturalaw » 2012-06-17 2:57

无语,数组部分没那么麻烦吧。不过2楼给出答案了。 :em01
  • The eternal law
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

Re: [已解决]求一段shell脚本,分析壁纸文件的,求高手协助!!

#6

帖子 tusooa » 2012-06-18 13:09

one-liner

代码: 全选

other_wall=(/usr/share/{backgrounds,wallpapers,xfce4/backdrops}/*.{jpg,png,gif})
没有必要支持大写文件名。

代码: 全选

] ls -ld //
头像
Methuselar
帖子: 122
注册时间: 2009-06-04 12:06
联系:

Re: [已解决]求一段shell脚本,分析壁纸文件的,求高手协助!!

#7

帖子 Methuselar » 2012-06-18 13:27

一楼第一句还不如

代码: 全选

wh = `xrandr | awk -v RS=','  '/current/ {print $2"x"$4}'`
为什么要取尺寸呢... feh之类的--bg-scale --bg-center --bg-max不够用么??

代码: 全选

find ~/wallpaper -type f -print0 | shuf -n1 -z | xargs -0 feh --bg-center
Mea Culpa!
回复