LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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-26-2011, 06:51 AM   #1
mtm100
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Rep: Reputation: Disabled
Bash variables


Hi,

I'm very new to linux and am currently creating a very simple file backup script in bash and I am using Ubuntu.

I am calling backup.sh script from within the terminal using sudo bash backup.sh.

I have a configuration file separate from backup.sh (for ease of upgrade etc) , that contains variables e.g. email address to send logs to, location of log file, location of backup files etc. Both backup.sh and the config file will reside in the same folder.

How do I make the variables available to backup.sh – easiest way possible – as you guess by my question I'm a novice!

Thanks
 
Old 10-26-2011, 07:23 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
It called sourcing .. you can just use a dot(.) followed by a space then followed by the path to the file.

Code:
#!/bin/bash

# import config
. /etc/myconfig.conf

...
 
Old 10-26-2011, 09:52 AM   #3
mtm100
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks kbp, saved me many tears, hours and swear words, thanks again.
 
Old 10-30-2011, 03:49 PM   #4
mtm100
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Similar question to the original, I have written a couple of functions that I am having to repeat across a couple of scripts (within the same back-up application) - is there a similar method for creating like a global (or sourcing file) where I can call them from?
Thanks
 
Old 10-30-2011, 03:53 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by mtm100 View Post
Similar question to the original, I have written a couple of functions that I am having to repeat across a couple of scripts (within the same back-up application) - is there a similar method for creating like a global (or sourcing file) where I can call them from?
Thanks
As far as I know, you could use exactly the same method, ie. sourcing:

Code:
. /path/to/file/with/global/functions
 
Old 10-31-2011, 10:27 AM   #6
mtm100
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Great, looks to be working by calling by:
. Configuration/myfunctions.sh
and then calling the function without containing the brackets e.g.
Logging_Events_Errors1

Thanks for your help
 
  


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
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
Bash Variables SlipDigby Programming 2 09-01-2008 05:00 PM
variables in bash haddad Linux - General 6 09-22-2004 05:29 PM
Variables in bash tcaptain Programming 1 03-03-2003 02:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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