版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/devilcry13/article/details/84109584
检查命令是否在服务器上存在
cmd_exist() {
type $1 >/dev/null 2>&1 && return 0 || return 1
}
cmd_exist "unzip" || sudo yum install -y unzip
获取服务器IP地址
get_ip() {
#ifconfig -a| awk -F':' '{ if(NR==2){ print $2 } }'| cut -d' ' -f1
IP=`ifconfig | grep -w inet | grep -v "127.0.0.1" | awk '{ print $2}'| tr -d "addr"`
echo ${IP#*:}
}
去重字符串数组中的重复项
remove_dup_item() {
local ustr=""
local orgstr=$1
local tmpstr=$1
for str in $orgstr; do
tmpstr=${tmpstr#*$str}
isuniq=yes
for lstr in $tmpstr; do
if [ "$lstr" = "$str" ]; then
isuniq=no
break
fi
done
if [ "$isuniq" = "yes" ]; then
if [ -z "$ustr" ]; then
ustr=$str
else
ustr="$ustr $str"
fi
fi
done
echo $ustr
}
添加定时任务
PROGRAM="/export/App/app_manager/updata_app.sh"
CRONTAB_NOTE="## $PROGRAM"
CRONTAB_CMD="0 2 * * * $PROGRAM"
#添加定时任务
add_cron_task() {
(crontab -l 2>/dev/null | grep -Fv $CRONTAB_NOTE; echo "$CRONTAB_NOTE") | crontab -
(crontab -l 2>/dev/null | grep -Fv $PROGRAM; echo "$CRONTAB_CMD") | crontab -
}
去除定时任务
#去除定时任务
PROGRAM="/export/App/app_manager/updata_app.sh"
CRONTAB_NOTE="## $PROGRAM"
CRONTAB_CMD="0 2 * * * $PROGRAM"
rmv_cron_task() {
(crontab -l 2>/dev/null | grep -Fv $CRONTAB_NOTE) | crontab -
(crontab -l 2>/dev/null | grep -Fv $PROGRAM) | crontab -
}
带颜色打印
echo_red() {
echo -e "\033[31m$*\033[0m"
}
echo_red_error() {
echo -e "\033[31m[ERROR]: $*\033[0m"
}
echo_red_success() {
echo -e "\033[31m[SUCCESS]: $*\033[0m"
}
echo_green() {
echo -e "\033[32m$1\033[0m"
}
echo_green_info() {
echo -e "\033[32m[INFO]: $*\033[0m"
}
echo_yellow() {
echo -e "\033[33m$*\033[0m"
}
echo_yellow_warn() {
echo -e "\033[33m[WARNNING]: $*\033[0m"
}
echo_blue() {
echo -e "\033[34m$*\033[0m"
}
echo_blue_info() {
echo -e "\033[34m[INFO]: $*\033[0m"
}
echo_pink() {
echo -e "\033[35m$*\033[0m"
}
比较文件
compare_file() {
diff -q $1 $2 1>/dev/null 2>&1
echo $?
}
生成不带注释的文件
#去开头空格、去#号注释行、去空行
get_nc_file() {
sed -e 's/^\s*//g' -e '/^#/d' -e '/^$/d' $1>$2
}
检查磁盘空间
# param1, 文件路径
# param2, 需要比较的空间大小,单位m
disc_space_enough() {
free_space=`df -m $1| tail -1 | awk -F" " '{print $4}'`
[ ${free_space} -ge $2 ] && echo 0 || echo 1
}
检查端口是否被占用
#检查端口是否被占用
port_ava() {
sudo netstat -anp | grep ":$1" >/dev/null 2>&1
[ $? -eq 0 ] && return 1 || return 0
}