2017-03-08 186 views
1

林很新的shell腳本。我在shell腳本中運行了下面的sqoop命令並得到了結果。在shell腳本傳遞兩個參數到shell腳本

sqoop命令

#!/bin/bash 
while read table; do 
sqoop eval --connect jdbc:mysql://127.0.0.1/mydb --username root --password cloudera --query "describe $table" 
done<tables.txt 

輸入文件:tables.txt只有一個字:MYTAB和IM流汗了這一結果。

但是我怎麼能寫一個腳本從文件中取兩個參數,當tables.txt中的數據看起來像這樣:MYDB:MYTAB,並且我想將dbname和表名傳遞給腳本中的sqoop命令,看起來像下面。

#!/bin/bash 
while read table; do 
sqoop eval --connect jdbc:mysql://127.0.0.1/$dbname --username root --password cloudera --query "describe $table" 
done<tables.txt 

誰能告訴我如何提取從文件中的值,並把它們在文件中?

回答

2

更改read說法正確的字段分隔IFS設置爲:

while IFS=":" read -r dbname table; do 
+1

++超快速的答案:) – anubhava

+1

@anubhava後:沒辦法那麼快你_sir_!感謝您的補充! – Inian

+1

它正在工作。如果有多個參數,我們可以給出 ,而IFS =「:」讀取-r dbname table arg3 arg;請在我們的命令中使用它們。這非常清楚,謝謝! – Sidhartha