LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Sourcing a file in a script (https://www.linuxquestions.org/questions/linux-software-2/sourcing-a-file-in-a-script-4175480048/)

TenaciousRock 10-08-2013 12:43 PM

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

Habitual 10-08-2013 01:49 PM

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.

Firerat 10-08-2013 01:52 PM

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


TenaciousRock 10-08-2013 01:52 PM

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.

TenaciousRock 10-08-2013 01:56 PM

Quote:

Originally Posted by Firerat (Post 5042272)
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.

Firerat 10-08-2013 02:00 PM

Quote:

Originally Posted by TenaciousRock (Post 5042277)
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

TenaciousRock 10-08-2013 02:02 PM

wow.

Firerat 10-08-2013 03:55 PM

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

TenaciousRock 10-08-2013 07:06 PM

Yeah that is way over my head lol

thanks

Firerat 10-08-2013 07:47 PM

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


All times are GMT -5. The time now is 10:20 AM.