Getting error while including values in xml tags using shell script

Hi All,

Please find the code below where I want to add the variable value in between the XML tags. I am taking one string and my goal is to put them between the xml tags. Ex : in between <name> , <lname>

Kindly suggest a correction because while executing this script I am getting and output as : ${ary[0]} rather than the input variable.

str="rajneesh_kumar"
IFS=_
ary=($str)
for key in "${!ary[@]}"; do echo "$key ${ary[$key]}";
cat > "name.xml" <<- "EOF"
<?xml version="1.0" encoding ="UTF-8" standaalone="yes"?>
<rajneeshX xmlns="http://rajneesh.com">
<name> ${ary[0]}</name>
<lname> ${ary[1]}</lname>

</<rajneeshX >

Please use code tags...
Try these alterations.

cat > "name.xml" << EOF

</rajneeshX>

And finally end with...

EOF

That is all I can see at the moment...

Can you tell me the full code after modifying, still i am not able to get.

Well here you go...
(I also missed the "done" statement on my first post.)

#!/bin/bash
str="rajneesh_kumar"
IFS=_
ary=($str)
for key in "${!ary[@]}"; do echo "$key ${ary[$key]}";done
cat > "name.xml" << EOF
<?xml version="1.0" encoding ="UTF-8" standalone="yes"?>
<rajneeshX xmlns="http://rajneesh.com">
<name>${ary[0]}</name>
<lname>${ary[1]}</lname>
</rajneeshX>
EOF
cat name.xml

results on MBP, OSX 10.7.5, default bas terminal.

Last login: Tue Aug 16 11:35:33 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> chmod 755 junk.sh
AMIGA:barrywalker~/Desktop/Code/Shell> ./junk.sh
0 rajneesh
1 kumar
<?xml version="1.0" encoding ="UTF-8" standalone="yes"?>
<rajneeshX xmlns="http://rajneesh.com">
<name>rajneesh</name>
<lname>kumar</lname>
</rajneeshX>
AMIGA:barrywalker~/Desktop/Code/Shell> _
1 Like

Hi, Can you suggest me how i can convert array string into upper case?

That highly depends on the OS and shell versions that you are using. In recent shells (like bash and ksh ), you can do it immediately using "parameter expansion". If that's not the case, your OS may offer various tools like tr , sed , awk which can do this for you (with somewhat more effort / resource consumption).

Here, can you suggest me how i can convert into uppercase. The name i want to convert into upper case in the below code.

#!/bin/bash
str="rajneesh_kumar"
IFS=_
ary=($str)
for key in "${!ary[@]}"; do echo "$key ${ary[$key]}";done
cat > "name.xml" << EOF
<?xml version="1.0" encoding ="UTF-8" standalone="yes"?>
<rajneeshX xmlns="http://rajneesh.com">
<name>${ary[0]}</name>
<lname>${ary[1]}</lname>
</rajneeshX>
EOF
cat name.xml

Although you didn't bother to answer, it seems you are using bash . Try

${ary[0]^^}
1 Like

There is a plethora of info on this subject on this site...

Last login: Tue Aug 16 17:02:07 on ttys000
AMIGA:barrywalker~> str=$( echo "hello world" | tr '[:lower:]' '[:upper:]' )
AMIGA:barrywalker~> echo "$str"
HELLO WORLD
AMIGA:barrywalker~> _