LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 10-08-2013, 12:43 PM   #1
TenaciousRock
LQ Newbie
 
Registered: Sep 2013
Posts: 7

Rep: Reputation: Disabled
Sourcing a file in a script


Hi all,

So I have a short bash script written with the goal of being able to download a zip folder containing credentials for a software program, unzip them and source them. Unfortunately the sourcing is not taking place, or if it is, it only lasts during the execution of the script and not outside it. Problem is I need it to be effective outside of the script. So, the script looks like so:

get-creds.sh

#!/bin/bash

yum install unzip -y
mkdir .creds
cd .creds
/usr/sbin/euca_conf --get-credentials admin.zip
unzip admin.zip
source eucarc
cd

I execute it from the terminal like so: ./folder/folder/folder/get-creds.sh

how can I set up the script so that that source command works outside the script as well? The credentials file needs to be sourced to run some commands and I want it as automated as possible.

Thanks
 
Old 10-08-2013, 01:49 PM   #2
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Undecided
Posts: 3,156
Blog Entries: 4

Rep: Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760
Do the creds change from download to download?
If not, just leave eucarc somewhere (one directory above?) and use source /path/to/eucarc
in your ~/.bashrc file.
 
Old 10-08-2013, 01:52 PM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,414

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
basically, you can't

once the bash script exits, its environment is 'lost'

However..

instead of using a script, add a function

in your ~/.bashrc
Code:
get-creds () {
yum install unzip -y
mkdir .creds
pushd .creds
    /usr/sbin/euca_conf --get-credentials admin.zip
    unzip admin.zip
    source eucarc
popd
}
source your ~/.bashrc ( new shell is automatic )
then type

get-creds

Warning:
I didn't fix your script
  • yum will fail if not root
  • yum may run needlessly
  • .creds is created in current dir


check if you already have unzip
Code:
[[ -x $(which unzip) ]] || sudo yum install unzip -y
consider a tempdir ( does .creds need to hang around? )
Code:
TempDir=$(mktemp -d)
pushd $TempDir
   .. do stuff
popd
rm -r $TempDir # cleanup
unset $TempDir # cleanup

Last edited by Firerat; 10-08-2013 at 01:53 PM. Reason: fix list
 
Old 10-08-2013, 01:52 PM   #4
TenaciousRock
LQ Newbie
 
Registered: Sep 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Yes the creds will change from download to download.

The script will be run as the initial install, users may need to source the cred file again at a later point manually if they exit the terminal/close the remote session or whatever. Or if for some reason the programs database needs overwritten new credentials will have to be downloaded and sourced. The path however will not change, unless the user messes up and puts it somewhere else in the scenario that they need to be redownloaded.
 
Old 10-08-2013, 01:56 PM   #5
TenaciousRock
LQ Newbie
 
Registered: Sep 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
basically, you can't

once the bash script exits, its environment is 'lost'

However..

instead of using a script, add a function

in your ~/.bashrc
Code:
get-creds () {
yum install unzip -y
mkdir .creds
pushd .creds
    /usr/sbin/euca_conf --get-credentials admin.zip
    unzip admin.zip
    source eucarc
popd
}
source your ~/.bashrc ( new shell is automatic )
then type

get-creds

Warning:
I didn't fix your script
  • yum will fail if not root
  • yum may run needlessly
  • .creds is created in current dir


check if you already have unzip
Code:
[[ -x $(which unzip) ]] || sudo yum install unzip -y
consider a tempdir ( does .creds need to hang around? )
Code:
TempDir=$(mktemp -d)
pushd $TempDir
   .. do stuff
popd
rm -r $TempDir # cleanup
unset $TempDir # cleanup
I'm not sure that will work for my scenario. These scripts are supposed to be part of an install suite that will make the installation process of some bundled software more simple for the people we distribute it to...unless I create a script that modifies or overwrites the bashrc with a version matching what I'd need it to look like. Also, yes the .creds directory needs to stick around permanently.
 
Old 10-08-2013, 02:00 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,414

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
Quote:
Originally Posted by TenaciousRock View Post
I'm not sure that will work for my scenario. These scripts are supposed to be part of an install suite that will make the installation process of some bundled software more simple for the people we distribute it to...unless I create a script that modifies or overwrites the bashrc with a version matching what I'd need it to look like. Also, yes the .creds directory needs to stick around permanently.
use your imagination
 
Old 10-08-2013, 02:02 PM   #7
TenaciousRock
LQ Newbie
 
Registered: Sep 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
wow.
 
Old 10-08-2013, 03:55 PM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,414

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
I shall use my imagination then

Code:
#!/bin/bash

BaseCamp=$HOME/.YourCompany
CredDir=${BaseCamp}/creds
SourceFile=${BaseCamp}/.SomeNamerc

[[ ! -d "$BaseCamp" ]] && mkdir "$BaseCamp"
[[ -x $(which unzip) ]] || \
    (
       echo "unzip utility required , please install"
       echo "yum install unzip -y"
       # be more verbose with your msg
       exit 1
    ) 
[[ ! -d "$CredDir" ]] && mkdir "$CredDir"

[[ ! -e "$SourceFile" ]] && \
    (
       cat > $SourceFile <<-END
	get-creds () {
	pushd $HOME/.YourCompany/creds
	    /usr/sbin/euca_conf --get-credentials admin.zip
	    unzip -qq -o admin.zip
	    source eucarc
	popd # echo some message
	}
	[[ -e $HOME/.YourCompany/creds/eucarc ]] \
	    && source $HOME/.YourCompany/creds/eucarc \
	    || get-creds
	END
    ) ######
      # http://mywiki.wooledge.org/BashGuide/InputAndOutput
      # Heredocs And Herestrings, I used tabs..
      ####

grep -q "$SourceFile" $HOME/.bashrc || \
    (
      echo "source $SourceFile" >> $HOME/.bashrc
      echo "some message about source file install"
      echo "message on use ( how to call the function(s) )"
    )
# end install script
# an example, you know the requirements better than i do
Warning:
basic script, no real error checks


http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashGuide
http://www.gnu.org/software/bash/manual/bashref.html
 
Old 10-08-2013, 07:06 PM   #9
TenaciousRock
LQ Newbie
 
Registered: Sep 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Yeah that is way over my head lol

thanks
 
Old 10-08-2013, 07:47 PM   #10
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,414

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
If you want to make life easy for your clients you will need to learn some scripting


looking back I would change the cat > $SourceFile <<-END

Code:
       cat > $SourceFile <<-END
	[[ -e $HOME/.YourCompany/creds/eucarc ]] \
	    || (
	          mkdir $HOME/.YourCompany/creds 2>/dev/null
	          pushd $HOME/.YourCompany/creds
	              /usr/sbin/euca_conf --get-credentials admin.zip
	              unzip -qq -o admin.zip
	          popd # echo some message
	       )
	source $HOME/.YourCompany/creds/eucarc
	END
but as well as needing to be more robust ( check for errors )
it should also include an uninstall function, to cleanup the users ~/.bashrc and so on
 
  


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
Sourcing Environment File in Shell Script linux098 Linux - Newbie 6 10-22-2012 02:38 AM
Sourcing the script after new software install?? P_Titty Linux - Newbie 1 12-29-2010 08:45 PM
Difference between executing & sourcing a script muazfarooqaslam Linux - Newbie 1 01-04-2008 02:51 AM
sourcing a file bdiddy84 Linux - Newbie 1 04-17-2007 09:07 PM
sourcing a python config file? shanenin Programming 8 08-10-2005 02:19 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration