LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: KornLee

【shell脚本欣赏区】:[展示你的作品的好去处!欢迎投帖]

[复制链接]
发表于 2003-9-9 21:21:21 | 显示全部楼层
让scroll lock指示灯闪烁,可以起提醒作用,没人会用tty19吧?
#!/bin/bash
#email:jyhln@163.com
#msn:jyhln@msn.com
#oicq:2899589
if [ $# != 1 ];then
echo usage 0 time
exit 0
fi
n=1
#sleep $1&&let "n=0"&
while [ $n -le $1 ]
do
n=`expr  $n + 1`
setleds -L +scroll < /dev/tty19
#setleds -L -caps < /dev/tty19
sleep 0.5
setleds -L -scroll < /dev/tty19
#setleds -L +caps < /dev/tty19
sleep 0.5
done
#setleds -L -caps < /dev/tty19
发表于 2003-9-9 21:22:16 | 显示全部楼层
有时候会下载好多文件,知道有重复的,但不知道是那些。下面的脚本可以找出当前目录或指定目录下所有大小相等的文件,然后配合后面两个脚本比较二进制文件。

#!/bin/bash
#email:jyhln@163.com
#msn:jyhln@msn.com
#oicq:28995895
echo -n>/tmp/result_001.tmp
echo -n>/tmp/result.tmp
if [ $# -eq 0 ];then
        dir_t=`pwd`
else
        dir_t=`pwd $@`
fi
ls -Rlp -o --full-time $dir_t>/tmp/filelist_001.tmp
#create tmp file

flag=100

for line in `cat /tmp/filelist_001.tmp`;do
#path
if [ "`echo $line|grep ^/|grep `" ];then
        flag=100
        dir_swap=$line
        continue
fi

#size
if [ "`echo $line|grep ^[-s][r-][-w][-x][-r]`" -a $flag -gt 10 ];then
        flag=2
fi

flag=` expr $flag + 1 `
if [ $flag -eq 6 ];then
        size=$line
        continue
fi
   #file
if [ $flag -eq 10 ];then
        flag=100
        echo -e $"$size \t$dir_swap\b/$line">>/tmp/result_001.tmp
        #echo -e $"$size \t$dir_swap $line"
fi

done

#if have the same size

for byte in `cut /tmp/result_001.tmp -f 1`
        do
        counter=`grep -w ^"$byte" /tmp/result_001.tmp|wc -l`
        
        if [ $counter -gt 1 ];then
                test_n=`grep ^"$byte " /tmp/result.tmp`
                if [ ! "$test_n" ];then
                        grep ^"$byte " /tmp/result_001.tmp>>/tmp/result.tmp
                        echo>>/tmp/result.tmp
                fi

        fi
done

cat /tmp/result.tmp
rm -f /tmp/result.tmp /tmp/filelist_001.tmp /tmp/result_001.tmp
发表于 2003-9-9 21:23:00 | 显示全部楼层
下面的脚本简略判断二进制文件是否相同,只采样一次,要修改采样位置大小,只能修改脚本。
#!/bin/bash
#email:jyhln@163.com
#msn:jyhln@msn.com
#oicq:28995895
#hexdump -x -n 16 -s 100
n=16        ##采样大小
s=200        ##采样位置

if [ ! $# = 2 ];then
echo usage: $0 hexfile1 hexfile2
exit 0
fi

hex1=`hexdump -x -n $n -s $s $1`
hex2=`hexdump -x -n $n -s $s $2`
if [ "$hex1" != "$hex2" ];then
echo different files
fi

下面的脚本比较两个二进制文件
#!/bin/bash
#email:jyhln@163.com
#msn:jyhln@msn.com
#oicq:28995895
#hexdump -x -n 16 -s 100

if [ ! $# = 2 ];then
echo usage: $0 hexfile1 hexfile2
exit 0
fi

hexdump -x $1>/tmp/hexdiff_001.tmp
hexdump -x $2>/tmp/hexdiff_002.tmp
diff /tmp/hexdiff_001.tmp /tmp/hexdiff_002.tmp
发表于 2003-9-9 21:23:53 | 显示全部楼层
下面的脚本是一个简单的rpm安装,基于X的。
#!/bin/bash
#email:jyhln@163.com
#msn:jyhln@msn.com
#oicq:28995895
if [ $# != 1 ];then
echo usage:  $0 file.rpm
exit 0
fi

function my_install(){
kdialog --msgbox "install $1......"&
rpm -ihv $1 2>/tmp/rpm_error_log
rpm_exno=$?
if [ $rpm_exno -eq 0 ];then
        killall kdialog&&kdialog --msgbox "sucsess"
elif grep "is already installed" /tmp/rpm_error_log
        then
        kdialog --yesno "`grep "is already installed" /tmp/rpm_error_log`,updata?"
        if [ $? -eq 0 ];then
                rpm -Uhv $1 2>/tmp/rpm_error_log
                rpm_exno=$?
                if [ $rpm_exno -eq 0 ];then
                killall kdialog&&kdialog --msgbox "sucsess"
                else
                killall kdialog&&kdialog --error "`cat /tmp/rpm_error_log`"
                fi
        else
        killall kdialog
        exit 0
        fi
else
        killall kdialog&&kdialog --error "`cat /tmp/rpm_error_log`"
fi
rm -f /tmp/rpm_error_log 2>/dev/null
}

kdialog --yesno "this will install $1,continue?"
sele=$?
case $sele in
        0)my_install $1;;
        1)test;;
        *)test
esac
 楼主| 发表于 2003-9-9 22:59:56 | 显示全部楼层
多谢jyh兄
以后发帖时注意用
  1. 代码
复制代码
的方式,这样便于阅读 :thank
发表于 2003-9-10 18:38:41 | 显示全部楼层
一个找字符的脚本

作者:777

源码
-----------------------------------------------------------------
#!/usr/local/bin/bash
#A bash script to find a keyword.
#Name findkey
while true;do
echo "Input directory: ( Will exit if you press e  ! ) "
read dir
if [ $dir = "e" ];then echo "The process already exit !";exit 0;fi
if [ -e $dir ];then
echo -n "Input keyword:   "
read "key"
grep -R "$key" $dir 2>/dev/null|less
else "The directory is not exist ! ";fi
done
#祝大家中秋节快乐~~~~~
------------------------------------------------------------------

发表于 2003-9-11 13:20:18 | 显示全部楼层
一个改变系统启动运行等级的脚本,

作者:777

# cat chlevel
-----------------------------------------------------------
#!/bin/bash
# change the run level  at the boot time.
#time :2003.8.5
grep id /etc/inittab
echo "Are you want change the runlevel at the boot ? (y/n) "
read num
if [ $num = "y" ];then
if [ -e /etc/inittab ]
then echo "Input a number for runlevel at system boot:"
read level
if [[ $level = ^[0-9] || ${#level} > 1 ]]
then echo "The number is wrong."
else
if [[ $level >0 && $level <6 ]]
then cat /etc/inittab|sed 's/id:[12345]:in/id:'$level':in/'>h&&cat h>/etc/inittab
rm -fr h&&chmod 644 /etc/inittab
else echo "The number is wrong!"
fi;fi
else echo "File not found!"
fi;else echo "The run level have not change!"
fi
grep id /etc/inittab
---------------------------------------------------------------
 楼主| 发表于 2003-9-11 15:26:19 | 显示全部楼层
最初由 777 发表
一个改变系统启动运行等级的脚本,

呵呵,我也来一个图形界面的;)
  1. [/home/javalee/myshell]cat chlevel
  2. #!/bin/ksh
  3. #name:chlevel
  4. #
  5. file=/etc/inittab
  6. title="脚本:chlevel"
  7. oldlevel=$(runlevel|cut -c3)
  8. info="当前运行级别:$oldlevel\n请输入启动级别[1-5][按q键离开]"
  9. id=$(whoami)
  10. function error {
  11. xdialog --title $title --no-buttons --infobox "错误!" 6 30
  12. }
  13. function finish {
  14. xdialog --title $title --no-buttons --infobox "启动级别修改完毕,系统将重启" 6
  15. 30
  16. reboot
  17. }
  18. function quit {
  19. xdialog --title $title --no-buttons --infobox "离开本程序!" 6 30
  20. }
  21. xdialog --title "身份验证" --no-cancel --inputbox "请输入用户名" 6 30 2>p$$
  22. p=$(cat p$$)
  23. if [[ $p != $id ]]
  24.         then xdialog  --infobox "$p:为非法用户!退出!!!" 6 30
  25.         exit 1
  26. fi
  27. while :
  28.         do
  29.         xdialog --title $title --no-cancel --inputbox "$info" 10 36 2>tmp$$
  30.         input=$(cat tmp$$)
  31.         case $input in
  32.         1|2|3|4|5)      vi -e -s -c ":%s/id:$oldlevel:init/id:$input:init/" -c ":wq!" $file
  33.                         finish
  34.                         exit 0;;
  35.         q)              quit
  36.                         break;;
  37.         *)              error
  38.                         continue;;
  39.         esac
  40. done
  41. rm tmp$$ p$$
复制代码
xdialog可以用系统本身的dialog来代替.
 楼主| 发表于 2003-9-11 20:08:05 | 显示全部楼层

一个比较两个文件的脚本[转]

作者:bjgirl
来自:www.chinaunix.net
  1. cat dfile
  2. #!/bin/ksh
  3. #scriptname:dfile
  4. function Usage {
  5. echo "Usage: dfile [-x|-y|-z] file1 file2"
  6. }
  7. (($# != 3 )) && { Usage; exit 1; }
  8. for i in $(cat $2);do
  9.         for j in $(cat $3);do
  10.         echo $i $j
  11.         done
  12. done>a$$
  13. same=$(awk '$1==$2 {print $1}' a$$)
  14. X=$(echo 两个文件相同的行是:
  15. echo $same|tr ' ' '\n')
  16. diff1=$(awk '$1 != $2 {print $1}' a$$|uniq -d )
  17. Y=$(echo "文件$2中存在而文件$3中不存在的行是:"
  18. echo $same $diff1|tr ' ' '\n'|sort|uniq -u
  19. echo "\n")
  20. for n in $(cat $3);do
  21.         for m in $(cat $2);do
  22.         echo $n $m
  23.         done
  24. done>b$$
  25. diff2=$(awk '$1 != $2 {print $1}' b$$|uniq -d )
  26. Z=$(echo "在文件$3中存在而文件$2中不存在的行是:"
  27. echo -n $diff2 $same|tr ' ' '\n'|sort|uniq -u
  28. echo "\n")
  29. while getopts :xyz: arg
  30. do
  31.         case $arg in
  32.         x)      echo $X|tr ' ' '\n';;
  33.         y)      echo $Y|tr ' ' '\n';;
  34.         z)      echo $Z|tr ' ' '\n';;
  35.         \?)     echo "选项错误!!!";Usage;;
  36.         esac
  37. done
  38. rm a$$ b$$
复制代码
 楼主| 发表于 2003-9-20 15:23:17 | 显示全部楼层

一个定时弹出提示窗口的脚本

  1. [/home/javalee/myshell]cat hello
  2. #!/bin/ksh
  3. #脚本名:hello
  4. #描述:定时提醒用户是否该休息啦.....:-}
  5. #作者:javalee
  6. #日期:2003/9/20
  7. function _time {
  8. n=1
  9. while ((n<600))
  10. do
  11.         sleep 1
  12.         ((n+=1))
  13. done
  14. }
  15. while true
  16. do
  17.         _time
  18.         info="$USERNAME\n你该休息休息啦~~ :) "
  19.         tishi="提示信息"
  20.         xdialog --title $tishi --beep --msgbox $info 10 10
  21.         xdialog --title $tishi --no-buttons --beep --inputbox "休息吗[y/n]?" 4 20 2>tmp$$       
  22.         input=$(cat tmp$$)
  23.         case $input in
  24.         y)      break ;;
  25.         n)      continue;;
  26.         *)      xdialog --title $tishi --no-buttons --infobox "错误的选择!" 4 20;continue;;
  27.         esac
  28. rm tmp$$
  29. done
复制代码
xdialog可用系统自身的dialog代替 ;)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表