LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 11-16-2016, 08:08 PM   #1
TashiDuks
Member
 
Registered: Sep 2010
Posts: 54

Rep: Reputation: 0
Update value in configuration file using shell script


Hi Everyone,

I have an question on how to update value in configuration file using shell scripting.
I have following configuration file (info.conf):

Code:
"device.node_id": "server01",
"ftp.banner": "FTP server ready",
"ftp.enabled": false,
"ftp.port": 21
Now using the script I need to change the values.

Please someone suggest me.

Thanks
 
Old 11-16-2016, 09:44 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,573
Blog Entries: 11

Rep: Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588
Welcome to LQ!

There are many choices for modifying character strings in a file, including sed, awk and shell built-in methods.

But you have not supplied enough information for anyone to offer much help.

Where will the new values come from?
How is the script intended to be used?
Should it change one value or multiple values?

Please try to rephrase your question in a more complete way, giving an example of how you want your script to work if possible.

See the Site FAQ for guidelines for asking more effective questions.
 
Old 11-17-2016, 02:52 AM   #3
TashiDuks
Member
 
Registered: Sep 2010
Posts: 54

Original Poster
Rep: Reputation: 0
Hi astrogeek,

Thanks for the information. Well to share further about my initial post.

I have info.conf file which is hidden and the info.conf contains following information:

Code:
"device.node_id": "server01",
"ftp.banner": "FTP server ready",
"ftp.enabled": false,
"ftp.port": 21
Now I want to have a shell script which will update the value of "device.node_id": to specified value. So to achieve that i have done the following script which works fine with only config file which is marked as executable.
Code:
#!/bin/bash

#Edit note name with double quotation e.g. "webserver01".
newNode="server211"

#Set OpenCanary Configuration File Path
openCanaryConfig="info.conf"
sed -i 's/\(^"device.node_id":\).*/\1 "'$newNode'",/' $openCanaryConfig
After running the above script my info.conf file changes to:
Code:
"device.node_id": "server211",
"ftp.banner": "FTP server ready",
"ftp.enabled": false,
"ftp.port": 21
But how to do with configuration file which is marked as executable (like info.conf*)

Info: when i run ll in terminal, i see * being marked at the end of info.conf file.

Can you please suggest, hope i have missed a option.

Thanks

Last edited by TashiDuks; 11-17-2016 at 03:14 AM.
 
Old 11-17-2016, 06:44 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,813

Rep: Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071
The permissions are not really an issue unless you did not have access to the file. Having it executable will make no difference to your script. The asterisk you see when using ls -l is simply there
to identify the file as executable but is no part of the file name. To confirm try the following in the same directory:
Code:
\ls -l
 
Old 11-18-2016, 04:19 AM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,573
Blog Entries: 11

Rep: Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588
As grail points out, the * appears only as an indicaator in file listings and does not affect the file name or your ability to update it.

If your script fails to update the file, please post here the output of ls -l for the config file, and tell us the user name running the script. It is probably due to the user not having permission to update the file.
 
Old 12-02-2016, 02:12 AM   #6
TashiDuks
Member
 
Registered: Sep 2010
Posts: 54

Original Poster
Rep: Reputation: 0
Hi,

I finally managed to solve this issue. I used following parameter for my 'sed':
Code:
sed -i 's/\("device.node_id":\).*/\1 "'$newNode'",/' $openCanaryConfig
Thanks
 
  


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
A shell script for svn update unclesamcrazy Linux - Newbie 2 01-11-2014 07:55 AM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 10:48 PM
Shell script to update IP kidestranged Programming 4 02-02-2007 07:57 AM
shell script for reading of configuration files nhydra Programming 2 06-13-2006 03:15 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:53 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