shell指令碼呼叫mysql資料庫 1

2021-10-17 20:39:45 字數 1805 閱讀 7361

1.獲取當前日期或者昨天日期(限定日期格式)

logdate=`date +%y%m%d`

date_1=`date -d "1 day ago" +%y%m%d`

2.通過shell讀取配置檔案的資料庫資訊3.使用shell中mysql命令匯入檔案到資料庫

mysqlimportfile="mysql --local-infile -h資料庫ip -u資料庫使用者名稱 -p資料庫密碼  $資料庫使用者名稱 -e"

後續可追加以下引數

a.into table tablename:將檔案內資料插入指定表中

b.replace into table tablename:replace作用:有相同資料先刪除再插入

c.fields terminated by ',' :檔案中以','分割來作為資料庫列

d.optionally enclosed by '\"':插入時忽略檔案中的"符號

e.lines terminated by '\n':檔案中以'\n'換行分割作為資料庫行

f.(colname1,colname2,colname3):指定插入表的列名

4.讀取資料庫資料到指定檔案student.csv

mysqldealstate="mysql -s -n -u$dbuser -p$dbpwd -h$dbip -p$dbport $dbname  -e"

tmpfile=/output/student.csv(這裡檔案是臨時檔案可能不存在)

`touch $tmpfile`(建立檔案)

引數解讀:

-s:查詢結果以逗號分隔資料庫列,以換行分隔資料庫行

-n:查詢結果中不包括列名

`$mysqldealstate "select * from student" > $tmpfile `

5.shell中執行mysql語句

mysqldealstate="mysql -s -n -u$dbuser -p$dbpwd -h$dbip -p$dbport $dbname  -e"

`$mysqldealstate "update student set age = 13 where id = 1 "`

6.在shell指令碼中定義變數,=兩邊不能有空格

7.統計檔案中關鍵字出現次數(這裡是統計換行的總數)

filelinescount=`grep $'\n' $ |wc -l`

8.echo的不同用法

a.cnt=`echo $1 | grep pwd | wc -l`  統計呼叫當前s**件第乙個引數中pwd的個數 

b.echo "...." 列印到控制台

c.echo "" >> $file 列印內容到指定資料夾

9.ifelse流程控制語句(ifelse還可以巢狀使用)

if [ 判斷條件 ];then

處理語句

else

處理語句

exit 1

fi10.whiledo迴圈

while [ 判斷條件 ]

do符合判斷條件執行某些操作

if [ 判斷條件 ];then

break //跳出while迴圈

continue  //跳出本次迴圈,進入下一次迴圈

else  

fidone

Python 呼叫shell指令碼

python呼叫shell指令碼,有兩種方法 os.system cmd 或os.popen cmd 前者返回值是指令碼的退出狀態碼,後者的返回值是指令碼執行過程中的輸出內容。實際使用時視需求情況而選擇。現假定有乙個shell指令碼test.sh bin bash 1.echo hello worl...

shell指令碼中呼叫其他指令碼

目前來說有三種方法 1.指令碼絕對路徑 這個方式是最普通的,底層呼叫的是fork實現,執行的時候開乙個子shell執行呼叫的指令碼,子shell執行的時候,父shell還在 子shell執行完畢後返回父shell,子shell從父shell繼承環境變數,但是子shell中的環境變數不會帶回父shel...

shell指令碼呼叫mysql資料庫 2

11.mysql的日期型別 datetime 顯示格式 yyyy mm dd hh mm ss 時間範圍 1000 01 01 00 00 00到9999 12 31 23 59 59 date 顯示格式 yyyy mm dd 時間範圍 1000 01 01到9999 12 31 timestamp...