1

I work with a system wherein we can use a shell script to produce output for a web interface. It works pretty well, but in the case of an issue today, I need to be able to insert a break tag where a newline stop would normally go. My script looks like this:

#!/bin/bash
users=`decl . -read /Groups/admin | grep GroupMembers | tr ' ' '\n'`
echo "<result>"$users"</result>"

I'm looking for a way for the \n to be a <br />, which does not work by just replacing it in the command. The script works, meaning it effectively grabs the data and displays it in our web interface, but the users are all on one line, which is not the desired result.

2 Answers 2

3

tr only replaces a single character with a single character (or with nothing, if using -d). To replace a single character with multiple characters, you need to use something else.

The option that's most similar to your current code is to use sed instead of tr:

#!/bin/bash
users=`decl . -read /Groups/admin | grep GroupMembers | sed 's# #<br />#g'`
echo "<result>"$users"</result>"
Sign up to request clarification or add additional context in comments.

4 Comments

Thank's for this as well. I haven't tried it yet, but if it works, it seems like a "more correct" way.
From the description, it sounds like sed 's#$#<br />#' would be more apropriate.
Good use of # to avoid having to escape the /.
@glennjackman: You may be right. To me, the OP sounded confident that his current script was correct aside from its producing \n rather than <br />, so I gave a script that made only that one change; but judging from his reply to sampson-chen's answer, it seems that this was not actually the change he needed.
1

Include $users into the quotes for your echo command. i.e. update your script to:

#!/bin/bash
users=`decl . -read /Groups/admin | grep GroupMembers | tr ' ' '\n'`
echo "<result>$users</result>"

And that should solve the "users are all on one line" problem

3 Comments

But it doesn't convert the newlines to HTML <br>.
No, but it produces the desired result.
@CarltonBrumbelow glad you got what you are looking for =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.