1. 程式人生 > >Shell腳本中執行sql語句操作

Shell腳本中執行sql語句操作

out ins host source con more char 演示 -1

  這篇文章主要介紹了Shell腳本中執行sql語句操作mysql的5種方法,本文講解了將SQL語句直接嵌入到shell腳本文件中、命令行調用單獨的SQL文件、使用管道符調用SQL文件等方法,需要的朋友可以參考下

  對於自動化運維,諸如備份恢復之類的,DBA經常需要將SQL語句封裝到shell腳本。本文描述了在Linux環境下mysql數據庫中,shell腳本下調用sql語句的幾種方法,供大家參考。對於腳本輸出的結果美化,需要進一步完善和調整。以下為具體的示例及其方法。

1、將SQL語句直接嵌入到shell腳本文件中

--演示環境 [[email protected] ~]# more /etc/issue CentOS release 5.9 (Final) Kernel \r on an \m [email protected]
/* */[(none)]> show variables like ‘version‘; +---------------+------------+ | Variable_name | Value | +---------------+------------+ | version | 5.6.12-log | +---------------+------------+ [[email protected] ~]# more shell_call_sql1.sh #!/bin/bash # Define log TIMESTAMP=`date +%Y%m%d%H%M%S` LOG=call_sql_${TIMESTAMP}.log echo "Start execute sql statement at `date`." >>${LOG} # execute sql stat mysql -uroot -p123456 -e " tee /tmp/temp.log drop database if exists tempdb; create database tempdb; use tempdb create table if not exists tb_tmp(id smallint,val varchar(20)); insert into tb_tmp values (1,‘jack‘),(2,‘robin‘),(3,‘mark‘); select * from tb_tmp; notee quit" echo -e "\n">>${LOG} echo "below is output result.">>${LOG} cat /tmp/temp.log>>${LOG} echo "script executed successful.">>${LOG} exit; [[email protected]
/* */ ~]# ./shell_call_sql1.sh Logging to file ‘/tmp/temp.log‘ +------+-------+ | id | val | +------+-------+ | 1 | jack | | 2 | robin | | 3 | mark | +------+-------+ Outfile disabled.

2、命令行調用單獨的SQL文件

[[email protected] ~]# more temp.sql tee /tmp/temp.log drop database if exists tempdb; create database tempdb; use tempdb create table if not exists tb_tmp(id smallint,val varchar(20)); insert into tb_tmp values (1,‘jack‘),(2,‘robin‘),(3,‘mark‘); select * from tb_tmp; notee [[email protected]
/* */ ~]# mysql -uroot -p123456 -e "source /root/temp.sql" Logging to file ‘/tmp/temp.log‘ +------+-------+ | id | val | +------+-------+ | 1 | jack | | 2 | robin | | 3 | mark | +------+-------+ Outfile disabled.

3、使用管道符調用SQL文件

[[email protected] ~]# mysql -uroot -p123456 </root/temp.sql Logging to file ‘/tmp/temp.log‘ id val 1 jack 2 robin 3 mark Outfile disabled. #使用管道符調用SQL文件以及輸出日誌 [[email protected] ~]# mysql -uroot -p123456 </root/temp.sql >/tmp/temp.log [[email protected] ~]# more /tmp/temp.log Logging to file ‘/tmp/temp.log‘ id val 1 jack 2 robin 3 mark Outfile disabled.

4、shell腳本中MySQL提示符下調用SQL

[[email protected] ~]# more shell_call_sql2.sh #!/bin/bash mysql -uroot -p123456 <<EOF source /root/temp.sql; select current_date(); delete from tempdb.tb_tmp where id=3; select * from tempdb.tb_tmp where id=2; EOF exit; [[email protected] ~]# ./shell_call_sql2.sh Logging to file ‘/tmp/temp.log‘ id val 1 jack 2 robin 3 mark Outfile disabled. current_date() 2014-10-14 id val 2 robin

5、shell腳本中變量輸入與輸出

[[email protected] ~]# more shell_call_sql3.sh #!/bin/bash cmd="select count(*) from tempdb.tb_tmp" cnt=$(mysql -uroot -p123456 -s -e "${cmd}") echo "Current count is : ${cnt}" exit [[email protected] ~]# ./shell_call_sql3.sh Warning: Using a password on the command line interface can be insecure. Current count is : 3 [[email protected] ~]# echo "select count(*) from tempdb.tb_tmp"|mysql -uroot -p123456 -s 3 [[email protected] ~]# more shell_call_sql4.sh #!/bin/bash id=1 cmd="select count(*) from tempdb.tb_tmp where id=${id}" cnt=$(mysql -uroot -p123456 -s -e "${cmd}") echo "Current count is : ${cnt}" exit [[email protected] ~]# ./shell_call_sql4.sh Current count is : 1

Shell腳本中執行sql語句操作

相關推薦

Shell執行sql語句操作

out ins host source con more char 演示 -1   這篇文章主要介紹了Shell腳本中執行sql語句操作mysql的5種方法,本文講解了將SQL語句直接嵌入到shell腳本文件中、命令行調用單獨的SQL文件、使用管道符調用SQL文件等方法,需

shell 執行SQL語句 -e "..."

... from rom bin 執行 dev use class col /usr/local/mysql/bin/mysql -uroot -p123456 -e " use faygo source faygo.sql select * from devquit "

shell執行sql(mysql為例)

技術分享 src ins 註釋 ima 嘗試 方式 sql腳本 分享圖片 1、sql腳本(t.sql) insert into test.t value ("LH",88); 2、shell腳本(a.sh 為方便說明,a.sh與t.sql在同一目錄下) 說明:

Shell指令碼執行sql語句操作mysql

--演示環境  [[email protected] ~]# more /etc/issue  CentOS release 5.9 (Final)  Kernel \r on an \m    [email protected][(none)]> show variab

shellSQL*Plus的環境變量

執行sql 簡寫 版本信息 shell 設置 環境變量 登錄 內容 plus -silent 用於消除在登錄SQL*Plus連接到數據庫時,默認顯示SQL*Plus及數據庫的版本信息 ,同時消除顯示提示符。 可以簡寫為-s set pagesiz

shell執行python並接收其返回值的例子

erl 結果 port ria 需要 deb def ID pri 1.在shell腳本執行python腳本時,需要通過python腳本的返回值來判斷後面程序要執行的命令 例:有兩個py程序 hello.py 復制代碼代碼如下: def main(): pri

shell執行shell

執行 info 分享 $1 所在 結果 com echo 目錄 1、a.sh #!/bin/sh name="hello" ./b.sh $name 2、b.sh(這裏把b.sh與a.sh放在同一目錄下,便於演示) #!/bin/shecho "parameter

shell介紹,shell結構和執行方式,date命令的用法,shell的變量簡介

linux 操作系統 centos shell腳本 筆記內容:20.1 shell腳本介紹20.2 shell腳本結構和執行20.3 date命令用法20.4 shell腳本中的變量筆記日期:2017-11-2120.1 shell腳本介紹 Shell Script,Shell腳本與Wind

20.1 shell介紹 20.2 shell結構和執行 20.3 date命令用法 20.4 shell的變量

20.1 shell腳本介紹 20.2 shell腳本結構和執行 20.3 date命令用法 20.4 shell腳本中的變量- 20.1 shell腳本介紹 - 20.2 shell腳本結構和執行 - 20.3 date命令用法 - 20.4 shell腳本中的變量 # 20.1 Shell腳本介紹 -

shell的邏輯判斷,文件目錄屬性判斷,if特殊用法,case語句

shell腳本中的邏輯判斷 文件目錄屬性判斷 if特殊用法 case判斷 筆記內容:20.5 shell腳本中的邏輯判斷20.6 文件目錄屬性判斷20.7 if特殊用法20.8/20.9 case判斷筆記日期:2017-11-2220.5 shell腳本中的邏輯判斷在所有的編程語言中都會有if

linux的shell介紹、shell結構和執行、date命令用法、shell的變量

長度 自動 內置變量 開頭 ash number 邏輯 ply 應該 Shell腳本介紹 shell是一種腳本語言 可以使用邏輯判斷、循環等語法 可以自定義函數 shell是系統命令的集合 shell腳本可以實現自動化運維,能大大增加我們的運維效率 Shell腳本結構和

shell介紹、結構和執行、date命令用法、shell的變量

出現 用法 日期 腳本語言 通過 idle ali 日歷 實現 shell 腳本介紹 shell 是一種腳本語言 shell有自己的語法,可以使用邏輯判斷、循環等語法 可以自定義函數,目的就是為了減少重復的代碼 shell 是系統命令的集合 shell 腳

20.1-4 shell介紹 shell結構和執行 date命令用法 shell的變量

十六周五次課(4月17日)20.1 shell腳本介紹20.2 shell腳本結構和執行20.3 date命令用法%w 星期幾 %W今年的第幾周cal是顯示日歷的時間戳可以相互查詢 20.4 shell腳本中的變量20.1-4 shell腳本介紹 shell腳本結構和執行 date命令用法 shell腳本中

shell,shell結構和執行方法,data命令,shell的變量

shellshell腳本結構和執行方 date shell腳本中的變量 shell是什麽shell是一種腳本語言 aming_linux blog.lishiming.net可以使用邏輯判斷、循環等語法可以自定義函數shell是系統命令的集合shell腳本可以實現自動化運維,能大大增加我們的運

shell介紹,shell結構和執行,date命令用法,shell的變量

shellShell腳本介紹 shell是一種腳本語言 blog.lishiming.net(阿銘的博客,可以去裏面找shell習題)可以使用邏輯判斷、循環等語法可以自定義函數,減少重復代碼shell是系統命令的集合shell腳本可以實現自動化運維,能大大增加我們的運維效率 Shell腳本結構和執行 開頭需

六十七、shell介紹、shell結構和執行、date命令用法、shell的變量

shell腳本介紹 shell腳本結構和執行 date命令用法 shell腳本中的變量 六十七、shell腳本介紹、shell腳本結構和執行、date命令用法、shell腳本中的變量一、shell腳本介紹 shell是一種腳本語言 aming_linux blog.lishiming.ne

68.shell介紹、shell結構和執行、date命令用法、shell的變量

shell腳本介紹 shell腳本結構和執行 date命令用法 shell腳本中的變量 一、shell是什麽 shell是一種腳本語言 aming_linux blog.lishiming.net 可以使用邏輯判斷、循環等語法 可以自定義函數 shell是系統命令的集合 shell腳本可

shell介紹 shell結構和執行 date命令用法 shell的變量

實現 images 個數 直接 shel png fff 冒號 用法 一、shell腳本介紹shell腳本要想寫好,必須通過不斷地去練習寫才能寫好,沒有捷徑要在我們拿到一個需求的時候有一個腳本的大致思路,想到需求怎麽去實現shell腳本可以大大提高我們的工作效率二、shel

shell 測試操作符號及 && 與 ||

new 失敗 tro 時間 file -s 如果 The cut 測試操作符 常用測試操作符 英文名 說明 -f 文件 file 文件存在且為普通文件為真,即測試表達式成立 -d 文件 directory 文件存在且為目錄文件為真,即測試表達式成立 -

shell介紹、shell結構和執行、date命令用法、shell的變量

腳本 執行 repl inpu 集合 語法錯誤 天前 date NPU 一:shell腳本介紹 shell是一種腳本語言 可以使用邏輯判斷、循環等語法可以自定義函數shell是系統命令的集合shell腳本可以實現自動化運維,能大大增加我們的運維效率 二:shell腳本結構