开发者

Trying to pass bash variable with spaces

开发者 https://www.devze.com 2023-03-31 20:01 出处:网络
Can anyone help with this query. Currently attempting to pass a variable into xml using sed. This works fine with just the text, however its not picking up the variable within bash such as $1, $2 etc

Can anyone help with this query. Currently attempting to pass a variable into xml using sed. This works fine with just the text, however its not picking up the variable within bash such as $1, $2 etc which will need to be inputted by the user.

EG:

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

successful output <sProblemDesc>anew test</sProblemDesc>

However when inserting a $1 variable between the anew test part, its either bringing back the literal name of the variable and not the data.

The user will be running the script as such

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

So I need to know how to get it to work with the variable but also take into consideration that I am having to use quotes to separate the variables..(if there is another way to do this let me know :D! )

Any queries let me know cheers

Update: As requested the variable name is just variablename="$1"

I have tried removing the quotes as well just having =$1 does not seem to work with that either, so I am guessing its because when running the script (./script "stuff here" "etc" "etc" I am using quotes there but not 开发者_C百科sure how to get round it.

EG

Script

variablename=$1

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

running the script

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

XML is blank such as <sProblemDesc></sProblemDesc>

another example

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

returns the following which is not showing the variable name its just blank

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

Cheers again

wingZero


I think there is some other issue, because this simple test works:

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

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

What does your script do more that this?


This works for me:

variablename=$1
echo ${variablename}

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

e.g. With the file create.xml as:

hello
<sProblemDesc></sProblemDesc>

this command:

./example.sh "here and here" foobar

produces:

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

精彩评论

暂无评论...
验证码 换一张
取 消