如何使用shell脚本
问题描述:
当我运行下面的代码,将数据存储在变量部门得到来自于同一个变量的数据库空间中的数据,testDate 和原料与材料成为彼此的混合物中存在的数据,例如空间如果 “计算机科学”是该部门的名称,然后“计算机”存储在可变 department
和“科学”存储在testDate
变量,但很明显,我想 存储整个“计算机科学”变量department
也是数据变量testDate
和testTime
因为您在输出中也可能观察到问题。 我该如何解决这个问题?如何使用shell脚本
mysql -uroot -proot -Dproject_ivr_db -rN --execute "SELECT Dpartment,TestDate,
TestTime FROM entrytests_datetime WHERE Discipline='msc'" |
while read department testDate testTime
do
echo "V,department=$department"
echo "V,testDate=$testDate"
echo "V,testTime=$testTime"
done
echo "E,resume"
输出:
V,department=computer
V,testDate=science
V,testTime=first february 2013 nine thirty a m
V,department=electronics
V,testDate=first
V,testTime=february 2013 ten thirty a m
E,resume
答
基本上,你必须确保的字段由一个空格其他一些字符在输出分开mysql
,你必须告诉哪个字符外壳那是。
通过IFS变量告诉shell。
告诉mysql
命令与--batch
或-B
选项来完成的:
--batch
,使用标签作为列分离器,具有在一个新行的每一行-B
打印结果。使用此选项,
mysql
不使用历史文件。在nontabular输出格式批处理模式的结果和特殊字符逸出。通过使用原始模式可能会禁用转义;请参阅
--raw
选项的说明。
所以:
IFS=" " # Tab (only) in quotes
mysql -B -uroot -proot -Dproject_ivr_db -rN --execute "SELECT Dpartment,TestDate,
TestTime FROM entrytests_datetime WHERE Discipline='msc'" |
while read department testDate testTime
do
echo "V,department=$department"
echo "V,testDate=$testDate"
echo "V,testTime=$testTime"
done
echo "E,resume"
+0
感谢您的帮助 – shehzy 2013-02-21 07:42:12
你的意思是使用参数或变量这样的链接:http://*.com/questions/10229324/pass-parameter-to-mysql-script-command-line ? – 2013-02-21 06:48:29