1. 程式人生 > 其它 >06-shell-函式

06-shell-函式

 

root@ubuntu2004:~# cat send_email.sh 
#!/bin/bash


email_send='18*@qq.com'
email_passwd='授權碼'
email_smtp_server='smtp.qq.com'

. /etc/os-release

msg_error() {
  echo -e "\033[1;31m$1\033[0m"
}

msg_info() {
  echo -e "\033[1;32m$1\033[0m"
}

msg_warn() {
  echo -e "\033[1;33m$1\033[0m"
}


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 \E[0m" echo -n "$1" && $MOVE_TO_COL echo -n "[" if [ $2 = "success" -o $2 = "
0" ] ;then ${SETCOLOR_SUCCESS} echo -n $" OK " elif [ $2 = "failure" -o $2 = "1" ] ;then ${SETCOLOR_FAILURE} echo -n $"FAILED" else ${SETCOLOR_WARNING} echo -n $"WARNING" fi ${SETCOLOR_NORMAL} echo -n "]" echo } install_sendemail () {
if [[ $ID =~ rhel|centos|rocky ]];then rpm -q sendemail &> /dev/null || yum install -y sendemail elif [ $ID = 'ubuntu' ];then dpkg -l sendemail &>/dev/null || { apt update; apt install -y libio-socket-ssl-perl libnet-ssleay-perl sendemail ; } else color "不支援此作業系統,退出!" 1 exit fi } send_email () { local email_receive="$1" local email_subject="$2" local email_message="$3" sendemail -f $email_send -t $email_receive -u $email_subject -m $email_message -s $email_smtp_server -o message-charset=utf-8 -o tls=yes -xu $email_send -xp $email_passwd [ $? -eq 0 ] && color "郵件傳送成功!" 0 || color "郵件傳送失敗!" 1 } if [ $# -ne 3 ];then color "指令碼引數不正確!" 1 msg_info "Usage: `basename $0` <mail_address> <subject> <message>" exit 1 fi install_sendemail send_email "$1" "$2" "$3"

 

root@ubuntu2004:~# ./send_email.sh [email protected] Winter  "Are you OK"