一.服务菜单脚本实例
#!/bin/sh
 
case $1 in
start)
        service mysqld start &> /dev/null
        echo "数据库启动成功"
        ;;
stop)
        service mysqld stop &> /dev/null
        echo "数据库停止成功"
        ;;
*)
        echo "请输入start或者stop"
        ;;
esac
~
 
 
二.群发邮件脚本实例
#!/bin/sh
for aa in `cat /root/maillist`
do
        mail $aa < /root/guanggao.html &> /dev/null
        echo "$aa地址发送邮件成功"
done
三.新建用户脚本实例
#!/bin/sh
for aa in `cat /root/userlist`
do
        adduser $aa
        echo "123.com" | passwd --stdin $aa &> /dev/null
        echo "$aa用户建立成功"
done
~
 
四.服务进程监控程序脚本实例
#!/bin/sh
if [ -f /var/run/httpd.pid ]
then
        echo "web service ok!!" > /dev/tty12
else
        service httpd restart > /dev/tty12
#       mail aa@163.com < /root/httpermessage
fi
 
五.zebra系统服务启动脚本实例
#!/bin/sh
#
# chkconfig: 2345 99 60
# description: Starts and stops the X Font Serv
 
case $1 in
start)
       /usr/local/sbin/zebra -d
       echo "Zebra is up"
       ;;
stop)
       killall -9 zebra
       rm -rf /var/run/zebra.pid
       echo "Zebra is down"
       ;;
restart)
       killall -9 zebra &> /dev/null
       rm -rf /var/run/zebra.pid
       sleep 1
       /usr/local/sbin/zebra -d &> /dev/null
       echo "Zebra restart ok"
       ;;
status)
       if [ -f /var/run/zebra.pid ]
       then
              echo "Zebra is runing..."
       else
              echo "Zebra is down...."
       fi
       ;;
*)
       echo "Usage: /etc/init.d/zebrad {start|stop|restart|status}"
esac
 
 
六.监控系统状态的脚本实例
# add 20 users one time
#!/bin/sh
i=1
num=20
pass=123456
 
while [ "$i" -le "$num" ]
do
    user="stu$i"
    if grep "$user" /etc/passwd >/dev/null
    then
         :
    else
         useradd "$user"
#        passwd "$user" >/dev/null 2>&1 << EOF
#$pass
#$pass
#EOF
         echo "$pass" | passwd --stdin "$user" >/dev/null 2>&1
    fi
    i=$((i+1))
done
 
# End of script
 
 
# To monitor cpu load by cmd "uptime"
# By licong on 2009/09/30
 
out_file=/tmp/uptime.out
mail_from=licong@aa.com
mail_to="licong@bb.com"
load_limit=1
> $out_file
 
field=$((`uptime | awk '{print NF}'` - 2))
load=$(uptime | awk '{print $'$field'}' | sed 's/,//')
load_int=$(echo $load | sed 's/\..*//')
 
if [ $load_int -ge $load_limit ]; then
    (
    echo -e "Load of cpu has exceeded $load \n"
    date
    echo
    uptime
    ) > $out_file
fi
 
if [ -n $out_file -a -s $out_file ]; then
    sendmail -f $mail_from -s "Load warming" $mail_to < $out_file
fi
 
rm -rf $out_file
 
# End of script
 
 
# My version of cp, ask for conferm
 
this_script=$(basename $0)
 
if [ $# -ne 2 ]
then
    echo "Usage: $this_script from to"
    exit 1
fi
 
from="$1"
to="$2"
 
if [ -e "$to" ]
then
    echo -e "$to exists, overwrite (yes/no)? \c"
    read answer
    if [ "$answer" != yes ]
    then
        echo "copy not performed"
        exit 0
    fi
else
    cp "$from" "$to"
fi
 
# End of script
七.带邮件通知功能的磁盘监控脚本实例
#
# Monit using of disk bf "df -m"
# By licong on 2010/07/21
#!/bin/sh
PCW_limit=75
PCA_limit=90
df_file=/tmp/df.out
 
echo -e "\n$(date)\n" > "$df_file"
 
#df -m | egrep -v '/dev/shm|cdrom|proc|Filesy' |      # for fs without lvm
 
df -m | sed 's/^ *//;/^[0-9]/!d' |                    # for fs with lvm
 
while read MB_total MB_used MB_free used dev_mount
do
    PC_used=$(echo $used | cut -d% -f1)
    if [ $PC_used -ge $PCW_limit -a $PC_used -lt $PCA_limit ]
    then
echo "Warning: $dev_mount used has exceded $used !" >> "$df_file"
    elif [ $PC_used -ge $PCA_limit ]
    then
echo "Alarm: $dev_mount used has exceded $used !" >> "$df_file"
    fi
done
 
echo >> "$df_file"
[ -n "$df_file" -a -s "$df_file" ] && mail -s "Warming of disk!" licong@aa.com < "$df_file"
rm -rf "$df_file"
 
# End of script
 
八.整理文档用脚本实例
/data目录下所有.swf文件移动到子目录下,子目录的名字取文件名的前2个字符;如果文件名只有1个字符,则目录名为2个重叠的该字符。
#!/bin/sh
cd /data
ls | grep '.swf' > filelist
 
while read filename
do
    mkdir -p ${filename:0:2}
    mv ${filename} ${filename:0:2}
done < filelist
 
ls -1 | awk -F'.' '{
    if (length($1)==1)
    {
        if (-d "$1$1")
        {
            system("mv "$0"/* "$1$1)
            system("rm -rf lib/en/"$0)
        }
        else
            {system("mv "$0" "$1$1)}
    }
}'
 
# End of script
 
 
#定义LOG文件名
LOGFILE=/home/tsptgz/tsptapp/log/fsgwkgetxf.log
echo $1 ftp 开始 >>$LOGFILE
 
ftp -n -i 22.0.50.1 <<+
user username password
cd /home/card/data/fs_gwk
lcd /home/tsptgz/tsptapp/acctbook/fsgwk/bankout/fsgwkFirst
get $1
+
 
if [ -s /home/tsptgz/tsptapp/acctbook/fsgwk/bankout/fsgwkFirst/$1 ]; then
        echo "文件"+/home/tsptgz/tsptapp/acctbook/fsgwk/bankout/fsgwkFirst/$1+"存在" >>$LOGFILE
else
        echo "文件"+/home/tsptgz/tsptapp/acctbook/fsgwk/bankout/fsgwkFirst/$1+"不存在" >>$LOGFILE
        exit 101;
fi