Shell 脚本欣赏区
MichaelBibby
|
1#
发表于 2004-10-14 22:16
|
MichaelBibby
|
1#
MichaelBibby 发表于 2004-10-14 22:16
Shell 脚本欣赏区
此贴用来收集兄弟们原创或者收集珍藏的一些好段子,为新手兄弟们提供好的学习范例。
转贴请注明出处,原创也请写上自己的ID,便于兄弟们转载 鼓励原创! 欢迎兄弟们多多发贴,最好能够加上一些必要的注释、说明。 代码请用code格式来格式化。 发表的段子请统一一下格式: [code:1] #!/bin/sh # ############################################### # Purpose: 简单说明一下这个脚本是干啥滴 # Author: 作者( 你滴ID),E-Mail(比如俺滴:michaelbibby AT sohu DOT com ) # From: 原出处( 比如,www.LinuxFans.org ) ############################################### Your Code Here ! [/code:1] E-Mail地址可以不写,看兄弟们自己的意愿。如果写的话,请一定注意格式,免得贴上来以后尽收到垃圾邮件。 PS:无关帖子一律砍掉 |
MichaelBibby
|
2#
MichaelBibby 发表于 2004-10-18 10:47
抛砖引玉 :
[quote:60a86c6bb6="KanKer"]受linuxsir上哈蜜瓜的启发,重新写了一个自动加载分区的脚本,条件所限,对scsi的检测还不太完善,请大家修正: 1、将下面脚本保存为/usr/sbin/fstype: [code:1]#!/bin/sh #节选自哈蜜瓜写的自动加载分区脚本。 FILE="$(LANG=C LC_ALL=C LC_MESSAGES=C file -Lkbs "$1")" if [ "$FILE" = "data" ]; then # could still be ReiserFS, since "file" only reads 64k in Debian FILE="$(dd if="$1" skip=16397 ibs=4 count=2 2>/dev/null)" fi case "$FILE" in *[Rr][Ee][Ii][Ss][Ee][Rr]*) echo "reiserfs";; # *ISO\ 9660*) echo "iso9660";; *[Mm][Ii][Nn][Ii][Xx]*) echo "minix";; *[Xx][Ff][Ss]*) echo "xfs";; *[Jj][Ff][Ss]*) echo "jfs";; *[Ee][Xx][Tt]3*) echo "ext3";; *[Ee][Xx][Tt]2*) echo "ext2";; # *[Ss][Ww][Aa][Pp]*) echo "swap";; *[Nn][Tt][Ff][Ss]*) echo "ntfs";; *[Ff][Aa][Tt]*) echo "vfat";; # *) echo "auto";; esac [/code:1] 2、将下面脚本放到/etc/rc.d/rc.local里(用ML1.2pre6以后系统的朋友可加在/etc/rc.d/rc.magic里),以便每次开机自动运行: --------------------------------------------------- [code:1] #自动检测加载分区 if [ -f /etc/profile.d/lang.sh ]; then . /etc/profile.d/lang.sh case $LANG in zh_CN*) MOUNTOPTIONS="iocharset=cp936" ;; zh_TW*) MOUNTOPTIONS="iocharset=cp950" ;; ja_JP*) MOUNTOPTIONS="iocharset=cp932" ;; ko_KR*) MOUNTOPTIONS="iocharset=cp949" esac fi if [ ! -f /etc/fstab ];then touch /etc/fstab fi #for ide devices for i in $(ls -d /proc/ide/h*);do if cat $i/media |grep -i disk >/dev/null;then for j in $(/sbin/fdisk -l /dev/$(basename $i)|cut -c1-10|grep hd);do LABEL=$(e2label $j 2>/dev/null) FSTYPE=$(/usr/sbin/fstype $j) if [ ! -z $FSTYPE ];then if [ -z $LABEL ];then if ! grep -Rn "$j " /etc/fstab >/dev/null;then MYMOUNT="/mnt/$(basename $j)" if [ ! -d "$MYMOUNT" ]; then mkdir -p "$MYMOUNT" fi if echo $FSTYPE|grep -i vfat >/dev/null|| echo $FSTYPE|grep -i ntfs >/dev/null;then echo "$j $MYMOUNT $FSTYPE $MOUNTOPTIONS,umask=0,exec 0 0" >>/etc/fstab mount -t $FSTYPE -o $MOUNTOPTIONS,umask=0,exec,rw $j $MYMOUNT 2>/dev/null else echo "$j $MYMOUNT $FSTYPE defaults 0 0" >>/etc/fstab mount -t $FSTYPE -o defaults $j $MYMOUNT 2>/dev/null fi fi else if ! grep -Rn "$j " /etc/fstab >/dev/null && ! grep -Rn LABEL=$LABEL /etc/fstab >/dev/null;then MYMOUNT="/mnt/$(basename $j)" if [ ! -d "$MYMOUNT" ]; then mkdir -p "$MYMOUNT" fi if echo $FSTYPE|grep -i vfat >/dev/null || echo $FSTYPE|grep -i ntfs >/dev/null;then echo "$j $MYMOUNT $FSTYPE $MOUNTOPTIONS,umask=0,exec 0 0" >>/etc/fstab mount -t $FSTYPE -o $MOUNTOPTIONS,umask=0,exec,rw $j $MYMOUNT 2>/dev/null else echo "$j $MYMOUNT $FSTYPE defaults 0 0" >>/etc/fstab mount -t $FSTYPE -o defaults $j $MYMOUNT 2>/dev/null fi fi fi fi done elif cat $i/media |grep -i cdrom >/dev/null;then if ! grep -Rn $(basename $i) /etc/fstab >/dev/null;then MYMOUNT="/mnt/cdrom-$(basename $i)" if [ ! -d "$MYMOUNT" ]; then mkdir -p "$MYMOUNT" fi echo "/dev/$(basename $i) $MYMOUNT udf,iso9660 $MOUNTOPTIONS,user,noauto 0 0" >>/etc/fstab fi fi done # for scsi devices,now only for sata. if cat /proc/scsi/scsi |grep Direct >/dev/null;then for j in $(/sbin/fdisk -l /dev/sda|cut -c1-10|grep 'sda');do LABEL=$(e2label $j 2>/dev/null) FSTYPE=$(/usr/sbin/fstype $j) if [ ! -z $FSTYPE ];then if [ -z $LABEL ];then if ! grep -Rn "$j " /etc/fstab >/dev/null;then MYMOUNT="/mnt/$(basename $j)" if [ ! -d "$MYMOUNT" ]; then mkdir -p "$MYMOUNT" fi if echo $FSTYPE|grep -i vfat >/dev/null || echo $FSTYPE|grep -i ntfs >/dev/null;then echo "$j $MYMOUNT $FSTYPE $MOUNTOPTIONS,umask=0,exec 0 0" >>/etc/fstab mount -t $FSTYPE -o $MOUNTOPTIONS,umask=0,exec,rw $j $MYMOUNT 2>/dev/null else echo "$j $MYMOUNT $FSTYPE defaults 0 0" >>/etc/fstab mount -t $FSTYPE -o defaults $j $MYMOUNT 2>/dev/null fi fi else if ! grep -Rn "$j " /etc/fstab >/dev/null && ! grep -Rn LABEL=$LABEL /etc/fstab >/dev/null;then MYMOUNT="/mnt/$(basename $j)" if [ ! -d "$MYMOUNT" ]; then mkdir -p "$MYMOUNT" fi if echo $FSTYPE|grep -i vfat >/dev/null || echo $FSTYPE|grep -i ntfs >/dev/null;then echo "$j $MYMOUNT $FSTYPE $MOUNTOPTIONS,umask=0,exec 0 0" >>/etc/fstab mount -t $FSTYPE -o $MOUNTOPTIONS,umask=0,exec,rw $j $MYMOUNT 2>/dev/null else echo "$j $MYMOUNT $FSTYPE defaults 0 0" >>/etc/fstab mount -t $FSTYPE -o defaults $j $MYMOUNT 2>/dev/null fi fi fi fi done elif cat /proc/scsi/scsi |grep CD-ROM >/dev/null;then if ! grep -Rn sda /etc/fstab >/dev/null;then MYMOUNT="/mnt/cdrom-sda" if [ ! -d "$MYMOUNT" ]; then mkdir -p "$MYMOUNT" fi echo "/dev/sda $MYMOUNT udf,iso9660 $MOUNTOPTIONS,user,noauto 0 0" >>/etc/fstab fi fi[/code:1][/quote] |
MichaelBibby
|
3#
MichaelBibby 发表于 2004-10-25 13:43
红旗中用来自动挂载分区,并建立链接图标的三个脚本:
(关于红旗如何实现自动挂载,如果有什么问题,可以单独发表主题探讨) First: “/etc/rc.d/init.d/ls-disk” [code:1] #!/bin/bash # # ls-disk This scripts probe dosdisk and mount them. # # chkconfig: 345 70 05 # description: This runs the VFAT and NTFS probe # Source function library. . /etc/init.d/functions case "$1" in start) echo -n $"Start ls-disk: " [ -x /bin/lspart ] && /bin/lspart success echo "" ;; *) echo $"Usage: $0 {start}" exit 1 ;; esac exit $RETVAL [/code:1] Second:"/etc/rc.d/init.d/functions" [code:1] # -*-Shell-script-*- # # functions This file contains functions to be used by most or all # shell scripts in the /etc/init.d directory. # # Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org> # Hacked by: Greg Galloway and Marc Ewing # # i18n originally by: Arnaldo Carvalho de Melo <acme@conectiva.com.br>, # Wanderlei Antonio Cavassin TEXTDOMAIN=initscripts TEXTDOMAINDIR=/etc/locale # Make sure umask is sane umask 022 # Set up a default search path. PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin" export PATH # Functions for the boot progress bar . /etc/rc.d/init.d/pr_functions # Get a sane screen width [ -z "${COLUMNS:-}" ] && COLUMNS=80 [ -z "${CONSOLETYPE:-}" ] && CONSOLETYPE="`/sbin/consoletype`" if [ -f /etc/sysconfig/i18n -a -z "${NOLOCALE:-}" ] ; then . /etc/sysconfig/i18n if [ "$CONSOLETYPE" != "pty" ]; then case "${LANG:-}" in ja_JP.eucJP|ko_KR.eucKR|zh_CN.GB2312|zh_TW.Big5|zh_CN.GB18030) unset LANG;; *) export LANG esac else export LANG fi fi # Read in our configuration if [ -z "${BOOTUP:-}" ]; then if [ -f /etc/sysconfig/init ]; then . /etc/sysconfig/init else # This all seem confusing? Look in /etc/sysconfig/init, # or in /usr/doc/initscripts-*/sysconfig.txt BOOTUP=color RES_COL=60 MOVE_TO_COL="echo -en \\033[${RES_COL}G" SETCOLOR_SUCCESS="echo -en \\033[1;32m" SETCOLOR_FAILURE="echo -en \\033[1;31m" SETCOLOR_WARNING="echo -en \\033[1;33m" SETCOLOR_NORMAL="echo -en \\033[0;39m" LOGLEVEL=1 fi if [ "$CONSOLETYPE" = "serial" ]; then BOOTUP=serial MOVE_TO_COL= SETCOLOR_SUCCESS= SETCOLOR_FAILURE= SETCOLOR_WARNING= SETCOLOR_NORMAL= fi fi if [ "${BOOTUP:-}" != "verbose" ]; then INITLOG_ARGS="-q" else INITLOG_ARGS= fi # Check if $pid (could be plural) are running checkpid() { local i for i in $* ; do [ -d "/proc/$i" ] && return 0 done return 1 } # A function to start a program. daemon() { # Test syntax. local gotbase= force= local base= user= nice= bg= pid= nicelevel=0 while [ "$1" != "${1##[-+]}" ]; do case $1 in '') echo $"$0: Usage: daemon [+/-nicelevel] {program}" return 1;; --check) base=$2 gotbase="yes" shift 2 ;; --check=?*) base=${1#--check=} gotbase="yes" shift ;; --user) user=$2 shift 2 ;; --user=?*) user=${1#--user=} shift ;; --force) force="force" shift ;; [-+][0-9]*) nice="nice -n $1" shift ;; *) echo $"$0: Usage: daemon [+/-nicelevel] {program}" return 1;; esac done # Save basename. [ -z "$gotbase" ] && base=${1##*/} # See if it's already running. Look *only* at the pid file. if [ -f /var/run/${base}.pid ]; then local line p read line < /var/run/${base}.pid for p in $line ; do [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p" done fi [ -n "${pid:-}" -a -z "${force:-}" ] && return # make sure it doesn't core dump anywhere; while this could mask # problems with the daemon, it also closes some security problems ulimit -S -c 0 >/dev/null 2>&1 # Echo daemon [ "${BOOTUP:-}" = "verbose" -a -z "$LSB" ] && echo -n " $base" # And start it up. if [ -z "$user" ]; then $nice initlog $INITLOG_ARGS -c "$*" else $nice initlog $INITLOG_ARGS -c "su -s /bin/bash - $user -c \"$*\"" fi [ "$?" -eq 0 ] && success $"$base startup" || failure $"$base startup" } # A function to stop a program. killproc() { RC=0 # Test syntax. if [ "$#" -eq 0 ]; then echo $"Usage: killproc {program} [signal]" return 1 fi notset=0 # check for second arg to be kill level if [ -n "$2" ]; then killlevel=$2 else notset=1 killlevel="-9" fi # Save basename. base=${1##*/} # Find pid. pid= if [ -f /var/run/${base}.pid ]; then local line p read line < /var/run/${base}.pid for p in $line ; do [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p" done fi if [ -z "$pid" ]; then pid=`pidof -o $$ -o $PPID -o %PPID -x $1 || \ pidof -o $$ -o $PPID -o %PPID -x $base` fi # Kill it. if [ -n "${pid:-}" ] ; then [ "$BOOTUP" = "verbose" -a -z "$LSB" ] && echo -n "$base " if [ "$notset" -eq "1" ] ; then if checkpid $pid 2>&1; then # TERM first, then KILL if not dead kill -TERM $pid usleep 100000 if checkpid $pid && sleep 1 && checkpid $pid && sleep 3 && checkpid $pid ; then kill -KILL $pid usleep 100000 fi fi checkpid $pid RC=$? [ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown" RC=$((! $RC)) # use specified level only else if checkpid $pid; then kill $killlevel $pid RC=$? [ "$RC" -eq 0 ] && success $"$base $killlevel" || failure $"$base $killlevel" fi fi else failure $"$base shutdown" RC=1 fi # Remove pid file if any. if [ "$notset" = "1" ]; then rm -f /var/run/$base.pid fi return $RC } # A function to find the pid of a program. Looks *only* at the pidfile pidfileofproc() { local base=${1##*/} # Test syntax. if [ "$#" = 0 ] ; then echo $"Usage: pidfileofproc {program}" return 1 fi # First try "/var/run/*.pid" files if [ -f /var/run/$base.pid ] ; then local line p pid= read line < /var/run/$base.pid for p in $line ; do [ -z "${p//[0-9]/}" -a -d /proc/$p ] && pid="$pid $p" done if [ -n "$pid" ]; then echo $pid return 0 fi fi } # A function to find the pid of a program. pidofproc() { base=${1##*/} # Test syntax. if [ "$#" = 0 ]; then echo $"Usage: pidofproc {program}" return 1 fi # First try "/var/run/*.pid" files if [ -f /var/run/$base.pid ]; then local line p pid= read line < /var/run/$base.pid for p in $line ; do [ -z "${p//[0-9]/}" -a -d /proc/$p ] && pid="$pid $p" done if [ -n "$pid" ]; then echo $pid return 0 fi fi pidof -o $$ -o $PPID -o %PPID -x $1 || \ pidof -o $$ -o $PPID -o %PPID -x $base } status() { local base=${1##*/} local pid # Test syntax. if [ "$#" = 0 ] ; then echo $"Usage: status {program}" return 1 fi # First try "pidof" pid=`pidof -o $$ -o $PPID -o %PPID -x $1 || \ pidof -o $$ -o $PPID -o %PPID -x ${base}` if [ -n "$pid" ]; then echo $"${base} (pid $pid) is running..." return 0 fi # Next try "/var/run/*.pid" files if [ -f /var/run/${base}.pid ] ; then read pid < /var/run/${base}.pid if [ -n "$pid" ]; then echo $"${base} dead but pid file exists" return 1 fi fi # See if /var/lock/subsys/${base} exists if [ -f /var/lock/subsys/${base} ]; then echo $"${base} dead but subsys locked" return 2 fi echo $"${base} is stopped" return 3 } echo_success() { [ "$BOOTUP" = "color" ] && $MOVE_TO_COL echo -n "[ " [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS echo -n $"OK" [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL echo -n " ]" echo -ne "\r" return 0 } echo_failure() { [ "$BOOTUP" = "color" ] && $MOVE_TO_COL echo -n "[" [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE echo -n $"FAILED" [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL echo -n "]" echo -ne "\r" return 1 } echo_passed() { [ "$BOOTUP" = "color" ] && $MOVE_TO_COL echo -n "[" [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING echo -n $"PASSED" [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL echo -n "]" echo -ne "\r" return 1 } echo_warning() { [ "$BOOTUP" = "color" ] && $MOVE_TO_COL echo -n "[" [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING echo -n $"WARNING" [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL echo -n "]" echo -ne "\r" return 1 } # Log that something succeeded success() { if [ -z "${IN_INITLOG:-}" ]; then initlog $INITLOG_ARGS -n $0 -s "$1" -e 1 else # silly hack to avoid EPIPE killing rc.sysinit trap "" SIGPIPE echo "$INITLOG_ARGS -n $0 -s \"$1\" -e 1" >&21 trap - SIGPIPE fi [ "$BOOTUP" != "verbose" -a -z "$LSB" ] && echo_success return 0 } # Log that something failed failure() { rc=$? if [ -z "${IN_INITLOG:-}" ]; then initlog $INITLOG_ARGS -n $0 -s "$1" -e 2 else trap "" SIGPIPE echo "$INITLOG_ARGS -n $0 -s \"$1\" -e 2" >&21 trap - SIGPIPE fi [ "$BOOTUP" != "verbose" -a -z "$LSB" ] && echo_failure return $rc } # Log that something passed, but may have had errors. Useful for fsck passed() { rc=$? if [ -z "${IN_INITLOG:-}" ]; then initlog $INITLOG_ARGS -n $0 -s "$1" -e 1 else trap "" SIGPIPE echo "$INITLOG_ARGS -n $0 -s \"$1\" -e 1" >&21 trap - SIGPIPE fi [ "$BOOTUP" != "verbose" -a -z "$LSB" ] && echo_passed return $rc } # Log a warning warning() { rc=$? if [ -z "${IN_INITLOG:-}" ]; then initlog $INITLOG_ARGS -n $0 -s "$1" -e 1 else trap "" SIGPIPE echo "$INITLOG_ARGS -n $0 -s \"$1\" -e 1" >&21 trap - SIGPIPE fi [ "$BOOTUP" != "verbose" -a -z "$LSB" ] && echo_warning return $rc } # Run some action. Log its output. action() { STRING=$1 echo -n "$STRING " shift initlog $INITLOG_ARGS -c "$*" && success $"$STRING" || failure $"$STRING" rc=$? echo return $rc } # returns OK if $1 contains $2 strstr() { #case "$1" in # *${2}*) return 0 ;; #esac #return 1 [ "$1" = "$2" ] && return 0 slice=${1#*$2*} [ "$slice" = "$1" ] && return 1 return 0 } # Confirm whether we really want to run this service confirm() { local YES=$"yY" local NO=$"nN" local CONT=$"cC" while : ; do echo -n $"Start service $1 (Y)es/(N)o/(C)ontinue? [Y] " read answer if strstr "$YES" "$answer" || [ "$answer" = "" ] ; then return 0 elif strstr "$CONT" "$answer" ; then return 2 elif strstr "$NO" "$answer" ; then return 1 fi done } [/code:1] Third:“/etc/rc.d/init.d/pr_functions” [code:1] #!/bin/sh PR_BASE=`basename $0` PREVRUN=`runlevel | cut -d ' ' -f 1` # Display our stuff ONLY if the kernel patch is present and we are booting # and ONLY if we are called as SXXname if [ -w /proc/progress ] && [ $PREVRUN = "N" ] && [ `echo $PR_BASE | cut -c 1` = "S" ]; then # get the name and the start number (0 to 99) of the service PR_NAME=`echo $PR_BASE | cut -c 4-` PR_NUM=`echo $PR_BASE | cut -c 2-3` # get a value from 67 to 100 PR_NEWNUM=`echo "$PR_NUM / 3 + 70" | bc` echo "$PR_NEWNUM starting $PR_NAME" echo "$PR_NEWNUM starting $PR_NAME" > /proc/progress fi # Basic functions used in rc.sysinit (and can be used in each service script) function pr_warn() { if [ -w /proc/progress ]; then echo w > /proc/progress fi } function pr_fail() { if [ -w /proc/progress ]; then echo f > /proc/progress fi } function pr_set() { if [ -w /proc/progress ]; then echo $1 $2 > /proc/progress fi } [/code:1] |
fke7985
|
4#
fke7985 发表于 2005-01-30 10:48
[code:1]#define N 127
move(int n, char SI, char DI, int *Pointer, int Top, long int *MoveSum, char *nPointer) { char flag; //*Pointer is on C Plane Sum //*nPointer-*Pointer的差为C柱子上的盘子数目 //flag存放'+'和'-'号,表示的是C柱子上的盘子数目是加1还是减1 //(*Pointer)表示还有多少个盘子不在C柱子上 //Top表示的是栈顶的指针值 //*MoveSum表示当前盘子是第几次移动,最后一次移动时MoveSum就是总移动次数 //n是盘子的编号 //SI源柱子,DI目标柱子 flag='*'; if(SI=='C'){ flag='+'; (*Pointer)++; }else if(DI=='C'){ flag='-'; (*Pointer)--; } (*MoveSum)++; printf("Plane Code:%d, %c|--->%c |'C' SumNum:%d OP:'%c'|Stack Pointer:%d|, Move Times:%ld\n",n,SI,DI,*nPointer-*Pointer,flag,Top,*MoveSum); return 0; } main() { long int stack[N],MoveSum; //stack[N]表示栈,MoveSum移动总次数 char A,B,C,Temp,n,*nPointer; //Temp交换变量用,*nPointer存放要移动盘子的数目与n,SumC是一样 int SumC; int Top; //栈顶指针 nPointer=(char *)malloc(sizeof(char)*1); while(1) { MoveSum=0; printf("Please Enter The Number(1~127):");//限制最多只能输入127个盘子数 scanf("%d",nPointer); if(*nPointer<=0 || *nPointer>=127) { printf("Enter The Number Error! Number:%d OutSize Range(1~127)\n",nPointer); return 1; } n=*nPointer; //之所以把n设为CHAR,是因为n,A,B,C合计占四个字节刚好存于long int的栈中 A='A';B='B';C='C'; //初始化 SumC=n; //A柱子上的盘子数目设为了n,也就是C柱子上还少n个盘子 Top=0; //栈顶指针置0 while(SumC>0) //如果SumC等于0,也是C柱子上的盘子少的数目是0,那么移动结束,也就是A上所有的盘子都移动了C上了 { while(n>1) { stack[Top]=0x00000000; stack[Top]=stack[Top] | n; stack[Top]=stack[Top]<<8; stack[Top]=stack[Top] | A; stack[Top]=stack[Top]<<8; stack[Top]=stack[Top] | B; stack[Top]=stack[Top]<<8; stack[Top]=stack[Top] | C; Temp=B;B=C;C=Temp; Top+=1; n--; //数据入栈 } if(n==1) move(n,A,C,&SumC,Top,&MoveSum,nPointer); //如果N为1则直接把盘子移到C变量中存的柱子编号上 Top--; //数据出栈 if(Top<0) break;//如果栈为空,那么移动结束,跳出循环 C=(char)(stack[Top]); B=(char)(stack[Top]>>8); A=(char)(stack[Top]>>16); n=(char)(stack[Top]>>24); //出栈操作 move(n,A,C,&SumC,Top,&MoveSum,nPointer); n--; Temp=A;A=B;B=Temp; } printf("Move Plane All Times:%ld\n",MoveSum); } }[/code:1] |
wsly790104
|
5#
wsly790104 发表于 2005-02-03 13:59
[code:1]#!/bin/bash
#history #2.3 delete lcd command line. ############################ #Ftp download file #usage: sh ftpdownload.sh [$filename] ############################ filename=$1 # ftp -nvi <<! ftp -nv <<! open 10.10.10.10 user anonymous 111@111.com ascii prompt #ryan add 0806 #lcd /test/ryan cd /ryan/script/ get $filename close bye ! clear echo -e " \n Have download filerun from 10FTP to /test/ryan " exit 0[/code:1] |
BOoRFGOnZ
|
6#
BOoRFGOnZ 发表于 2005-02-16 09:58
随机换ip+mac上网
[code:1]#!/bin/bash netstart() { IP[0]=10.64.154.18 MAC[0]=00:a1:b0:13:fd:d1 . . . . IP[51]=10.64.154.96 MAC[51]=00:10:5C:BD:80:FD n=0 ping -c 1 10.64.1.1 > /dev/null i=$? while [ "$i" -ne 0 ];do RANGE=51 number=$RANDOM let "number %= $RANGE" ifconfig eth0 down >/dev/null 2>&1 ifconfig eth0 hw ether "${MAC[$number]}" >/dev/null 2>&1 ifconfig eth0 up >/dev/null 2>&1 service network stop >/dev/null 2>&1 ifconfig eth0 "${IP[$number]}" netmask 255.255.255.0 >/dev/null 2>&1 route add -net default gw 10.64.154.129 >/dev/null 2>&1 service network start >/dev/null 2>&1 ping -c 1 10.64.1.1 > /dev/null i=$? #let "n=$n+1" #if [ "$n" -eq 10 ];then # exit 1 #fi done echo "================================== ${IP[$number]}===================================" } #main m=1 while [ "$m" -ne 0 ];do ping -c 1 10.64.1.1 > /dev/null q=$? if [ "$q" -ne 0 ] ; then netstart; fi sleep 60 done [/code:1] |
watercloud
|
7#
watercloud 发表于 2005-02-16 18:31
[code:1]$ read -t 10 t < /dev/tcp/www.xfocus.net/25 && echo $t
220 xfocus.org ESMTP Service[/code:1] [code:1]$ echo k|awk '{ "/inet/tcp/0/www.xfocus.org/25" |& getline ; print }' 220 xfocus.org ESMTP Service[/code:1] |
zhy2111314
|
8#
zhy2111314 发表于 2005-03-11 08:46
shell中俄罗斯方块(转自CU)
运行于GNU bash, version 2.05a.0(1)-release (i686-pc-linux-gnu) [code:1] #!/bin/bash # Tetris Game # 10.21.2003 xhchen<xhchen@winbond.com.tw> #颜色定义 cRed=1 cGreen=2 cYellow=3 cBlue=4 cFuchsia=5 cCyan=6 cWhite=7 colorTable=($cRed $cGreen $cYellow $cBlue $cFuchsia $cCyan $cWhite) #位置和大小 iLeft=3 iTop=2 ((iTrayLeft = iLeft + 2)) ((iTrayTop = iTop + 1)) ((iTrayWidth = 10)) ((iTrayHeight = 15)) #颜色设置 cBorder=$cGreen cScore=$cFuchsia cScoreValue=$cCyan #控制信号 #改游戏使用两个进程,一个用于接收输入,一个用于游戏流程和显示界面; #当前者接收到上下左右等按键时,通过向后者发送signal的方式通知后者。 sigRotate=25 sigLeft=26 sigRight=27 sigDown=28 sigAllDown=29 sigExit=30 #七中不同的方块的定义 #通过旋转,每种方块的显示的样式可能有几种 box0=(0 0 0 1 1 0 1 1) box1=(0 2 1 2 2 2 3 2 1 0 1 1 1 2 1 3) box2=(0 0 0 1 1 1 1 2 0 1 1 0 1 1 2 0) box3=(0 1 0 2 1 0 1 1 0 0 1 0 1 1 2 1) box4=(0 1 0 2 1 1 2 1 1 0 1 1 1 2 2 2 0 1 1 1 2 0 2 1 0 0 1 0 1 1 1 2) box5=(0 1 1 1 2 1 2 2 1 0 1 1 1 2 2 0 0 0 0 1 1 1 2 1 0 2 1 0 1 1 1 2) box6=(0 1 1 1 1 2 2 1 1 0 1 1 1 2 2 1 0 1 1 0 1 1 2 1 0 1 1 0 1 1 1 2) #所有其中方块的定义都放到box变量中 box=(${box0[@]} ${box1[@]} ${box2[@]} ${box3[@]} ${box4[@]} ${box5[@]} ${box6[@]}) #各种方块旋转后可能的样式数目 countBox=(1 2 2 2 4 4 4) #各种方块再box数组中的偏移 offsetBox=(0 1 3 5 7 11 15) #每提高一个速度级需要积累的分数 iScoreEachLevel=50 #be greater than 7 #运行时数据 sig=0 #接收到的signal iScore=0 #总分 iLevel=0 #速度级 boxNew=() #新下落的方块的位置定义 cBoxNew=0 #新下落的方块的颜色 iBoxNewType=0 #新下落的方块的种类 iBoxNewRotate=0 #新下落的方块的旋转角度 boxCur=() #当前方块的位置定义 cBoxCur=0 #当前方块的颜色 iBoxCurType=0 #当前方块的种类 iBoxCurRotate=0 #当前方块的旋转角度 boxCurX=-1 #当前方块的x坐标位置 boxCurY=-1 #当前方块的y坐标位置 iMap=() #背景方块图表 #初始化所有背景方块为-1, 表示没有方块 for ((i = 0; i < iTrayHeight * iTrayWidth; i++)); do iMap[$i]=-1; done #接收输入的进程的主函数 function RunAsKeyReceiver() { local pidDisplayer key aKey sig cESC sTTY pidDisplayer=$1 aKey=(0 0 0) cESC=`echo -ne "\33"` cSpace=`echo -ne "\40"` #保存终端属性。在read -s读取终端键时,终端的属性会被暂时改变。 #如果在read -s时程序被不幸杀掉,可能会导致终端混乱, #需要在程序退出时恢复终端属性。 sTTY=`stty -g` #捕捉退出信号 trap "MyExit;" INT TERM trap "MyExitNoSub;" $sigExit #隐藏光标 echo -ne "\33[?25l" while (( 1 )) do #读取输入。注-s不回显,-n读到一个字符立即返回 read -s -n 1 key aKey[0]=${aKey[1]} aKey[1]=${aKey[2]} aKey[2]=$key sig=0 #判断输入了何种键 if [[ $key == $cESC && ${aKey[1]} == $cESC ]] then #ESC键 MyExit elif [[ ${aKey[0]} == $cESC && ${aKey[1]} == "[" ]] then if [[ $key == "A" ]]; then sig=$sigRotate #<向上键> elif [[ $key == "B" ]]; then sig=$sigDown #<向下键> elif [[ $key == "D" ]]; then sig=$sigLeft #<向左键> elif [[ $key == "C" ]]; then sig=$sigRight #<向右键> fi elif [[ $key == "W" || $key == "w" ]]; then sig=$sigRotate #W, w elif [[ $key == "S" || $key == "s" ]]; then sig=$sigDown #S, s elif [[ $key == "A" || $key == "a" ]]; then sig=$sigLeft #A, a elif [[ $key == "D" || $key == "d" ]]; then sig=$sigRight #D, d elif [[ "[$key]" == "[]" ]]; then sig=$sigAllDown #空格键 elif [[ $key == "Q" || $key == "q" ]] #Q, q then MyExit fi if [[ $sig != 0 ]] then #向另一进程发送消息 kill -$sig $pidDisplayer fi done } #退出前的恢复 function MyExitNoSub() { local y #恢复终端属性 stty $sTTY ((y = iTop + iTrayHeight + 4)) #显示光标 echo -e "\33[?25h\33[${y};0H" exit } function MyExit() { #通知显示进程需要退出 kill -$sigExit $pidDisplayer MyExitNoSub } #处理显示和游戏流程的主函数 function RunAsDisplayer() { local sigThis InitDraw #挂载各种信号的处理函数 trap "sig=$sigRotate;" $sigRotate trap "sig=$sigLeft;" $sigLeft trap "sig=$sigRight;" $sigRight trap "sig=$sigDown;" $sigDown trap "sig=$sigAllDown;" $sigAllDown trap "ShowExit;" $sigExit while (( 1 )) do #根据当前的速度级iLevel不同,设定相应的循环的次数 for ((i = 0; i < 21 - iLevel; i++)) do sleep 0.02 sigThis=$sig sig=0 #根据sig变量判断是否接受到相应的信号 if ((sigThis == sigRotate)); then BoxRotate; #旋转 elif ((sigThis == sigLeft)); then BoxLeft; #左移一列 elif ((sigThis == sigRight)); then BoxRight; #右移一列 elif ((sigThis == sigDown)); then BoxDown; #下落一行 elif ((sigThis == sigAllDown)); then BoxAllDown; #下落到底 fi done #kill -$sigDown $$ BoxDown #下落一行 done } #BoxMove(y, x), 测试是否可以把移动中的方块移到(x, y)的位置, 返回0则可以, 1不可以 function BoxMove() { local j i x y xTest yTest yTest=$1 xTest=$2 for ((j = 0; j < 8; j += 2)) do ((i = j + 1)) ((y = ${boxCur[$j]} + yTest)) ((x = ${boxCur[$i]} + xTest)) if (( y < 0 || y >= iTrayHeight || x < 0 || x >= iTrayWidth)) then #撞到墙壁了 return 1 fi if ((${iMap[y * iTrayWidth + x]} != -1 )) then #撞到其他已经存在的方块了 return 1 fi done return 0; } #将当前移动中的方块放到背景方块中去, #并计算新的分数和速度级。(即一次方块落到底部) function Box2Map() { local j i x y xp yp line #将当前移动中的方块放到背景方块中去 for ((j = 0; j < 8; j += 2)) do ((i = j + 1)) ((y = ${boxCur[$j]} + boxCurY)) ((x = ${boxCur[$i]} + boxCurX)) ((i = y * iTrayWidth + x)) iMap[$i]=$cBoxCur done #消去可被消去的行 line=0 for ((j = 0; j < iTrayWidth * iTrayHeight; j += iTrayWidth)) do for ((i = j + iTrayWidth - 1; i >= j; i--)) do if ((${iMap[$i]} == -1)); then break; fi done if ((i >= j)); then continue; fi ((line++)) for ((i = j - 1; i >= 0; i--)) do ((x = i + iTrayWidth)) iMap[$x]=${iMap[$i]} done for ((i = 0; i < iTrayWidth; i++)) do iMap[$i]=-1 done done if ((line == 0)); then return; fi #根据消去的行数line计算分数和速度级 ((x = iLeft + iTrayWidth * 2 + 7)) ((y = iTop + 11)) ((iScore += line * 2 - 1)) #显示新的分数 echo -ne "\33[1m\33[3${cScoreValue}m\33[${y};${x}H${iScore} " if ((iScore % iScoreEachLevel < line * 2 - 1)) then if ((iLevel < 20)) then ((iLevel++)) ((y = iTop + 14)) #显示新的速度级 echo -ne "\33[3${cScoreValue}m\33[${y};${x}H${iLevel} " fi fi echo -ne "\33[0m" #重新显示背景方块 for ((y = 0; y < iTrayHeight; y++)) do ((yp = y + iTrayTop + 1)) ((xp = iTrayLeft + 1)) ((i = y * iTrayWidth)) echo -ne "\33[${yp};${xp}H" for ((x = 0; x < iTrayWidth; x++)) do ((j = i + x)) if ((${iMap[$j]} == -1)) then echo -ne " " else echo -ne "\33[1m\33[7m\33[3${iMap[$j]}m\33[4${iMap[$j]}m[]\33[0m" fi done done } #下落一行 function BoxDown() { local y s ((y = boxCurY + 1)) #新的y坐标 if BoxMove $y $boxCurX #测试是否可以下落一行 then s="`DrawCurBox 0`" #将旧的方块抹去 ((boxCurY = y)) s="$s`DrawCurBox 1`" #显示新的下落后方块 echo -ne $s else #走到这儿, 如果不能下落了 Box2Map #将当前移动中的方块贴到背景方块中 RandomBox #产生新的方块 fi } #左移一列 function BoxLeft() { local x s ((x = boxCurX - 1)) if BoxMove $boxCurY $x then s=`DrawCurBox 0` ((boxCurX = x)) s=$s`DrawCurBox 1` echo -ne $s fi } #右移一列 function BoxRight() { local x s ((x = boxCurX + 1)) if BoxMove $boxCurY $x then s=`DrawCurBox 0` ((boxCurX = x)) s=$s`DrawCurBox 1` echo -ne $s fi } #下落到底 function BoxAllDown() { local k j i x y iDown s iDown=$iTrayHeight #计算一共需要下落多少行 for ((j = 0; j < 8; j += 2)) do ((i = j + 1)) ((y = ${boxCur[$j]} + boxCurY)) ((x = ${boxCur[$i]} + boxCurX)) for ((k = y + 1; k < iTrayHeight; k++)) do ((i = k * iTrayWidth + x)) if (( ${iMap[$i]} != -1)); then break; fi done ((k -= y + 1)) if (( $iDown > $k )); then iDown=$k; fi done s=`DrawCurBox 0` #将旧的方块抹去 ((boxCurY += iDown)) s=$s`DrawCurBox 1` #显示新的下落后的方块 echo -ne $s Box2Map #将当前移动中的方块贴到背景方块中 RandomBox #产生新的方块 } #旋转方块 function BoxRotate() { local iCount iTestRotate boxTest j i s iCount=${countBox[$iBoxCurType]} #当前的方块经旋转可以产生的样式的数目 #计算旋转后的新的样式 ((iTestRotate = iBoxCurRotate + 1)) if ((iTestRotate >= iCount)) then ((iTestRotate = 0)) fi #更新到新的样式, 保存老的样式(但不显示) for ((j = 0, i = (${offsetBox[$iBoxCurType]} + $iTestRotate) * 8; j < 8; j++, i++)) do boxTest[$j]=${boxCur[$j]} boxCur[$j]=${box[$i]} done if BoxMove $boxCurY $boxCurX #测试旋转后是否有空间放的下 then #抹去旧的方块 for ((j = 0; j < 8; j++)) do boxCur[$j]=${boxTest[$j]} done s=`DrawCurBox 0` #画上新的方块 for ((j = 0, i = (${offsetBox[$iBoxCurType]} + $iTestRotate) * 8; j < 8; j++, i++)) do boxCur[$j]=${box[$i]} done s=$s`DrawCurBox 1` echo -ne $s iBoxCurRotate=$iTestRotate else #不能旋转,还是继续使用老的样式 for ((j = 0; j < 8; j++)) do boxCur[$j]=${boxTest[$j]} done fi } #DrawCurBox(bDraw), 绘制当前移动中的方块, bDraw为1, 画上, bDraw为0, 抹去方块。 function DrawCurBox() { local i j t bDraw sBox s bDraw=$1 s="" if (( bDraw == 0 )) then sBox="\40\40" else sBox="[]" s=$s"\33[1m\33[7m\33[3${cBoxCur}m\33[4${cBoxCur}m" fi for ((j = 0; j < 8; j += 2)) do ((i = iTrayTop + 1 + ${boxCur[$j]} + boxCurY)) ((t = iTrayLeft + 1 + 2 * (boxCurX + ${boxCur[$j + 1]}))) #\33[y;xH, 光标到(x, y)处 s=$s"\33[${i};${t}H${sBox}" done s=$s"\33[0m" echo -n $s } #更新新的方块 function RandomBox() { local i j t #更新当前移动的方块 iBoxCurType=${iBoxNewType} iBoxCurRotate=${iBoxNewRotate} cBoxCur=${cBoxNew} for ((j = 0; j < ${#boxNew[@]}; j++)) do boxCur[$j]=${boxNew[$j]} done #显示当前移动的方块 if (( ${#boxCur[@]} == 8 )) then #计算当前方块该从顶端哪一行"冒"出来 for ((j = 0, t = 4; j < 8; j += 2)) do if ((${boxCur[$j]} < t)); then t=${boxCur[$j]}; fi done ((boxCurY = -t)) for ((j = 1, i = -4, t = 20; j < 8; j += 2)) do if ((${boxCur[$j]} > i)); then i=${boxCur[$j]}; fi if ((${boxCur[$j]} < t)); then t=${boxCur[$j]}; fi done ((boxCurX = (iTrayWidth - 1 - i - t) / 2)) #显示当前移动的方块 echo -ne `DrawCurBox 1` #如果方块一出来就没处放,Game over! if ! BoxMove $boxCurY $boxCurX then kill -$sigExit ${PPID} ShowExit fi fi #清除右边预显示的方块 for ((j = 0; j < 4; j++)) do ((i = iTop + 1 + j)) ((t = iLeft + 2 * iTrayWidth + 7)) echo -ne "\33[${i};${t}H " done #随机产生新的方块 ((iBoxNewType = RANDOM % ${#offsetBox[@]})) ((iBoxNewRotate = RANDOM % ${countBox[$iBoxNewType]})) for ((j = 0, i = (${offsetBox[$iBoxNewType]} + $iBoxNewRotate) * 8; j < 8; j++, i++)) do boxNew[$j]=${box[$i]}; done ((cBoxNew = ${colorTable[RANDOM % ${#colorTable[@]}]})) #显示右边预显示的方块 echo -ne "\33[1m\33[7m\33[3${cBoxNew}m\33[4${cBoxNew}m" for ((j = 0; j < 8; j += 2)) do ((i = iTop + 1 + ${boxNew[$j]})) ((t = iLeft + 2 * iTrayWidth + 7 + 2 * ${boxNew[$j + 1]})) echo -ne "\33[${i};${t}H[]" done echo -ne "\33[0m" } #初始绘制 function InitDraw() { clear RandomBox #随机产生方块,这时右边预显示窗口中有方快了 RandomBox #再随机产生方块,右边预显示窗口中的方块被更新,原先的方块将开始下落 local i t1 t2 t3 #显示边框 echo -ne "\33[1m" echo -ne "\33[3${cBorder}m\33[4${cBorder}m" ((t2 = iLeft + 1)) ((t3 = iLeft + iTrayWidth * 2 + 3)) for ((i = 0; i < iTrayHeight; i++)) do ((t1 = i + iTop + 2)) echo -ne "\33[${t1};${t2}H||" echo -ne "\33[${t1};${t3}H||" done ((t2 = iTop + iTrayHeight + 2)) for ((i = 0; i < iTrayWidth + 2; i++)) do ((t1 = i * 2 + iLeft + 1)) echo -ne "\33[${iTrayTop};${t1}H==" echo -ne "\33[${t2};${t1}H==" done echo -ne "\33[0m" #显示"Score"和"Level"字样 echo -ne "\33[1m" ((t1 = iLeft + iTrayWidth * 2 + 7)) ((t2 = iTop + 10)) echo -ne "\33[3${cScore}m\33[${t2};${t1}HScore" ((t2 = iTop + 11)) echo -ne "\33[3${cScoreValue}m\33[${t2};${t1}H${iScore}" ((t2 = iTop + 13)) echo -ne "\33[3${cScore}m\33[${t2};${t1}HLevel" ((t2 = iTop + 14)) echo -ne "\33[3${cScoreValue}m\33[${t2};${t1}H${iLevel}" echo -ne "\33[0m" } #退出时显示GameOVer! function ShowExit() { local y ((y = iTrayHeight + iTrayTop + 3)) echo -e "\33[${y};0HGameOver!\33[0m" exit } #游戏主程序在这儿开始. if [[ $1 != "--show" ]] then bash $0 --show& #以参数--show将本程序再运行一遍 RunAsKeyReceiver $! #以上一行产生的进程的进程号作为参数 exit else #当发现具有参数--show时,运行显示函数 RunAsDisplayer exit fi [/code:1] |
17dvd
|
9#
17dvd 发表于 2005-07-01 17:23
一个简单的通讯录Shell程序:
[code:1] if [ $# -eq 0 ] then echo "使用说明:telp -q,查询;telp -a,增加." exit fi if [ $1 = -q ] then echo -n "请输入姓名(编号、地址或电话号码):" read a grep $a telp fi if [ $1 = -a ] then echo -n "请输入姓名、编号、地址和电话号码:" read d e f g echo "# $d $e $f $g\n">>telp fi setcolor -n # 张三 001 人民大街 8888888 [/code:1] Linux工程师学习视频 http://17dvd.cn/dvd/list.asp?id=417 |
ywchen2000
|
10#
ywchen2000 发表于 2005-07-05 09:34
Linux开机自动挂载WINDOWS下的分区
笔者有两快硬盘,其中第一块硬盘装了WindowsMe,第二快装了Win2000和red hat linux7.2,在WINDOWS环境中,笔者划分了四个分区,每次在LINUX环境中要想看这四个区的内 容都要输入一大堆的命令,非常麻烦.近来学习了SHELL编程,想到了一个解决方法.现在写出 来与大家分享. 首先,在/mnt下,你要有winme temp win2000 share这几个目录,如果没有可以通过在虚拟终端分别输入mkdir /mnt/winme, mkdir /mnt/temp,mkdir /mnt/win2000,mkdir /mnt/share来建立。接着在虚拟终端,输入vi mymount,然后按insert键,输入以下代码: #!/bin/bash case $1 in m) mount -o iocharset=cp936 -t vfat /dev/hda5 /mnt/winme mount -o iocharset=cp936 -t vfat /dev/hda6 /mnt/temp mount -o iocharset=cp936 -t vfat /dev/hdc1 /mnt/win2000 mount -o iocharset=cp936 -t vfat /dev/hdc2 /mnt/share ;; u) umount -o iocharset=cp936 -t vfat /dev/hda5 /mnt/winme umount -o iocharset=cp936 -t vfat /dev/hda6 /mnt/temp umount -o iocharset=cp936 -t vfat /dev/hdc1 /mnt/win2000 umount -o iocharset=cp936 -t vfat /dev/hdc2 /mnt/share ;; esac 按Esc输入:wq回车就可以了. 现在让我来解释一下这些代码.第一行#!/bin/bash指定以bash shell执行此文 件.case $1 in 为取的命令行参数.若为m则开始挂载,若为u则卸载.其中-o iocharset=cp936能够显示中文名.-t vfat 为指定文件系统类型为WINDOWS下的VFAT文件系 统.winme temp win2000和share为目录/mnt下的子目录. 好了,该程序已经写好了.但是它还没有执行权利.我们只要输入下面这个命令就可以了. chmod u+x mymount 呵呵,到这步你只要输入./mymount m,就可以挂载了.不过笔者比较懒,觉得这样做比较麻 烦.于是就请教了一些高手,终于解决了这个问题呀.呵呵.在/etc/rc.d/rc.local 文件中输 入 sh ./root/mymount m就可以了. 重新启动你的电脑,进入LINUX看看是否自动挂载.呵呵.笔者以后就非常轻松,不需要输入 那么多的命令了.爽呀. |