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/)

TB0ne 03-06-2016 10:45 AM

Quote:

Originally Posted by postcd (Post 5511085)
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.

You've been handed:
  • A tip to look at xargs
  • A sample script which will work with a SMALL amount of thought and effort on your part
  • A tip to look at string replacement/eval command (with a sample script)
  • Links to bash scripting tutorials
...and you STILL can't/won't/aren't showing us anything YOU, personally, have done to make this happen. You STILL haven't shown us your sample input data, or what you're after for output data exactly...despite being asked for these things numerous times. This is the same behavior you show in most of your other threads.

This seems to dovetail nicely with your previous comment of "Im not looking to waste hours of time reading some technical data to get answer to my question." We WILL NOT write your script for you. Show effort of your own.

postcd 03-06-2016 10:53 AM

That script was not helpfull alas.

michaelk 03-06-2016 11:02 AM

Which script?
Did you look at the link I posted?

postcd 03-06-2016 11:08 AM

Quote:

Originally Posted by michaelk (Post 5511177)
Which script?
Did you look at the link I posted?

Yes, this seems related

stringZ=abcABC123ABCabc
echo ${stringZ//abc/xyz}

But not sure how i can use it in my case. I already tried it, but it do not replace phrasse "$ip" by IP. I used something like:
echo ${url//1.1.1.1/$ip}
and
echo ${url//$ip/1.1.1.1}
but no change in variable

pan64 03-06-2016 11:13 AM

Quote:

Originally Posted by postcd (Post 5511085)
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.

Looks like it is definitely difficult. In general it is something like "communication error". You do not understand the solution given and the others do not really understand what is your problem.
I have only one comment to this (see LQ rules)
"That script was not helpfull alas." is not an acceptable answer.
"your copy paste "solutions" that do not fits my case. That is why im asking for other people oppinion" is not an answer too.
If you want to improve the quality of this communication (and obviously it is you who need a solution) you need to give some information about: what was wrong with that? Otherwise others will not be able to help you, because they (even me) have no idea how to give a better tip...

postcd 03-06-2016 11:18 AM

pan64: i dont understand anything on one single script that was provided in this thread. Not sure how it can work in my case.

pan64 03-06-2016 11:26 AM

in that case ask how does this or that work, even line by line. Probably you will be able to modify it to fit your needs. But "repeating does not work" will not help

postcd 03-06-2016 11:31 AM

ok, then im asking for line by line explanation, if there is not any more simplistic way to achive what i want. i never thought it can be that difficult, wanted to learn some simple way.

TB0ne 03-06-2016 11:31 AM

Quote:

Originally Posted by postcd (Post 5511187)
pan64: i dont understand anything on one single script that was provided in this thread. Not sure how it can work in my case.

You do this by doing something you have flat out said you don't want to do: READ AND LEARN. Again, you STILL, despite being asked many times, do not provide sample input data, sample output data, or most importantly ANYTHING THAT YOU, PERSONALLY HAVE ACTUALLY DONE OR TRIED. You are expecting people to hand you a script that 'meets your case', without YOU having to do anything. How many times, over the past three years, have you done exactly this?

Sorry, the scripts provided WILL work, if you put any effort into them. If you actually posted something like "I modified the script in post #xxx, and gave it this input, but got this result, and I'm confused", you'd get help. Saying "it doesn't work" tells us nothing.

And looking at the link michaelk gave you (after suggesting you look at it, they also had to look up a link for you), the sample script tells you exactly what you need to do...but you didn't do it. You're not following the syntax that's very clearly laid out in that document. And whether you like it or not, the FIRST REPLY (post #2), will work perfectly for you. It not only shows you how to read a file into an array, but how to process that array into variables (which is EXACTLY WHAT YOU WANT), then perform operations on that array...such as print them out, or do whatever you want with them.

postcd 03-06-2016 11:33 AM

post reported & ignored, please be ontopic.

TB0ne 03-06-2016 11:44 AM

Quote:

Originally Posted by postcd (Post 5511197)
post reported & ignored, please be ontopic.

It was on topic, and as you've been told before, report whatever you want. AGAIN:
  • You are asked for sample input data. You have not provided it.
  • You are asked for sample output data. You have not provided it.
  • You are asked for examples of what YOU, PERSONALLY have done. You have not provided it
  • You say NOTHING about the problems you're having with the script(s)/example(s)
  • You keep asking for something to 'meet your case', yet show no effort of your own
NONE of these things are in keeping with LQ Rules or Question Guidelines, period.
Quote:

Originally Posted by postcd
ok, then im asking for line by line explanation, if there is not any more simplistic way to achive what i want. i never thought it can be that difficult, wanted to learn some simple way.

It isn't difficult...would take me about 5 minutes to write such a script, and any others that have replied to you could do it in about the same time. That is because instead of whining about "im newbie" (after three years), they actually learned and read the scripting tutorials, wrote scripts, and LEARNED. Asking for "line by line explanation" is the first sign of effort you have actually shown in any of your posts.
Code:

saveIFS="$IFS"  IFS defines what separates the fields. In this case, a newline. Replace with whatever is your separator
IFS=$'\n'
array=($(<file)) Open your input file, and read it all into the array. This would assume you have a file with foo and bar in it
IFS="$saveIFS"  Only needed if something in the loop depends on IFS
echo ${array[0]}    Array element 1 (starts at zero), output: EXAMPLEfoo
echo ${array[1]}    Array element 2 (second after separator) output: EXAMPLEbar
for i in "${array[@]}"; do echo "$i"; done  iterate over the array, until you're done reading the input file

So instead of echoing $i back out, put it into your output string wherever you want.

AGAIN...try reading the bash scripting tutorials.

postcd 03-06-2016 11:51 AM

post not read and ignored as it do not provide any helpfull solution

pan64 03-06-2016 12:35 PM

what do you think, what will be really helpful - for you?

postcd 03-06-2016 12:40 PM

you do not know what is helpfull when you ask the question?
an answer that can help you solve the issue.
prefferably example with comment on how that works. are you that stupid not to understand this. I hope i explained enough. And we can stop offtopic and time waste in this thread. If anyone of you do not wish to provide what i said, please be so kind and give me a favor to stay away from commenting in my topics and wasting everyones time.

pan64 03-06-2016 12:50 PM

I'm afraid here, at LQ everyone is so stupid, they cannot understand you. Thanks for your valuable input.


All times are GMT -5. The time now is 07:47 PM.