2011-08-28 55 views
0

任何人都可以幫助使用此查詢。目前試圖使用sed將變量傳遞給xml。這隻適用於文本,但是它不會拾取bash中的變量,例如需要由用戶輸入的$ 1,$ 2等。試圖通過空格傳遞bash變量

EG:

sed 's/<\/sProblemDesc>/'"anew test"'&/' create.xml

成功輸出 <sProblemDesc>anew test</sProblemDesc>

然而插入重新測試部,其要麼帶回變量的文本名稱,而不是數據之間的1 $變量時。

用戶將運行腳本,這樣

./script.sh "enter stuff here" "enter more stuff" "21313122131"

所以,我需要知道如何得到它與變工作,但也考慮到,我不必使用引號分開考慮變量。(如果有另一種方式做到這一點讓我知道:d)

任何疑問讓我知道歡呼

更新: 作爲請求的變量名稱只是VA riablename =「$ 1」

我試過刪除引號以及只有= $ 1似乎並沒有工作,所以我猜測它是因爲當運行腳本(./script「東西在這裏」「等「」等「我在那裏用引號,但不知道如何繞過它。

EG

腳本

variablename=$1

sed 's/<\/sProblemDesc>/'"$variablename"'&/' create.xml

運行腳本

./script "variablename here" "etc etc" "etc"

XML是空白,如 <sProblemDesc></sProblemDesc>

另一示例

echo sed 's/<\/sActionDesc>/'"$variablename"'&/' create.xml

返回未示出可變名稱其只是空白再次

sed s/<\/sActionDesc>/&/ create.xml

乾杯wingZero

以下
+3

你能告訴你如何使用$ 1,$ 2? –

+0

我同意以前的說法。我試圖重新創建你的腳本,但它的工作方式與你的意圖一致(從我可以推斷出來的)。 – eddieantonio

+0

嗨,變量是variablename =「$ 1」 – wingZero

回答

0

我認爲有一些其他的問題,因爲這個簡單的測試工作:

$ cat test.sh 
#!/bin/bash 
myvar=$1 
echo "<sProblemDesc></sProblemDesc>" | sed 's/<\/sProblemDesc>/'"$myvar"'&/' 

$ ./test.sh "enter stuff here" 
<sProblemDesc>enter stuff here</sProblemDesc> 

什麼是你的腳本做多,這?

+0

嗨,歡迎您的評論,因爲它幫助我解決它!這是因爲事件之後的變量,就像你現在說的那樣工作!!非常歡呼:D! – wingZero

0

這個工作對我來說:

variablename=$1 
echo ${variablename} 

sed 's/<\/sProblemDesc>/'"${variablename}"'&/' create.xml 

例如隨着文件create.xml爲:

hello 
<sProblemDesc></sProblemDesc> 

此命令:

./example.sh "here and here" foobar 

生產:

here and here 
hello 
<sProblemDesc>here and here</sProblemDesc> 
+0

嗨,歡迎您的評論,因爲它幫助我解決它!這是因爲事件之後的變量,就像你現在說的那樣工作!!非常歡呼:D! – wingZero