LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to replace in an bash script variable? (https://www.linuxquestions.org/questions/linux-general-1/how-to-replace-in-an-bash-script-variable-4175574038/)

postcd 03-05-2016 09:42 AM

How to replace in an bash script variable?
 
This topic is a waste of time, no solution to the question.

Hello,

im having urlfile and each url in it contains $ip variable which i need to replace by actual IP that is actual for the present "while loop" cycle:

Code:

while read ip;do

while read url;do
curl -s "$url" >> output
done < urlfile

done < ipfile

# cat urlfile
http://domain1.com/?something&?ipp=$ip
http://domain2.com/?ipaddr=$ip&something=something
http://domain2.com/?a=lldd?ipaddr=$ip#something

But i dont know what is the clever/low resource usage/low amount of code way to replace that $ip variable inside the URLs that i have either in urlfile or in variable that i populate somehow out of urlfile? Thanks in advance


----

Update: Solution for my case:

Quote:

while read ip;do
cp /mypath/urlfile /mypath/urlfile2
# do replacing in the temporary urlfile2 so the urls reflect current IP
find /mypath -name 'urlfile' -type f -print0 | xargs -0 sed -i "s|IPHERE|$ip|g"
while read webpage;do
# my command here
done < /mypath/urlfile2
done < /mypath/ipfile
rm -f /mypath/urlfile2
(it may need tweaking as i needed to rewrite it for this example purpose, it basically duplicate original urlfile with IP placeholders IPHERE and do replacing in it (sed need to use double quotation marks " to work with $ip variable))

TB0ne 03-05-2016 11:03 AM

Quote:

Originally Posted by postcd (Post 5510635)
Hello,
im having urlfile and each url in it contains $ip variable which i need to replace by actual IP that is actual for the present "while loop" cycle:
Code:

while read ip;do

while read url;do
curl -s "$url" >> output
done < urlfile

done < ipfile

# cat urlfile
http://domain1.com/?something&?ipp=$ip
http://domain2.com/?ipaddr=$ip&something=something
http://domain2.com/?a=lldd?ipaddr=$ip#something

But i dont know what is the clever/low resource usage/low amount of code way to replace that $ip variable inside the URLs that i have either in urlfile or in variable that i populate somehow out of urlfile? Thanks in advance

What have you tried to do this? Have you checked ANY of the bash scripting tutorials? Setting variables is very well documented. You can use either xargs, or any of the 'standard' methods of assigning a variable.

A brief Google search turns up lots. From just ONE is this sample:
Code:

saveIFS="$IFS"
IFS=$'\n'
array=($(<file))
IFS="$saveIFS"
echo ${array[0]}    # output: EXAMPLEfoo
echo ${array[1]}    # output: EXAMPLEbar
for i in "${array[@]}"; do echo "$i"; done    # iterate over the array

Did you try to do research of your own?

postcd 03-05-2016 11:09 AM

yes, please anyone else how to do it please?

TB0ne 03-05-2016 12:35 PM

Quote:

Originally Posted by postcd (Post 5510671)
yes, please anyone else how to do it please?

So again, you're handed a solution but ignore it, don't post any of your own efforts, and wonder why people don't help you more??

postcd 03-05-2016 01:40 PM

Sorry, im newbie i do not understand your copy paste "solutions" that do not fits my case. That is why im asking for other people oppinion. I ask you to stop answering in my topics. If you do not provide solutions and instead you will be bashing offtopic & wasting my time and other readers time, i will just report your post.

michaelk 03-05-2016 02:03 PM

Did you post the entire urlfile? And is it exactly as shown? Does the IP file and url file correspond one for one? Or as in your loop do you apply each IP to every URL?

postcd 03-05-2016 02:23 PM

Quote:

Originally Posted by michaelk (Post 5510730)
Did you post the entire urlfile? And is it exactly as shown? Does the IP file and url file correspond one for one? Or as in your loop do you apply each IP to every URL?

Thx, no, i didnt posted real urlfile as i wanted the URLs to stay private.

So far i have 3 URLs in urlfile and there are parts of that URLs, parts where should be IP assigned..:
...=$ip
...=$ip&...
...=$ip&...

michaelk 03-05-2016 03:14 PM

And the other questions?

Does the IP file and url file correspond one for one? Or as in your loop do you apply each IP to every URL?

postcd 03-05-2016 03:24 PM

Quote:

Originally Posted by michaelk (Post 5510761)
in your loop do you apply each IP to every URL?

simply said, i have 3 URLs to process, and for each URL i need to use all IPs in my list.

So for each IP i will process all 3 URLs, and i need to replace the $ip variable in them by actual IP (actual for that "while loop" i mentioned in my initial post). Is it clearer please?

TB0ne 03-05-2016 03:54 PM

Quote:

Originally Posted by postcd (Post 5510721)
Sorry, im newbie i do not understand your copy paste "solutions" that do not fits my case.

First, stop playing the "im newbie" card...you've been here THREE YEARS. Secondly, I *DID* provide you with a solution that did exactly what you were after. The 'problem' with it, is that it required YOU to actually think and learn something. It was up to YOU to modify it to fit your needs.

That, is how you stop being a 'newbie' and start to be able to do something for yourself.
Quote:

That is why im asking for other people oppinion. I ask you to stop answering in my topics. If you do not provide solutions and instead you will be bashing offtopic & wasting my time and other readers time, i will just report your post.
Too bad...this is a public forum. ANYONE can respond...if you don't like it, then post somewhere else. And don't bother reporting my post (which was not only on topic, but gave you a solution)...I did it for you, since you are AGAIN ignoring the question guidelines and LQ Rules.
Quote:

Originally Posted by postcd
Thx, no, i didnt posted real urlfile as i wanted the URLs to stay private.

..which makes NO SENSE at all, since you could easily replace any URL's with samples...same with addresses.
Quote:

simply said, i have 3 URLs to process, and for each URL i need to use all IPs in my list.

So for each IP i will process all 3 URLs, and i need to replace the $ip variable in them by actual IP (actual for that "while loop" i mentioned in my initial post). Is it clearer please?
Your post was clear to start with. What you STILL haven't provided is examples of your input data, what you're looking for as output data, and ANY EFFORT ON YOUR PART TO ACTUALLY PERFORM THIS TASK. Again, what have YOU written/done/tried on your own???

Since you're a 'newbie' (who has been here 3 years), you probably haven't read the question guidelines. Quoting the relevant parts here, bolded parts are mine:
Quote:

Originally Posted by Question Guidelines from LQ
Here are a couple tips that will enable us to help you moving forward:
  • When asking a question, be sure to provide as many relevant details as possible. You should include the distribution and version you're using, along with hardware details, application version and exact error messages where applicable. ..this is where you post your sample data
  • If you're actively troubleshooting an issue you should also include any steps you've already taken. ..this is where you post what YOU have written
  • Before posting, have you used the search function to ensure your question hasn't been asked before? ...this is where, if you had tried, you would have found THOUSANDS of bash looping examples, which you COULD have modified on your own
If you are unwilling or unable to ask questions in a manner that allows us to help you, it's unlikely our community will be able to provide you a solution. Unfortunately, serial offenders who show wanton disregard for this request after multiple pointers may be asked to seek help elsewhere.

And given your nasty private messages to me, which contain gems like:
Quote:

Originally Posted by postcd Private Messages
  • Im not looking to waste hours of time reading some technical data to get answer to my question.
  • if you see im looking for solution, please do not fill my thread repeatedly with something im not interested in. It just wasting my precious time and we already been reading from you that you want me/us read the readme so i assume it is a waste of time also for other future readers.
  • you seems to be too stupid to understand that threads are created in aim to get solution not to be isntructed to read the README
  • Im not interested to learn from you if that is advice of this kind: "read software developer website"

Again, if you want to LEARN, this is the place. If you want someone to hand you an exact command, write your scripts for you, etc., then HIRE SOMEONE.

michaelk 03-05-2016 08:14 PM

I think string expansion by using the eval command should work but there are many ways.
I can not provide examples at the moment

TB0ne 03-05-2016 09:12 PM

Quote:

Originally Posted by michaelk (Post 5510869)
I think string expansion by using the eval command should work but there are many ways. I can not provide examples at the moment

Good luck. The OP only want a pre-written script and isn't interested in learning or doing for themselves, if their previous posts are any indication. This thread is a good example.

In the first reply, I gave the OP something that would work, but they flat out said they didn't want to modify anything, and wanted a solution to 'fit their case'

TobiSGD 03-06-2016 05:43 AM

Things you do in a public forum:
- Be kind to each other
- Be on topic
- When you don't understand an answer ask for clarification, in the same way: If the question is not clear enough ask for clarification
- Use all the options the forum software provides you. This includes the Ignore List function.

Things you never do on a public forum (and even real life):
- releasing private messages into public without explicit permission from all involved parties. There is a reason they are called private messages. If you feel you are harassed, insulted, ..., in private messages contact a mod or Jeremy.

postcd 03-06-2016 07:15 AM

Please anyone else know the solution for this (see initial mesaage)? I cant believe it can be so difficult to come with simple, clever solution.

michaelk 03-06-2016 08:21 AM

Check out string replacement

http://www.tldp.org/LDP/abs/html/str...ipulation.html


All times are GMT -5. The time now is 09:52 AM.