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

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

  2. #trim the charactor in head and tail

  3. L_trim()
复制代码
  该函数取掉变量rand_time左边所有的空格:
  1. tmp=${rand_time:0:1}

  2. while [ $tmp = 0 ]
  3. do
  4. rand_time=${rand_time:1}

  5. tmp=${rand_time:0:1}
  6. done
  7. }

  8. R_trim()
复制代码
  {该函数取掉变量rand_time右边所有的空格:
  1. {
  2. Len=${#rand_time}
  3. let "sLen =Len -1"
  4. tmp=${rand_time:sLen:1}
  5. while [ $tmp = 0 ]
  6. do
  7. rand_time=${rand_time:0:$sLen}
  8. Len=${#rand_time}
  9. let "sLen =Len -1"
  10. tmp=${rand_time:sLen:1}
  11. done
  12. }
  13. #get current time as random
  14. rand_time=`date '+%N'`
  15. #trim the "0" from the var
  16. #rand_time=${rand_time:0:6}
  17. L_trim
  18. R_trim
  19. #get filecount of $1
  20. fcount=`ls $1 | wc -l`
  21. echo "fcount $fcount"
  22. #get a random number between 1 and fcount
  23. rand_num=$(($rand_time % $fcount ))
  24. echo $rand_num

  25. tmp=1
  26. for file in $1/*.jpg
  27. do
  28. if [ $tmp -lt $rand_num ]
  29. then
  30. let "tmp=tmp+1"
  31. continue
  32. fi
复制代码
  可以将随机取出的图片设为桌面:
  1. #qiv -Tx $HOME/.wallpaper.jpg
  2. echo "file:$file"
  3. break;
  4. done
复制代码

作者: coolentboy   发布时间: 2006-10-16

坐下来慢慢学习

作者: 邱建元   发布时间: 2006-10-16

作者: chameleon   发布时间: 2006-10-16

以后多发些这样的啊

作者: spark1313   发布时间: 2006-10-16

zai lai yici

作者: spark1313   发布时间: 2006-10-19

:0L :0L

作者: michrykitty   发布时间: 2006-10-19

对 坐下来慢慢学习啊

作者: pomerl   发布时间: 2007-12-25

。。。没懂

作者: 逍遥の石头   发布时间: 2010-02-28

混分下资料,不过也来观摩一下高手

作者: liu351128   发布时间: 2010-03-04

最后一个挺好玩的

作者: xtygaht   发布时间: 2010-03-04

有点看不懂

作者: 坏boy   发布时间: 2010-03-04

It was kind of interesting!

作者: jan729   发布时间: 2010-04-17

study

作者: axbyc123   发布时间: 2010-04-18

不错

作者: axbyc123   发布时间: 2010-04-19

学习了

作者: axbyc123   发布时间: 2010-04-19

谢谢分享

作者: 飞虎小白龙   发布时间: 2010-04-19

学习学习啊!!!

作者: 飞虎小白龙   发布时间: 2010-05-21

正好坐下慢慢学习啊!!!

作者: hurricane668   发布时间: 2010-05-24

看得迷糊糊的

作者: daidengke   发布时间: 2010-05-24

学习一下

作者: lp518   发布时间: 2010-05-30

不错啊

作者:   发布时间: 2010-12-05