LinuxSir.cn,穿越时空的Linuxsir!

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

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

[复制链接]
 楼主| 发表于 2003-6-4 15:21:06 | 显示全部楼层

RPM-INSTALLER

感谢作者:liweiming
  1. #!/bin/sh
  2. DIALOG=Xdialog
  3. $DIALOG --backtitle "选项" \
  4.         --title "安装选项" \
  5.         --checklist "这是一个利用Xdialog制作的RPM包安装程序,如果有什么问题,
  6. 请联系Lee FICQ:1368 \n
  7. 请选择安装选项:" 0 0 0 \
  8.         "-i"  "默认普通安装" on \
  9.         "-U"    "升级安装" off \
  10.         " --force"   "强制" On \
  11.         " --nodeps"  "忽略依赖关系" off 2> /tmp/checklist.tmp.$$
  12. retval=$?
  13. ch1="-`grep -wo i /tmp/checklist.tmp.$$`"
  14. ch2="-`grep -wo U /tmp/checklist.tmp.$$`"
  15. ch3=`grep -wo force /tmp/checklist.tmp.$$`
  16. ch4=`grep -wo nodeps /tmp/checklist.tmp.$$`
  17. if [ "$ch1" == "-" ]; then
  18. ch1=""
  19. fi
  20. if [ "$ch2" == "-" ]; then
  21. ch2=""
  22. fi
  23. if [ "$ch3" != "" ]; then
  24. ch3="--$ch3"
  25. fi
  26. if [ "$ch4" != "" ]; then
  27. ch4="--$ch4"
  28. fi
  29. rm -f /tmp/checklist.tmp.$$
  30. case $retval in
  31.   0)
  32.         choice="$ch1 $ch2 $ch3 $ch4"
  33.   FILE=`$DIALOG --stdout --title "请选择一个RPM包文件" --fselect /home 0 0`
  34.         case $? in
  35.                 0)
  36.                   (rpm $choice -vh $FILE  )|$DIALOG --title "正在安装,请稍等....." --left --progress "RPM包正在安装:\n\n" 0 0 $RPM_MAX_DOTS_NUMBER $RPM_LEADING_MSG_LENGTH
  37.                    $DIALOG --title "Finished!" --msgbox "安装完毕!" 0 0
  38.                 ;;
  39.         esac
  40.         ;;
  41.   1)
  42.     echo "Bye!";;
  43.   255)
  44.     echo "Bye!";;
  45. esac
复制代码
 楼主| 发表于 2003-6-21 00:59:43 | 显示全部楼层

阿拉伯数字转换为大写数字的脚本[perl版本]

感谢作者Y00兄
[code]
#!/usr/bin/perl -w

# Chinese count method
# wrote by Lyoo
# iamlyoo@163.com
# 2003/06/22


# match @unit = qw / 个 拾 佰 仟 万 拾万 佰万 仟万/
@unit = qw / A B C D E F G H / ;

############################################################################
#
# receive user's input
#
############################################################################

$count = 0;
while ( $count < 1 ) {
print "lease input a number:";
chomp ($number = <STDIN>);
if ( $number =~ /^[-,+]?\d+\.?\d*$/ ) {
$count += 1;
} else {
print "It's not a Number!\n";
redo;
}
}

############################################################################
#
# create a number_array
#
############################################################################

# add a number to the number_string,
# so that the while-loop can get the "0" in the tail of the number_string.
$number_9 = $number."9";

# convert the number to a array.
$dot = "no";
while ($number_9) {
my $single = $number_9;
$single =~ s/([\d,.,+,-]).*/$1/;
$number_9 =~ s/[\d,.,+,-](.*)/$1/;
push (@number_array,$single);
$dot = "yes" if $single eq ".";
}

# delect the addition number.reverse the array.
pop @number_array;
@number_array = reverse @number_array;

# get number's sylobm.
$sylobm = "";
$sylobm = pop @number_array if $number_array[-1] =~ /[+,-]/;

# get the number_dot_string.
$number_dot_string = "";
if ($dot eq "yes") {
while (@number_array) {
$number_dot_string .= shift @number_array;
last if $number_dot_string =~ /\d\./;
};
$number_dot_string = reverse $number_dot_string;
};


#############################################################################
#
# creat a number_unit_array
#
#############################################################################

$min_unit = 9;
$j = 0;
$i = 0;
$n = 0;

foreach (@number_array) {
push (@number_unit_array,$unit[$i].$_);

if ($i == 0) {
$j++;
$min_unit = "on";
$switch = "on"
};

unless ($switch eq "off" || $_ eq "0") {
$min_unit = $n;
};

unless ($switch eq "off" || $min_unit eq "on") {
$number_unit_array[$min_unit] = ("Z" x ($j-1)).$number_unit_array[$min_unit];
$switch = "off";
}

$i++;
$n++;
$i = $i % 8;
}

#############################################################################
#
# modify the number_unit_string
#
#############################################################################

foreach (@number_unit_array) {
$number_unit_string .= $_;
}
$number_unit_string = reverse $number_unit_string;
$_ = $number_unit_string;
s/0[A-H]/0/g;
s/0+/0/g;
s/A//g;
s/0+$//;

#print "$_\n";

s/H(\d)G(\d)F(\d)E/D$1C$2B$3E/g;

s/H(\d)G(\d)F/D$1C$2F/g;
s/H(\d)G(\d)E/D$1C$2E/g;
s/H(\d)F(\d)E/D$1B$2E/g;
s/G(\d)F(\d)E/C$1B$2E/g;


s/H(\d)E/D$1E/g;
s/G(\d)E/C$1E/g;
s/F(\d)E/B$1E/g;

s/H(\d)F/D$1F/g;
s/G(\d)F/C$1F/g;

s/H(\d)G/D$1G/g;

$number_unit_string = "$sylobm"."$_"."$number_dot_string";

#############################################################################
#
# output the number_unit_string as a array
#
#############################################################################

# convert number_unit_string to array.
# it's ugly but without this action
# chinese can't output correct.
# I don't know why

while ($number_unit_string) {
my $single = $number_unit_string;
$single =~ s/([\w,.,+,-]).*/$1/;
$number_unit_string =~ s/[\w,.,+,-](.*)/$1/;
push (@number_unit_ok,$single);
}
#print "number_unit_ok is @number_unit_ok.\n";

foreach (@number_unit_ok) {
&print_chinese;
}
print "\n";

sub print_chinese {
if ($_ eq 0) {
print "零";
} elsif ($_ eq 1) {
print "壹";
} elsif ($_ eq 2) {
print "贰";
} elsif ($_ eq 3) {
print "叁";
} elsif ($_ eq 4) {
print "肆";
} elsif ($_ eq 5) {
print "伍";
} elsif ($_ eq 6) {
print "陆";
} elsif ($_ eq 7) {
print "柒";
} elsif ($_ eq 8) {
print "捌";
} elsif ($_ eq 9) {
print "玖";
} elsif ($_ eq A) {
print "个";
} elsif ($_ eq B) {
print "拾";
} elsif ($_ eq C) {
print "佰";
} elsif ($_ eq D) {
print "仟";
} elsif ($_ eq E) {
print "万";
} elsif ($_ eq F) {
print "拾万";
} elsif ($_ eq G) {
print "佰万";
} elsif ($_ eq H) {
print "仟万";
} elsif ($_ eq Z) {
print "亿";
} elsif ($_ eq "+") {
print "<正>";
} elsif ($_ eq "-") {
print "<负>";
} elsif ($_ eq ".") {
print "<点>";
}
}

#############################################################################
# the end of this script
############################################################################
 楼主| 发表于 2003-6-21 01:03:03 | 显示全部楼层

阿拉伯数字转换为大写数字的脚本[shell版本]

感谢作者 penny兄
  1. #!/bin/bash
  2. #penny_ccf@hotmail.com
  3. #it's ugly, but it works
  4. cconvert(){
  5. declare -a cnum;
  6. declare -a cmag;
  7. cnum[1]="壹"
  8. cnum[2]="贰"
  9. cnum[3]="叁"
  10. cnum[4]="肆"
  11. cnum[5]="伍"
  12. cnum[6]="陆"
  13. cnum[7]="柒"
  14. cnum[8]="拔"
  15. cnum[9]="玖"
  16. cnum[0]="零"
  17. cmag[0]=""
  18. cmag[1]="拾"
  19. cmag[2]="佰"
  20. cmag[3]="仟"
  21. cmag[4]="万"
  22. cmag[5]="拾"
  23. cmag[6]="百"
  24. cmag[7]="千"
  25. tempalpha="$1";
  26. ctempmag=$2;
  27. if [ $tempalpha == "00000000" ] ; then
  28. CSTR="";
  29. return 0;
  30. fi
  31. let templength="${#tempalpha}";
  32. CSTR="";
  33. for ((m=0;m<templength;m++))
  34. do
  35. tempi=${tempalpha:m:1};
  36. let tempj="$templength-$m-1";
  37. if ((( tempi == 0 )) && (( tempj ==4 ))); then
  38. CSTR=$CSTR"万";
  39. elif (( tempi == 0 )); then
  40. CSTR=$CSTR${cnum[0]};
  41. else
  42. CSTR=$CSTR${cnum[$tempi]}${cmag[$tempj]};
  43. fi
  44. done
  45. CSTR=$(echo $CSTR | sed -e 's/零零*/零/g' -e 's/零$//g' -e 's/零零零万//g');
  46. CMAG="";
  47. for ((m=0;m<ctempmag;m++))
  48. do
  49. CMAG=$CMAG"亿";
  50. done
  51. CSTR=$CSTR$CMAG;
  52. }
  53. alpha=$1;
  54. length=${#alpha};
  55. let k="$length/8";
  56. let modl="$length%8";
  57. MYSTR="";
  58. tempstr=${alpha:0:$modl};
  59. if ((modl>0)); then cconvert $tempstr $k; fi
  60. MYSTR=$MYSTR$CSTR;
  61. for ((i=0;i<k;i++))
  62. do
  63.   let pos="$i*8+modl";
  64.   tempstr=${alpha:$pos:8};
  65.   let tempmag="$k-$i-1";
  66.   cconvert $tempstr $tempmag;
  67.   MYSTR=$MYSTR$CSTR;
  68. done  
  69. echo $MYSTR | sed -e 's/亿零万/亿零/g'  -e 's/零万/万/g' -e 's/零亿/亿/g' -e 's/零零*/零/g' -e 's/零$//g';
复制代码
 楼主| 发表于 2003-7-5 01:25:32 | 显示全部楼层

一个拷贝进度条的实现

特别感谢作者:idkey兄
  1. #!/bin/sh
  2. # Last modified: 2003年07月05日 星期六 00时09分44秒 [test]
  3. SOURCE=$1
  4. TARGET=$2
  5. #CP=./fack_cp
  6. CP=cp
  7. $CP "$SOURCE" "$TARGET" &
  8. CPID=$!
  9. isalive(){
  10.         out=`ps -p $1 2> /dev/null`
  11.         return $?
  12. }
  13. while [ 1 ]; do {
  14.         SSIZE=`/bin/ls -l $SOURCE | gawk "{print \\\$5}"`
  15.         if [ -f $TARGET ]; then
  16.                 TSIZE=`/bin/ls -l $TARGET | gawk "{print \\\$5}"`
  17.         else
  18.                 TSIZE="0"
  19.         fi
  20.         PERCENT=`echo "scale=2; $TSIZE/$SSIZE*100" | bc -l`
  21.         RATE=`echo "scale=0; 63*$PERCENT/100" | bc -l`
  22.         BLUE="\\033[3;44m"
  23.         NORMAIL="\\033[0;39m"
  24.         BAR=$BLUE
  25.         i=0
  26.         while [ $i -le 62 ]; do
  27.                 [ $i = $RATE ] && BAR=$BAR"\\033[7;39m"
  28.                 BAR=$BAR" "
  29.                 let i=$i+1
  30.         done
  31.         BAR=$BAR$NORMAIL
  32.         echo -en "\r$BAR ${PERCENT}%"
  33.         if ! isalive "$CPID"; then echo -en "\n"; exit; fi
  34.         sleep 1
  35. }
  36. done
复制代码
 楼主| 发表于 2003-7-6 16:44:55 | 显示全部楼层

shell命令中文帮助

特别感谢作者: ilmargaret兄

脚本很简单却很实用,尤适合于编程初学者和英语太菜的人.
功能: 任何时候在命令行里输入你想查找的命令名称,该脚本都会显示此命令的详细用法,而且是中文的哦.
脚本名称:showme
  1. csplit -sf help /root/scripts/functionlib/datafile.txt /"command: $1"/ '/over/' 2>errors
  2. if [ $? -eq 0 ]
  3. then
  4. more help01
  5. rm help??
  6. else
  7. echo Sorry,the commnand $1 was not found in your datafile!
  8. fi
复制代码
系统设置:将附件和脚本放在/root/scripts/functionlib/下,然后在/etc/profile中做如下改动:

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC 改为:
export PATH=${PATH}:/root/scripts/functionlib USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC ,这样就可以在任何目录下执行该脚本了,就象执行man命令一样.

备注: 附件datafile里你可以不断的添加新的命令,只要添加的命令与我原来的格式一致即可,时间久了就变成手册了,随时查阅,对shell编程很有用!
发表于 2003-7-12 13:19:20 | 显示全部楼层
idkey 兄的有bug.
发表于 2003-8-3 10:35:51 | 显示全部楼层
This shell do a statistics on the online pcs.
  1. #!/bin/sh
  2. #This shell do a statistics on the online pcs.
  3. #The ip range is from $SURFIX.$FROM to $SURFIX.$TO
  4. #The best way is using argument to define the range and log file
  5. #I didn't do that
  6. SURFIX=192.168.32
  7. FROM=1
  8. TO=244
  9. LOGFILE=log
  10. let TO=TO+1
  11. var=$FROM
  12. #log file save the results
  13. #If we has do that before, contine the statistics
  14. #if we does do that, create a new statistics
  15. if [ ! -f $LOGFILE ];then
  16. #It seems that this while is not necessary, but make this shell
  17. #more easy to understand
  18.         while [ $var -lt $TO ];
  19.         do
  20.                 set ip$var=0;
  21.                 let var=var+1
  22.         done
  23. else
  24.         for tmp in `awk "{ print \\\$2 }" $LOGFILE`
  25.         do
  26.                 let ip$var=$tmp
  27.                 let var=var+1
  28.         done
  29. fi
  30. #ip$var save the data how many times remote host is on line
  31. #use ping command to find out
  32. var=$FROM
  33. while [ $var -lt $TO ]
  34. do
  35.         if ping $SURFIX.$var -c 1 -w 1;then
  36.                 let ip$var=ip$var+1
  37.         fi
  38.         let var=var+1
  39. done
  40. #create a new empty log file
  41. #and save new data
  42. >$LOGFILE
  43. var=$FROM
  44. while [ $var -lt $TO ]
  45. do
  46.         echo $SURFIX.$var $[ip$var] >>$LOGFILE
  47.         let var=var+1
  48. done
复制代码
发表于 2003-8-3 16:27:47 | 显示全部楼层
OpenLab:/ # cat log
192.168.32.1 76
192.168.32.2 0
192.168.32.3 0
192.168.32.4 0
192.168.32.5 0
192.168.32.6 0
192.168.32.7 0
192.168.32.8 0
192.168.32.9 16
192.168.32.10 76
192.168.32.11 76
192.168.32.12 45
192.168.32.13 22
192.168.32.14 20
192.168.32.15 0
192.168.32.16 41
192.168.32.17 0
192.168.32.18 0
192.168.32.19 0
192.168.32.20 0
192.168.32.21 25
192.168.32.22 19
192.168.32.23 0
192.168.32.24 0
192.168.32.25 63
192.168.32.26 0
192.168.32.27 17
192.168.32.28 63
192.168.32.29 0
192.168.32.30 0
192.168.32.31 0
192.168.32.32 0
192.168.32.33 0
192.168.32.34 0
192.168.32.35 1
192.168.32.36 0
192.168.32.37 0
192.168.32.38 16
192.168.32.39 0
192.168.32.40 0
192.168.32.41 49
192.168.32.42 9
192.168.32.43 0
192.168.32.44 0
192.168.32.45 21
192.168.32.46 0
192.168.32.47 0
192.168.32.48 0
192.168.32.49 0
192.168.32.50 0
192.168.32.51 0
192.168.32.52 56
192.168.32.53 0
192.168.32.54 0
192.168.32.55 63
192.168.32.56 0
192.168.32.57 63
192.168.32.58 0
192.168.32.59 22
192.168.32.60 0
192.168.32.61 33
192.168.32.62 31
192.168.32.63 62
192.168.32.64 63
192.168.32.65 26
192.168.32.66 57
192.168.32.67 0
192.168.32.68 0
192.168.32.69 46
192.168.32.70 0
192.168.32.71 0
192.168.32.72 0
192.168.32.73 56
192.168.32.74 63
192.168.32.75 0
发表于 2003-8-11 01:46:14 | 显示全部楼层

我也来一个:用iptables自动封禁多线程连接ftpd的ip,hoho

使用metalog和iptables配合,对多线程连接ftp服务器的ip进行暂时的封禁

metalog.conf里ftpd的配置如下
  1. FTP Server :
  2.   program  = "pure-ftpd"
  3.   logdir   = "/var/log/ftpd"
  4.   command  = "/usr/sbin/ftpdchk" #将log分三个参数提交给处理程序,分别是:时间、进程名、内容
复制代码
处理程序如下:
  1. #!/bin/bash
  2. # ftpdchk - check and block xx ip
  3. # [email]benzy@bbs.pku.ecu.cn[/email], 2003/8/1
  4. # cp file to /usr/sbin/ftpdchk
  5. #set -x
  6. # Too many connections (1) from this IP: [211.71.208.1]
  7. ip="`echo "$3" | grep -oe '\[[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\]'`"
  8. if [ -n "$ip" ];then
  9.         log="/var/log/ftpd/ftpdchk.log"
  10.         ip=${ip#[};ip=${ip%]}
  11.         time=$1
  12. #       ftpd=$2
  13.         # iptable配置略。总之建一个ftp链专门处理ftp请求:)
  14.         /sbin/iptables -I ftp -j DROP -p tcp --dport ftp -s $ip
  15.         # 5分钟后解封
  16.         echo "/sbin/iptables -D ftp -j DROP -p tcp --dport ftp -s $ip" | at now +5minutes
  17.         echo "$ip       $time" >>$log
  18. fi
复制代码
发表于 2003-8-15 00:16:05 | 显示全部楼层

批量改当前目录下文件后缀

  1. #!/bin/sh
  2. for filen in *
  3. do
  4. fn=`echo $filen |cut -d "." -f 1`
  5. ext=`echo $filen |cut -d "." -f 2`
  6. if [ $ext = $1 ];then
  7. echo "$filen -> $fn.$2"
  8. mv $filen  $fn.$2      
  9. fi
  10. done
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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