LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 06-29-2011, 05:16 AM   #1
amritpalpathak
Member
 
Registered: Feb 2010
Posts: 116

Rep: Reputation: 15
Problem in making a script


hello everyone
Well i dont whether i am going to ask a right question or not.I am making a script for my project.In between there is need for settings of configuration files as per each user's needs.
For example:
users will edit the file to mention he/her own mobile number ,own moblie model ,own database etc etc.
How can i do it in the script?
I mean each user will do it itself or there is any way to do it automatically ?

Thanks in advance

amritpalpathakgne.wordpress.com
 
Old 06-29-2011, 05:47 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Which language are you using? In bash/ksh you can use the read statement to query user input. E.g.
Code:
read -p "Please enter model of your mobile" model
in this case the text entered by the user will be stored in the variable "model". Then you have to check the input and make the script to act accordingly. Usually this is done by means of the case/esac construct.
 
Old 06-29-2011, 06:14 AM   #3
amritpalpathak
Member
 
Registered: Feb 2010
Posts: 116

Original Poster
Rep: Reputation: 15
Thank you very much colucix sir.
Now i can easily accept the input from users but i have to edit the config file i.e I want that input given by the users will be replace by config file's entries at particuler line.
please tell me how to replace user's input with config file's particuler line's stuff?

i am using gedit text editor.

Again thanks

amritpalpathakgne.wordpress.com
 
Old 06-29-2011, 06:50 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Code:
echo $model > config.txt
echo $number >> config.txt
This will write the model and number to a file config.txt (change to your needs). The first line will overwrite an existing config.txt, the second line will append.

model and number are read according to post #2

Last edited by Wim Sturkenboom; 06-29-2011 at 06:51 AM.
 
Old 06-29-2011, 07:14 AM   #5
amritpalpathak
Member
 
Registered: Feb 2010
Posts: 116

Original Poster
Rep: Reputation: 15
Thank you sir
It is like it will overwrite 1 and then 2 then of config.tex file.But i have to replace it with particuler line number'contents (suppose line number 9).
Than what to do? how to mention that line number ?

Thanks again.
 
Old 06-29-2011, 08:01 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Have a look at sed to take the values you have been given and replace alternate values in a file.
 
Old 06-29-2011, 08:55 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
If you have a file format like this:

Code:
key: value
foo: bar
Then you can use sed to edit it, like this:

Code:
var="$(echo "$var" | sed -e 's@\\@\\\\@g' -e 's@/@\\/@g')" # escape special chars
sed -i 's/^key:.*/key: '"$var"'/' path/to/configfile

Last edited by MTK358; 06-29-2011 at 08:58 AM.
 
  


Reply



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
help on making a script pinoyskull Programming 1 10-09-2007 07:42 AM
making a script.. assasukasse Linux - Newbie 6 06-02-2006 03:17 AM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM
Need help in making script depam Linux - Software 1 10-19-2005 07:40 PM
Making a new script Lostboys Slackware 9 09-07-2004 06:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:14 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration