1.程序shell腳本的內(nèi)容大致如下:
#! /bin/bash
#execute all scripts in specified directory
MYDATE=`date +%Y%m%d%H%M%S`
SQL_PATHS=(目錄1 目錄2 目錄3 目錄4) #按文件夾執(zhí)行順序?qū)懭肽_本文件夾路徑丁屎,根路徑為當(dāng)前shell腳本路徑
LOG_FILE=${MYDATE}.log
LOG_FILE1=test.log
host=******
port=3306
user=root
confirm=
password=******
function read_dir() {
count=0
echo -e "=======================sql files in directory of $1 will be executed below=======================" >>${LOG_FILE}
echo "=======================sql files in directory of $1 will be executed below======================="
for file in `ls $1`
do
if [ -d $1"/"$file ]
then
read_dir $1"/"$file
else
let count++
echo $count $1"/"$file
echo -e $count $1"/"$file >>${LOG_FILE}
fi
done
echo "=======================sql files in directory of $1 will be executed above======================="
echo -e "=======================sql files in directory of $1 will be executed above=======================\n" >>${LOG_FILE}
}
function exec_sql() {
for file in `ls $1`
do
if [ $file != "sys_data_pb_mix.sql" ]
then
if [ -d $1"/"$file ]
then
exec_sql $1"/"$file
else
mysql -h$host -P$port -u$user -p$password database_name --default-character-set=utf8 < $1"/"$file >& error.log
echo "=======================executed result in $1"/"$file======================="
echo -e "=======================executed result in $1"/"$file=======================" >>${LOG_FILE}
cat error.log >>${LOG_FILE} #輸出執(zhí)行日志
error=`grep ERROR error.log` #讀取錯誤日志信息
if [ -n "$error" ] ;
then #如果有錯誤就退出程序
echo $error
exit
fi
fi
fi
done
echo "=======================sql files in directory of $1 has been executed success===================="
echo -e "=======================sql files in directory of $1 has been executed success====================" >>${LOG_FILE}
}
for path in ${SQL_PATHS[@]}
do
read_dir $path
done
echo "sql files will be executed on MySql:" $host "-P" $port " in database database_name with" $user
for path in ${SQL_PATHS[@]}
do
exec_sql $path
done
exit
;;
esac
2.在window下一切正常未状,但是到了linux環(huán)境下就直接報錯了 锯仪,類似“\r” command not found這種,具體的說明見這里:
https://cloud.tencent.com/developer/article/1068665
查資料的時候首先發(fā)現(xiàn)的是“dos2unix”命令的轉(zhuǎn)換皮迟,但是發(fā)現(xiàn)centos7上并沒有這個命令,查看了下安裝方法桑寨,比較繁瑣伏尼,發(fā)現(xiàn)有替代命令,果斷啟用dos2unix.
這是上邊帖子中的描述:
當(dāng)然尉尾,如果只是要轉(zhuǎn)換格式爆阶,我們還有多種替代方案,沒必要吊死在 dos2unix/unix2dos 上沙咏。畢竟有些系統(tǒng)可能沒有這 2 個命令辨图。
替代命令①:sed
以下2種都可以:
sed -e 's/.$//g' oldfile > newfile
sed 's/^M//' oldfile > newfile #注意 ^M = Ctrl + v,Ctrl + m 命令組合打印出來的字符
替代命令②:tr
cat oldfile | tr -d "\r" > newfile
3.另外還有個問題,直接通過cat這種方式生成的shell腳本肢藐,默認(rèn)是沒有執(zhí)行權(quán)限的故河,需要 chmod +x shell.sh來賦權(quán)限,讓腳本可已執(zhí)行吆豹。