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-03-2018, 09:54 AM   #1
blason
Member
 
Registered: Feb 2016
Posts: 122

Rep: Reputation: Disabled
Using global variables in multiple shell scripts, how to?


Hi There,

I have few multiple script which are pulling data from password protected http site. Unfortunately those have to get executed separately. Those script are around in number and I have created variable in every script for --http-user & --http-password; can I create global variable which can be then used in those multiple files so that I dont have to keep changing in those 8 scripts.

TIA
Blason R
 
Old 06-03-2018, 12:10 PM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
If the processes don't share a parent process then perhaps drawing the value from a configuration file might be the best idea.
 
Old 06-03-2018, 01:05 PM   #3
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
Sorry wrong post.

Last edited by blason; 06-03-2018 at 01:06 PM.
 
Old 06-03-2018, 01:13 PM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by blason View Post
Sorry wrong post.
???

Perhaps I don't understand your question correctly.

What I am saying is that if you have a variable which you have to declare in 8 separate scripts, and the variable changes frequently, then why don't you put that variable's value into a configuration file and get each script to read the value of the variable from the file. That way you only need to change the contents of the configuration file if the variable's value changes, not any of the 8 scripts.

Edit: I started constructing this reply while you were editing your previous post, and when I finally posted it, it appears to have replaced your original post which I quoted in my reply. An interesting feature, but it makes my post look stupid now.

Last edited by hydrurga; 06-03-2018 at 01:16 PM.
 
Old 06-03-2018, 01:46 PM   #5
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by hydrurga View Post
If the processes don't share a parent process then perhaps drawing the value from a configuration file might be the best idea.
Can you please elabore more on this? I mean I need I have 8 scripts which has a stanza something like this

USER=abcd
PPASS=qwert123

wget -t 3 --http-user $USER --http-password $PPASS https://xxxxxxx.xxxxx/cnf.tgz

SO I was wondering instead of entering password at eight times can we define global variable and call at those 8 scripts?
 
Old 06-03-2018, 01:59 PM   #6
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Search on the internet for bash read variable from file (I'm assuming that you're using Bash, although you haven't specified which scripting language you're using). You could consider sourcing the configuration file.

On the other hand, if all the processes are called by the same parent process, then you could look into creating environment variables in the parent process which are inherited by the child processes. Again, we don't know how you are calling your processes.
 
Old 06-03-2018, 02:01 PM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
you can define environment variables yes:
Code:
export PASSWD=supersecret
but you cannot define them from within those scripts; they would be lost again when the shell is closed.
so you have to define the variables at some earlier point.
e.g.:
  • in the login shell (on my system, i could do this via /etc/bash.bashrc or ~/.bash_profile, or /etc/profile)
  • in a parent script, that would in turn call all the other scripts.

PS: assuming bash.
 
Old 06-03-2018, 03:52 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
But $USER is set by the shell and should not be altered in the calling shell.
Better set the variables in a separate file "http-vars"
Code:
USER=abcd
PPASS=qwert123
And in each script source it with
Code:
. http-vars
Note: if the file contains a password then it makes sense to read-protect it for other users.
 
Old 06-03-2018, 08:53 PM   #9
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
You could put the variables in a file and use command substitution with cat. $(cat pass) will pass the output of the command.
 
  


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
Shell Scripts Variables Test Hi_This_is_Dev Linux - General 5 08-28-2010 08:21 PM
[SOLVED] Variables between shell scripts Potatos Linux - Newbie 1 05-20-2010 09:32 AM
global environment variables in shell script a.toraby Programming 2 11-11-2007 03:25 AM
about shell scripts global variables nesta Programming 2 01-05-2007 12:32 AM
Passing variables between shell scripts big_ginge21 Programming 3 09-26-2006 12:10 PM

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

All times are GMT -5. The time now is 12:14 AM.

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