|
下面的脚本只用了一些最基本的bash知识,学习起来简单。
好多东西我也记不起拉,先贴上来吧.
当时自己可能写了很多没用的东西
大家看看而已,高手就算啦
[code:1]
#!/bin/bash
#Author: updatedb
#Date: Feb, 14' 04
#Descritpion: send mail to a group
#Error: delete function have a error
TITLE=test
MAILCONTENT=index.htm
USERLIST=/root/.mailsend.conf
USERADDLIST=""
USERDELLIST=""
LASTERROR=0
MAILADDR=""
OTHERADDR=""
CURSOR=0
SIGN=0
TAIL=0
function help() {
cat<< _ACEOF
USAGE: Send mail to a group, please using this command
as follow:
COMMAND:
mailsend [-h|--help] -[s|S] "subject" -[c|C] fileName -[u|U] unamelistfile -[a|A] useraddlist -[d|D] userdellist
OPTION:
-h | --help print this page
-a | -A user list will be added to mail list
-d | -D user list wlll be deleted from mail list
-s | -S read the subject of the letter, default value is ShenQu e-bussiness
-c | -C file name you want to send
-u | -U mail address list
EXAMPLE: mailsend -s "Test" -c test.html -u user.conf
_ACEOF
exit 0
}
function quit() {
echo "user list file check finished!!"
exit 0
}
function error() {
cat << _ACEOF
$1 doesn't exist or the file can't be read.
You must specified the position of $1
_ACEOF
exit 1
}
function success() {
if [ $1 -ne 0 ] ; then
if [ $1 -eq 2 ] ; then
exit 2
fi
echo Program return code equal to $1, there are some error.
help
exit $1
fi
}
#test the file be specfiied to send can be read
#user list file couldn't be found.
function inputChk() {
if test -n $1 && ! test "$1" = ""; then
if ! [ -r $1 ] ; then
error $1
fi
fi
}
#get all chars but the last char
function chopChar() {
#remove the last char in string and return it in $rval
if test -z "$1" ; then
REMAINSTRING=""
return
fi
#wc put some space behind the output this is why we need sed:
NUMOFCHAR=`echo -n "$1" | wc -c | sed -e 's/ //g'`
if test "$NUMOFCHAR" -eq "1" ; then
REMAINSTRING=""
return
fi
NUMOFCHARMINUS1=`expr $NUMOFCHAR "-" 1`
#now we cut all but the last char
REMAINSTRING=`echo -n $1 | cut -b 0-${NUMOFCHARMINUS1}`
}
#if the line is null, return ""
function chopLine() {
if test -z "$1" ; then
MAILADDR=""
return
fi
LINE=`echo -n "$1" | sed -e 's/ //g'`
LENGHT=`echo -n "$LINE" | wc -c`
if test $LENGHT -lt 5 ; then
MAILADDR=""
echo "The Invalid mail address: $LINE"
return
fi
numOfChar "@" "$1"
if [ $NUM -ne 1 ] ; then
echo "The Invalid mail address: $LINE"
MAILLADDR=""
return
fi
MAILADDR="$LINE"
return
}
#get the last char
function lastChar() {
#remove the last char in string and return it in $rval
if test -z "$1" ; then
LASTCHAR=""
return
fi
#wc put some space behind the output this is why we need sed:
NUMOFCHAR=`echo -n "$1" | wc -c | sed -e 's/ //g'`
#now we cut all but the last char
LASTCHAR=`echo -n $1 | cut -b $NUMOFCHAR`
return
}
#stastic the line number of the file
function numOfLine() {
NUMOFLINE=`cat $1 | wc -l | sed -e 's/ //g'`
return
}
#stastic the char $1 number of the line $2
function numOfChar() {
NUM=0
POSITIONSIGN=1
NUMOFCHAR=`echo $2 | wc -c | sed -e 's/ //g'`
FIRSTCHAR=`echo $2 | cut -b $POSITIONSIGN`
while test $POSITIONSIGN -le $NUMOFCHAR ; do
if [ "$FIRSTCHAR" = "@" ] ; then
NUM=`expr $NUM + 1`
fi
POSITIONSIGN=`expr $POSITIONSIGN + 1`
FIRSTCHAR=`echo $2 | cut -b $POSITIONSIGN`
done
return
}
#add a line that containes $1 to $2
function addLine() {
if [ -r $2 ] ; then
echo $1 >> "$2"
lineFilter "$2"
checkUserList "$2"
fi
return
}
#add a lot of lines that be contained, $1 is filename
function addLines() {
cat $1 | while read LINE
do
addLine "$LINE" "$USERADDLIST"
done
}
#delete a line that contains $1 from $2
function deleteLine() {
cat $2 | sed -e "/$1/d" > $2.cre
rm $2
mv $2.cre $2
return
}
#delete a lot of line , $1 is filename
function deleteLines() {
cat $1 | while read LINE
do
deleteLine "$LINE" "$USERLIST"
done
}
#check if the user list have some repeated items.
function checkUserList() {
echo "Start check user list file ..."
cat $1 | while read MAILADDR
do
SIGN=`expr $SIGN + 1`
TAIL=`expr $NUMOFLINE - $SIGN`
[ $TAIL -eq 0 ] && echo "End check user list file !!!"
CURSOR=`expr $SIGN + 1`
tail -n $TAIL $1 | while read OTHERMAIL
do
[ "$MAILADDR" = "$OTHERMAIL" ] && echo "Warning: There are some item repeatly in $ $SIGN line with $CURSOR line" && LASTERROR=2
CURSOR=`expr $CURSOR + 1`
done
done
return
}
#crean the dupliction of line
function lineFilter() {
cat $1 | sed -e 's/[[:space:]]*//g' | sort -u | uniq -i > $1.cre
rm $1 -f
mv $1.cre $1 -f
return
}
#now we will must find USER argument, read file echo line every.
function mailsend() {
cat $1 | while read USER
do
#if $USER is a invalid email address, then ignore it.
chopLine "$USER"
[ "$MAILADDR" = "" ] && continue
#send mail to $USE
mail -s "$TITLE" $USER < $MAILCONTENT || LASTERROR=$?
success "$LASTERROR"
echo "mail -s \"$TITLE\" $USER < $MAILCONTENT"
done
}
#parse the arguments from the command lines.
while test $# -gt 0 ; do
case "${1}" in
-h | --help | --h* ) # -h for help
help ;;
-a | -A )
shift
test $# -eq 0 && help
USERADDLIST=$1
shift;;
-d | -D )
shift
test $# -eq 0 && help
USERDELLIST=$1
shift;;
-s | -S ) # -s for subject
shift
test $# -eq 0 && help
TITLE=$1
shift ;;
-c | -S ) # -c for the context file
shift
test $# -eq 0 && help
MAILCONTENT=$1
shift ;;
-u | -U )
shift
test $# -eq 0 && help
USERLIST=$1
shift ;;
-- )
shift;
break ;; # stop option processing
-* )
help ;; # unknown option
* )
help ;; # first non-opt arg
esac
done
inputChk "$USERLIST"
inputChk "$MAILCONTENT"
inputChk "$USERADDLIST"
inputChk "$USERDELLIST"
numOfLine "$USERLIST"
lineFilter "$USERLIST"
echo "--------------------------------- USER LIST REPEATED ITEMS LIST ---------------------------------------------"
checkUserList "$USERLIST"
echo "-------------------------------------------------------------------------------------------------------------"
if ! [ "$USERADDLIST" = "" ] ; then
addLines "$USERADDLIST"
fi
if ! [ "$USERDELLIST" = "" ] ; then
deleteLines "$USERDELLIST"
fi
#mailsend "$USERLIST"
success "$LASTERROR"
[/code:1]
这段代码用来检查用户列表的有效唯一性
[code:1]
#!/bin/bash
#first get a line
#compare with other line
USERLIST="test"
MAILADDR=""
OTHERMAIL=""
CURSOR=0
SIGN=0
TAIL=0
function error() {
echo $1
}
function success() {
echo $1
exit 0
}
function numOfLine() {
NUMOFLINE=`cat $1 | wc -l | sed -e 's/ //g'`
return
}
function checkUserList() {
numOfLine "$USERLIST"
cat $USERLIST | while read MAILADDR
do
SIGN=`expr $SIGN + 1`
TAIL=`expr $NUMOFLINE - $SIGN`
[ $TAIL -eq 0 ] && success "Success finished"
CURSOR=`expr $SIGN + 1`
tail -n $TAIL $USERLIST | while read OTHERMAIL
do
if [ "$MAILADDR" = "$OTHERMAIL" ] ; then
error "these are some item repeatly in $SIGN line with $CURSOR line"
break
fi
CURSOR=`expr $CURSOR + 1`
done
done
}
if [ -e $USERLIST.create ] ; then
rm -f $USERLIST.create
fi
cat $USERLIST | sed -e 's/[[:space:]]*//g' | sort -u > $USERLIST.CRE
rm $USERLIST -f
mv $USERLIST.CRE $USERLIST -f
checkUserList
[/code:1]
用户的邮件列表我放在/root/.mailsend.conf,内容好像是一个邮件
地址的列表,时间太久啦。 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
×
|