Shell 脚本欣赏区
此贴用来收集兄弟们原创或者收集珍藏的一些好段子,为新手兄弟们提供好的学习范例。 :arrow:转贴请注明出处,原创也请写上自己的ID,便于兄弟们转载:mrgreen:
鼓励原创!
欢迎兄弟们多多发贴,最好能够加上一些必要的注释、说明。 :o
代码请用code格式来格式化。
发表的段子请统一一下格式:
#!/bin/sh
#
###############################################
# Purpose: 简单说明一下这个脚本是干啥滴
# Author:作者( 你滴ID),E-Mail(比如俺滴:michaelbibby AT sohu DOT com )
# From:原出处( 比如,www.LinuxFans.org )
###############################################
Your Code Here !
E-Mail地址可以不写,看兄弟们自己的意愿。如果写的话,请一定注意格式,免得贴上来以后尽收到垃圾邮件。 8)
PS:无关帖子一律砍掉 8) 抛砖引玉 :mrgreen: :
受linuxsir上哈蜜瓜的启发,重新写了一个自动加载分区的脚本,条件所限,对scsi的检测还不太完善,请大家修正:
1、将下面脚本保存为/usr/sbin/fstype:
#!/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
2、将下面脚本放到/etc/rc.d/rc.local里(用ML1.2pre6以后系统的朋友可加在/etc/rc.d/rc.magic里),以便每次开机自动运行:
---------------------------------------------------
#自动检测加载分区
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|grephd);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,noauto0 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,noauto0 0" >>/etc/fstab
fi
fi 红旗中用来自动挂载分区,并建立链接图标的三个脚本:
(关于红旗如何实现自动挂载,如果有什么问题,可以单独发表主题探讨)
First: “/etc/rc.d/init.d/ls-disk”
#!/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
Second:"/etc/rc.d/init.d/functions"
# -*-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, <[email protected]>
# Hacked by: Greg Galloway and Marc Ewing
#
# i18n originally by: Arnaldo Carvalho de Melo <[email protected]>,
# 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
}
Third:“/etc/rc.d/init.d/pr_functions”
#!/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
}
[b]我也来贴贴汉诺塔(是C不是shell)[/b]
#define N 127move(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);
}
} #!/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 [email protected]
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 :o随机换ip+mac上网
#!/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
$ read -t 10 t < /dev/tcp/www.xfocus.net/25 && echo $t
220 xfocus.org ESMTP Service
$ echo k|awk '{ "/inet/tcp/0/www.xfocus.org/25" |& getline ; print }'
220 xfocus.org ESMTP Service shell中俄罗斯方块(转自CU)
运行于GNU bash, version 2.05a.0(1)-release (i686-pc-linux-gnu)
#!/bin/bash
# Tetris Game
# 10.21.2003 xhchen<[email protected]>
#颜色定义
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
一个简单的通讯录Shell程序:
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
Linux工程师学习视频
http://17dvd.cn/dvd/list.asp?id=417
Linux开机自动挂载WINDOWS下的分区
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看看是否自动挂载.呵呵.笔者以后就非常轻松,不需要输入
那么多的命令了.爽呀. 以下代码是使用了script命令来捕获所有的击键操作,用于监视用户.
ps:学学严谨的作风.(korn与bash语法略有差别)
TS=$(date +%m%d%y%H%M%S) #file time stamp
THISHOST=$(hostname|cut -f1-2 -d.)#host name of this machine
LOGDIR=/usr/local/logs/script #directory to hold the logs
LOGFILE=${THISHOST}.${LOGNAME}.$TS#creates the name of the log file
touch $LOGDIR/$LOGFILE #creates the actual file
#set the command prompt
export PS1="[$LOGNAME:$THISHOST]@" '$PWD> '
############### run it here ######################
chown $LOGNAME ${LOGDIR}/${LOGFILE}#let the user own the file during the script
chmod 600 ${LOGDIR}/${LOGFILE} #change permission to RW for the ower
script ${LOGDIR}/${LOGFILE} #start the script monitoring session
chown root ${LOGDIR}/${LOGFILE} #change the ownership to root
chmod 400 ${LOGDIR}/${LOGFILE} #set permission to read-only by root
在CU看到的一段安全代码
#! /bin/bash
# platinum, 2005.07.25
SCANNER=`grep "\`date \"+ %d %H:%M\" -d \"-1min\"\`" /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $1"="$2;}'`
for i in $SCANNER
do
NUM=`echo $i|awk -F= '{print $1}'`
IP=`echo $i|awk -F= '{print $2}'`
echo $NUM
echo $IP
if [ $NUM -gt 5 ] && [ -z "`iptables -vnL INPUT|grep $IP`" ]
then
iptables -I INPUT -s $IP -m state --state NEW,RELATED,ESTABLISHED -j DROP
echo "`date` $IP($NUM)" >> /var/log/scanner.log
fi
done
放在cron中定时执行。
?????
请问这些代码有什么用!!和怎么用?我是新手!请大家多多指教! 记录登录终端与登录时间执行命令
#!/bin/bash
logfilename=/var/log/typescript_`tty | awk -F/ '{print $(NF-1) $(NF)}'`.log
ls `echo $logfilename` >/dev/null 2>&1
if [ $? -ne 0 ];then
touch `echo $logfilename`
fi
script -a `echo $logfilename`
自己写的SHELL。
以此程序了解一些SHELL的运行机制。
像一般C一样编译运行就可以了。
/*filename:mini.h*/
#define WHITESPACE " ., &"
#define MAX_ARG_LEN 10
#define MAX_DIRS 20
#define MAX_PATH_LEN 100
#define STD_INPUT 0
#define STD_OUTPUT 1
#define MAX_ARGS 5
struct command_t {
int argc;
char *name;
char *argv[MAX_ARGS];
};
/************************************************************
*MiniShell
*filename:mini.c
*it's a simple shell written by Manio(maniosterATgmail.com)
*Bugs:1.the commands must be seprated by only one whitespace
*and more and more bugs here...
*the part of pipe havn't be written.
*date,addr:08/16/2006 Yangshuo,China
************************************************************/
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include "mini.h"
//#define DEBUG 1
void printLineHead();
void parseCommand(char *,char **);
char *GetChkFullPath(char **,char **);
main(){
int i,j;/*for common use*/
int stat;/*used by wait()*/
int isInRedir,isOutRedir,isPipe;
int fid;/*for file opening*/
char orginalCmd[100];/*store the command user typed*/
char *dir[MAX_DIRS];
struct command_t command;
char *tempstr[7];
/*initial argv
*maybe a error will be occured if argv is not be initialed
*/
for( i = 0 ; i < MAX_ARGS ; i++ )
command.argv[i] = (char *)malloc(MAX_ARG_LEN);
while( 1 ){
printLineHead();
gets(orginalCmd);/* read user's input */
#ifdef DEBUG
printf("::::after gets(orginalCmd):::: ");
printf("orginalCmd=%s ",orginalCmd);
printf(":::::::::::::::::::::::::::::: ");
#endif
/*if "exit" inputed,exit the shell*/
if(strcmp(orginalCmd,"exit") == 0)
break;
/*devide command line to several parts*/
parseCommand(orginalCmd,command.argv);
/*get the amount of argv*/
for(i = 0;command.argv[i] != NULL;i++);
command.argc = i;
#ifdef DEBUG
printf("command.argc=%d ",command.argc);
#endif
#ifdef DEBUG
printf("::::parseCommand command.argv:::: ");
printf("command.name = %s ",command.name);
int commandi;
for(commandi=0;command.argv[commandi]!=NULL;commandi++)
printf("command.argv[%d]=%s ",commandi,command.argv[commandi]);
printf("::::END command.argv:::: ");
#endif
/*get the dirs from PATH;PATH is a enviroment var*/
#ifdef DEBUG
printf("getDir start=========================== ");
#endif
getDir(dir);
#ifdef DEBUG
int diri=0;
for(;dir[diri]!=NULL;diri++)
{
printf("in main dir[%d] is %s ",diri,dir[diri]);
}
#endif
/*get the name of command,this must get the full path of the file,but here is for debug*/
command.name = GetChkFullPath(command.argv,dir);
#ifdef DEBUG
printf("in main after GetChkFullPath command.name=%s ",command.name);
#endif
if(command.name == NULL ){
/*the command inputed is not valid or is empty*/
continue;
fprintf(stderr, "Command %s not found ", command.argv[0]);
}
/*scan< > | ,isPipe isInRedir and isOutRedir store the position of "< > |" */
isPipe = isInRedir = isOutRedir = 0;
for( i= 0 ; command.argv[i] != NULL ; i++ )
{
if( strcmp( command.argv[i] , "<" ) == 0 ){
isInRedir = i;
}
if( strcmp( command.argv[i] , ">" ) == 0 ){
isOutRedir = i;
}
if( strcmp( command.argv[i] ,"|" ) == 0 ){
isPipe = i;
}
}
#ifdef DEBUG
printf("isin,inout,ispipe:%d%d%d ",isInRedir,isOutRedir,isPipe);
#endif
#ifdef DEBUG
command.argv[3] = NULL;
printf("show the argvs which is not set ");
for(i = 0 ; i < MAX_ARGS ; i++)
printf("argv[%d]=%s ",i,command.argv[i]);
#endif
/*excute a commmand*/
if(fork() == 0){
#ifdef DEBUG
printf("::::::::::::::::In fork==0:::::::::::::::::: ");
#endif
if(isInRedir){
fid = open(command.argv[isInRedir + 1],O_RDONLY);
close(STD_INPUT);//close standard input
dup(fid);//redirect fid to standard input
close(fid);
command.argv[isInRedir] = NULL; /*ignore the word after isInRedir*/
#ifdef DEBUG
printf("::::parseCommand command.argv:::: ");
printf("command.name = %s ",command.name);
for(commandi=0;command.argv[commandi]!=NULL;commandi++)
printf("command.argv[%d]=%s ",commandi,command.argv[commandi]);
printf("::::END command.argv:::: ");
#endif
}
if(isOutRedir){
fid = open(command.argv[isOutRedir + 1],O_WRONLY|O_CREAT);
close(STD_OUTPUT);
dup(fid);
close(fid);
command.argv[isOutRedir] = NULL;
#ifdef DEBUG
printf("::::parseCommand command.argv:::: ");
printf("command.name = %s ",command.name);
for(commandi=0;command.argv[commandi]!=NULL;commandi++)
printf("command.argv[%d]=%s ",commandi,command.argv[commandi]);
printf("::::END command.argv:::: ");
#endif
}
execv(command.name,command.argv);/*excute the command*/
#ifdef DEBUG
printf("::::::::::::::::In fork==0 END:::::::::::::::::: ");
#endif
}else{
#ifdef DEBUG
printf("::::::::::::::::In fork!=0:::::::::::::::::: ");
#endif
wait(&stat);/*wait for the end of child process*/
#ifdef DEBUG
printf("::::::::child process done!::IN FORK!=0:END::::: ");
#endif
}
// loopcount = 0;
}/*while*/
#ifdef DEBUG
printf("Shell terminaled ");
#endif
}
void printLineHead()
{
printf(" MANIO's miniSHELL>");
}
/**************************************************************
parseCommand()
*seprate cLine by WHITESPACE,put them to pchar[]
*#define WHITESPACE " ., &"
*bugs:This code does not handle multiple WHITESPACE characters
***************************************************************
*/
void parseCommand(char *cLine, char *pchar[]) {
int argc;
char *tCLine;
char **clPtr;
// Initialization
tCLine = cLine;
clPtr = &tCLine;
argc = 0;
// This code does not handle multiple WHITESPACE characters
while((pchar[argc++] = strsep(clPtr, WHITESPACE)) != NULL);
pchar[argc--] = NULL; // Null terminated list of strings
}
/****************************************************************
*get the path in PATH seprated by ":"
*****************************************************************
*/
intgetDir(char *dir[])
{
int i;
char *s;
char *oldpath;
char *ppath;
s=(char *)getenv("PATH");
#ifdef DEBUG
printf("s is %s ",s);
printf("s2=====%c ",s[2]);
#endif
oldpath = (char *)malloc(strlen(s)+1);
strcpy(oldpath,s);
#ifdef DEBUG
printf("oldpath is %s ",oldpath);
#endif
ppath = oldpath;
#ifdef DEBUG
printf("ppath charis %c ",*ppath);
#endif
for(i=0;*ppath!='';ppath++)
{
#ifdef DEBUG
printf("in for ppath charis %c ",*ppath);
#endif
if(*ppath==':')
{
dir[i++] = oldpath;
*ppath='';
oldpath = ppath+1;
}
}
dir[i] = NULL;
#ifdef DEBUG
int diri=0;
for(;dir[diri]!=NULL;diri++)
{
printf("dir[%d] is %s ",diri,dir[diri]);
}
#endif
return 1;
}
/****************************************************************
*get and check the full path which the user inputed
*dir :dirs in PATH
*argv:argv in command_t
*****************************************************************
*/
char *GetChkFullPath(char **argv,char **dir)
{
#ifdef DEBUG
printf(":::::::::::GetChkFullPath start ");
printf("argv[0]=%s dir[0]=%s ",argv[0],dir[0]);
#endif
char fullpath[MAX_PATH_LEN];
char *result;
int i;
result = NULL;
if( *argv[0] != '/' )
{
// *argv[0] != ''
#ifdef DEBUG
printf("JUGE argv[0]!=/ ");
#endif
for(i = 0;dir[i] != NULL ;i++)
{
strcpy(fullpath,dir[i]);
strcat(fullpath,"/");
strcat(fullpath,argv[0]);
#ifdef DEBUG
printf("after strcat fullpath:%s ",fullpath);
#endif
if(access(fullpath, X_OK | F_OK) != -1){
#ifdef DEBUG
printf("FOUND %s in %s ",argv[0],dir[i]);
#endif
result = (char *)malloc(strlen(fullpath)+1);
strcpy(result,fullpath);
break;
}
}
}else{
#ifdef DEBUG
printf("JUGE argv[0]==/ ");
#endif
if(access(argv[0], X_OK | F_OK) != -1){
#ifdef DEBUG
printf("FOUND %s",argv[0]);
#endif
result = (char *)malloc(strlen(argv[0])+1);
strcpy(result , argv[0]);
}
}
if(result == NULL ){
printf("%s is not a command. ",argv[0]);
return NULL;
}else{
#ifdef DEBUG
printf("GetChkFullPath end result == %s ",result);
#endif
return result;
}
}
页:
[1]
2