[Linux]-常用代码块
Posted NightPxy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Linux]-常用代码块相关的知识,希望对你有一定的参考价值。
1.读取执行的上级目录
basepath=$(cd `dirname $0`;cd ..; pwd)
2.Sudo切换用户并执行MySQL语句
echo ‘密码‘|sudo -u mysqladmin -S /usr/local/mysql/bin/mysql -uMySQL用户名 -pMySQL密码 -e "delete xxxx;"
3.参数读取
if [[ $# -ge 1 ]]; then opt="$1" shift case "$opt" in -help) print_msg "$helpInfo" ;; -day) targetTime=`date -d "$1" "+%Y%m%d"` train_day $targetTime ;; -daysection) beginTime=`date -d "$1" "+%Y%m%d"` endTime=`date -d "$2" "+%Y%m%d"` while [ "$beginTime" -le "$endTime" ] do train_day $beginTime beginTime=`date -d "$beginTime +1 day " +%Y%m%d` done ;; *) print_msg "train_day参数错误 $helpInfo" -err exit 1 ;; esac else targetTime=`date -d "-1 day " +%Y%m%d` print_msg "默认统计昨日->$targetTime" train_day $targetTime exit 0 fi
Shell基础库
#!/bin/bash function print_msg(){ help=‘print_msg 消息内容 [-err | -waring | -info]‘ if [[ $# -eq 1 ]]; then echo -e "