LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   wanna to save the input to a file (https://www.linuxquestions.org/questions/programming-9/wanna-to-save-the-input-to-a-file-613316/)

envisage 01-13-2008 02:14 PM

wanna to save the input to a file
 
am currently working with shell scripting in Linux and this is what.. I wanna to achieve..

I have a file named "a" having some parameters in it. for ex: color= red and boys = 10.
Now, I wanna to run a script, so that It will ask the values of color and boys from me to input,.
I will then enter color-green and boys=12.

Then it will update those values into the file "a".

So that, when I will open the file :"a".. I will find the update values of color and boys as green and 12 respectively.

As per my understanding, we need to locate the address of variable color and boys that were stored the values as red and 10 respectively earlier.
Then we need to replace those values as green and 12 to that memory location.

Kindly suggest me, if this will work or you have different opinion.
Thanks for your time ... and sorry to distrub you... Hope you don't mind..

Thanks,

zaichik 01-13-2008 03:35 PM

You haven't said what shell, but here's an idea in bash:

Code:

#!/bin/sh


echo -n "Enter color: "
read new_color
echo -n "Enter number of boys: "
read new_boys

sed -i s/color=.*/color=$new_color/ lq.txt
sed -i s/boys=.*/boys=$new_boys/ lq.txt

Code:

root@beren [~/scripts/shell]# cat lq.txt
color=red
boys=10
root@beren [~/scripts/shell]# ./lq.sh
Enter color: blue
Enter number of boys: 12
root@beren [~/scripts/shell]# cat lq.txt
color=blue
boys=12
root@beren [~/scripts/shell]#

Far and away not perfect; consider a different output file:
Code:

root@beren [~/scripts/shell]# cat lq.txt
bgcolor=white
color=red
girls_not_boys=15
boys=10
root@beren [~/scripts/shell]# ./lq.sh
Enter color: blue
Enter number of boys: 12
root@beren [~/scripts/shell]# cat lq.txt
bgcolor=blue
color=blue
girls_not_boys=12
boys=12
root@beren [~/scripts/shell]#

But it's a start. Based on the contents of the output file, you will need to adjust the regular expressions you use with sed.

Hope that helps.

sundialsvcs 01-13-2008 07:15 PM

Well, it might also be even simpler than that.

"Every Linux/Unix program" has the notion that it reads its input from STDIN, writes its output to STDOUT, and sends its error-messages to STDERR. All three of these names are abstract names: you can define, on the fly, what these input-files belong to.

By default, STDIN will be "your terminal" and both STDOUT and STDERR will also be "your terminal."

Now, let's say that this time when I run my program, I want to read the input from one file, write the output to another, and simply dispose-of any errors.
Code:

myprogram <inputfile >outputfile 2>/dev/null
("/dev/null" is a pseudo-device that consumes anything written to it. The "bit-bucket...")

What if we'd like to pipe the output of one program straight into the mouth of another one? Easy:
Code:

foo | bar
"Get to know the Unix shell(s)." The time will be well-spent.
Quote:

Originally Posted by Dorothy
I don't think we're in Redmond anymore, Toto...



All times are GMT -5. The time now is 12:30 AM.