LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-23-2011, 12:06 AM   #1
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
Linux - Ubuntu


how do you create a config file to define vairable to be used in a shell script? Where do you copy it to and how do you invoke it in a script?

For exapmple:

I wish do define a value for a backup directory ($backup)
I also wish to define a restore directory ($restore)
I want to use these from now on in a script I am writing to perform backups, how can this be done?

Can I write a script that:
Creates/modifies a config file with these values, stores, and then executes a backup using the new values?

Does anyone have an example of a simple config file or knwo where I would store it? also would I have to do a sudo first within the script?

Really new to all this, thanks guys for your help.
 
Old 10-23-2011, 12:38 AM   #2
zootboy
Member
 
Registered: Nov 2008
Location: In a dumpster, with my laptop.
Distribution: Fedora
Posts: 124

Rep: Reputation: 25
Well, if you'd like to keep it simple, just make some text files with the variables in them (one file per variable, the text of the file is the variable value). In your bash script, just use this:

variable=`cat /path/to/your/var/file`

Then you'll be able to reference $variable in your script.

By the way, using sudo is only necessary if the user that's running the script doesn't have permission to access the files. And to use sudo in a bash script, you'll need to do some configuring to allow a no-password sudo.
 
1 members found this post helpful.
Old 10-23-2011, 12:38 AM   #3
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
Hi Batistuta,

One (simple) way to accomplish this would be to have a file /etc/backup.conf that looked like:

Code:
TARGET=/some/path
SOURCE=/and/some/other/path
RSYNC_OPTS='-av'
You could then source that file from your shell script, e.g.

Code:
#!/bin/bash

source /etc/backup.conf
echo $TARGET
'source' is the same as the . operator in Bash, which is discussed here: http://www.gnu.org/s/bash/manual/bas...Shell-Builtins

Then, you could change the values in the script, and write them back out to the conf file, perhaps with a sed replace.

Code:
sed -i 's/TARGET=.*/TARGET=myval/' /etc/backup.conf
Again, fairly hands free.

Or, if you're conf was small, just write out a new one to replace it.

Last edited by jhwilliams; 10-23-2011 at 12:41 AM.
 
2 members found this post helpful.
Old 10-23-2011, 12:40 AM   #4
zootboy
Member
 
Registered: Nov 2008
Location: In a dumpster, with my laptop.
Distribution: Fedora
Posts: 124

Rep: Reputation: 25
Wow, upstaged in less than a minute. New record for me. I didn't know about the source command. Ya learn something new every day...
 
1 members found this post helpful.
Old 10-23-2011, 12:52 AM   #5
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
Hi zootboy,

Yea, looks like we were both responding at the same time. :-)

Jameson
 
1 members found this post helpful.
Old 10-23-2011, 04:40 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,254

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
@zootboy: also, in your example in post #2, all the variables in the cfg file would end up in one bash var; prob not what you want...
 
Old 10-23-2011, 05:44 PM   #7
zootboy
Member
 
Registered: Nov 2008
Location: In a dumpster, with my laptop.
Distribution: Fedora
Posts: 124

Rep: Reputation: 25
I did note that you could only put one var per file. The intention was that you would do that for every var you wanted.
 
Old 10-23-2011, 06:15 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,254

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
oh yeah; sorry; its very early here ...
Anyway, I'd definitely go with source (or '. ').

Last edited by chrism01; 10-23-2011 at 06:16 PM.
 
Old 10-23-2011, 06:43 PM   #9
zootboy
Member
 
Registered: Nov 2008
Location: In a dumpster, with my laptop.
Distribution: Fedora
Posts: 124

Rep: Reputation: 25
Definitely. Now that we've hijacked this thread, Batistuta, do you still have any questions?
 
Old 10-24-2011, 05:10 AM   #10
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Thanks guys exactly what I needed to know - legends
 
Old 10-24-2011, 09:25 AM   #11
zootboy
Member
 
Registered: Nov 2008
Location: In a dumpster, with my laptop.
Distribution: Fedora
Posts: 124

Rep: Reputation: 25
Glad we could help. Don't forget to set the thread to [SOLVED].
 
  


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
[UBUNTU]How to Covert Multiple Jpegs To one pdf file in UBUNTU/Linux papulia Linux - Newbie 6 12-02-2012 11:44 AM
Can Ubuntu One be used in Linux Mint and can Ubuntu music store be accessed from... linustalman Linux - General 2 12-28-2011 02:37 AM
cant install Linux multi media software on ubuntu hardy heron ubuntu 8.0.4 gnome Jazz44 Linux - General 3 09-15-2008 11:17 AM
LXer: Ubuntu Linux Is Dead, Long Live Ubuntu Linux LXer Syndicated Linux News 0 12-06-2007 12:41 PM
LXer: Ubuntu Multimedia Center - A new Ubuntu-based Linux distribution LXer Syndicated Linux News 0 11-21-2006 04:03 PM


All times are GMT -5. The time now is 06:17 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration