LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-13-2008, 02:14 PM   #1
envisage
LQ Newbie
 
Registered: Jun 2007
Posts: 9

Rep: Reputation: 0
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,
 
Old 01-13-2008, 03:35 PM   #2
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
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.
 
Old 01-13-2008, 07:15 PM   #3
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,329

Rep: Reputation: 1099Reputation: 1099Reputation: 1099Reputation: 1099Reputation: 1099Reputation: 1099Reputation: 1099Reputation: 1099
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
save file from std input khairilthegreat Linux - Newbie 3 08-13-2007 02:04 AM
[Perl] cgi.pm - save input in .html file noir911 Programming 2 01-07-2007 02:36 PM
Hint: Wanna try Linux, so wanna rig your Windows system to dual-boot? sundialsvcs Linux - Newbie 2 08-16-2005 02:56 PM
Mozilla 1.7 - No File Save / File Open dialogs on Slackware 10.0 jayseye Slackware 8 08-10-2004 05:52 AM
i installed mplayer from source file and now i wanna delete it from my computer ... SlackwareMan Linux - Software 6 07-30-2004 07:40 AM


All times are GMT -5. The time now is 11:53 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration