Linux操作系统的Shell编程脚本欣赏

http://tech.ddvip.com   2007年03月11日    社区交流

本文详细介绍Linux操作系统的Shell编程脚本欣赏

  自己买了一个数码相机,柯达7590。自己拍了一些照片。欣赏时,感觉自己还有点专业水准,拿来作桌面。这么多图片到底选那一个,放弃那一个呢?最后决定,都选,都用来左桌面,让系统每30分钟自动换一个。利用系统子带的工具cron. 再加上自己编写的脚本,很容易实现。下面给出从文件夹中随机取出一副图片的脚本程:

#!/bin/bash
#trim the charactor in head and tail
L_trim()

  该函数取掉变量rand_time左边所有的空格:

tmp=${rand_time:0:1}
while [ $tmp = 0 ]
do
rand_time=${rand_time:1}
tmp=${rand_time:0:1}
done
}
R_trim()

  {该函数取掉变量rand_time右边所有的空格:

{
Len=${#rand_time}
let "sLen =Len -1"
tmp=${rand_time:sLen:1}
while [ $tmp = 0 ]
do
rand_time=${rand_time:0:$sLen}
Len=${#rand_time}
let "sLen =Len -1"
tmp=${rand_time:sLen:1}
done
}
#get current time as random
rand_time=`date '+%N'`
#trim the "0" from the var
#rand_time=${rand_time:0:6}
L_trim
R_trim
#get filecount of $1
fcount=`ls $1 | wc -l`
echo "fcount $fcount"
#get a random number between 1 and fcount
rand_num=$(($rand_time % $fcount ))
echo $rand_num
tmp=1
for file in $1/*.jpg
do
if [ $tmp -lt $rand_num ]
then
let "tmp=tmp+1"
continue
fi

  可以将随机取出的图片设为桌面:

#qiv -Tx $HOME/.wallpaper.jpg
echo "file:$file"
break;
done

来源:赛迪网    作者:tibetman    责编:豆豆技术应用

正在加载评论...