发几个自己以前写的脚本,其中有两个监控系统状态的简单脚本,修改一下可以直接拿来用。用到的方法,在我的shell学习笔记系列中都有介绍。由于脚本都比较简单,就不写那么多注释了,有需要修改借用的朋友可以在此留言。

##############################################################

# add 20 users one time

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 2009/09/25
#

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个重叠的该字符

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