Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi everyone, I am very new to using sed and Bash scripting, and created this file to practise:
Code:
#!/bin/bash
readarray -d "|" myarray < listOfURLs
for i in "${myarray[@]}"
do
#echo "{$i}"
sed -i "s@URL@$$i@g" HTMLTemplate > filledInHTMLTemplate
done
The purpose of the code is to read a bunch of URLs from listOfURLs, and replace the text URL in HTMLTemplate with one of the URLs ( a different one each time, iterating through the whole array).
I am not sure if the double dollar in $$i has a purpose or is a typo.
Why did you comment out the echo? Most likely something in the $i (or $$i) causes sed's hiccup, and the echo is a good way to figure this out. Perhaps there is an "@" in the variable?
By the way, you probably intended echo "${i}", though echo "{$i}" shouldn't cause any harm.
Another oddity, though probably not related to the error message, is the usage of sed's -i option. Its effect is to modify the HTMLTemplate file and to output nothing. This is probably not your intention.
Last edited by berndbausch; 03-24-2020 at 09:41 PM.
Reason: wording
First, you are misusing the "-i" option, which is telling sed to modify the HTMLTemplate file in place rather than sending a modified version to stdout. It's probably good that it didn't work.
To answer your specific question, the most likely cause is that $i is expanding to a string that contains one or more "@" characters. It's difficult to make the sed 's' command work reliably with arbitrary strings expanded into the command. Frequently, I use the ctrl-G (ASCII BEL, or '\007') as a delimiter in such situations, but to be completely general you might have to pre-scan the string and find some character that is not present there. How to insert such nonprinting characters into your script depends on which editor you are using.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.