Getting error while including values in xml tags using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting error while including values in xml tags using shell script
# 1  
Old 08-16-2016
Question 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.


Code:
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 >


Last edited by rbatte1; 08-16-2016 at 01:16 PM.. Reason: Added ICODE tags
# 2  
Old 08-16-2016
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...

Last edited by wisecracker; 08-16-2016 at 07:50 AM.. Reason: Noticed a space in xml tag that was not visible in the original post.
# 3  
Old 08-16-2016
Can you tell me the full code after modifying, still i am not able to get.
# 4  
Old 08-16-2016
Well here you go...
(I also missed the "done" statement on my first post.)
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

results on MBP, OSX 10.7.5, default bas terminal.
Code:
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> _


Last edited by wisecracker; 08-16-2016 at 08:13 AM.. Reason: Rename standaalone to standalone...
This User Gave Thanks to wisecracker For This Post:
# 5  
Old 08-16-2016
Hi, Can you suggest me how i can convert array string into upper case?
# 6  
Old 08-16-2016
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).
# 7  
Old 08-16-2016
Here, can you suggest me how i can convert into uppercase. The name i want to convert into upper case in the below code.
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


Moderator's Comments:
Mod Comment Please use CODE (not ICODE) tags as required by forum rules!

Last edited by RudiC; 08-16-2016 at 01:09 PM.. Reason: Added CODE tags and reformatted code part.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read xml tags and then remove the tag using shell script

<Start> <Header> This is header section </Header> <Body> <Body_start> This is body section <a> <b> <c> <st>111</st> </c> <d> <st>blank</st> </d> </b> </a> </Body_start> <Body_section> This is body section (3 Replies)
Discussion started by: RJG
3 Replies

2. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

3. Shell Programming and Scripting

Passing values to an XML file from shell script

:wall: Hi, I have an XML file with 5 tags. I need to pass values to the XML file from a shell script that will replace values in 2 of the tags. I cannot hardcode the tag values in XML and use replace command in script as the values are likely to change. Please help !!!!!!!!!!! (2 Replies)
Discussion started by: Monalisaa
2 Replies

4. UNIX for Advanced & Expert Users

Shell Script to read XML tags and the data within that tag

Hi unix Gurus, I am really new to Unix Scripting. Please help me to create a shell script which reads the xml file and from that i need to fetch a particular information. For example <SOURCE BUSINESSNAME ="" DATABASETYPE ="Teradata" DBDNAME ="DWPROD3" DESCRIPTION ="" NAME... (2 Replies)
Discussion started by: SmilePlease
2 Replies

5. Shell Programming and Scripting

Shell Command to compare two xml lines while ignoring xml tags

I've got two different files and want to compare them. File 1 : HTML Code: <response ticketId="944" type="getQueryResults"><status>COMPLETE</status><description>Query results fetched successfully</description><recordSet totalCount="1" type="sms_records"><record... (1 Reply)
Discussion started by: Shaishav Shah
1 Replies

6. Shell Programming and Scripting

Shell script to extract data in repeating tags from xml

Hi, I am new to shell scripting. I need to extract data between repeating tags from an xml file and store the data in an array to process it further. <ns1:root xmlns:ns1="http://example.com/config"> <ns1:interface>in1</ns1:interface> <ns1:operation attribute1="true" attribute2="abd"... (2 Replies)
Discussion started by: sailendra
2 Replies

7. Shell Programming and Scripting

How to arrange xml tags in single row using shell script?

I want to put one xml record in one row and so on... sample two records are here. <?xml version="1.0"?> <Object> <Header> <XCOMVers>V1.0</XCOMVers> <REPORT>XXXXX</REPORT> <CODE>002</CODE> </Header> <IssueCard> <Record> <L>CAR SYSTEM -SSSSS -</L> ... (3 Replies)
Discussion started by: sene_geet
3 Replies

8. Shell Programming and Scripting

How to arrange xml tags in single row using shell script?

I want to put one xml record in one row and so on... sample two records are here. <?xml version="1.0"?> <Object> <Header> <XCOMVers>V1.0</XCOMVers> <REPORT>XXXXX</REPORT> <CODE>002</CODE> </Header> <IssueCard> <Record> <L>CAR SYSTEM -SSSSS -</L> ... (1 Reply)
Discussion started by: sene_geet
1 Replies

9. Shell Programming and Scripting

Replacing number between xml tags with ksh shell script

Hallo, im basically a complete noob on shell scripting and im trying to replace or rather add 1 to a number between xml tags. The xml basically has a tag somewhere that looks like this: <tag>12345678901234</tag> Now i want to replace the number between the tags. And i want the file to... (6 Replies)
Discussion started by: Demoric
6 Replies
Login or Register to Ask a Question