Find the answer to your Linux question:
Results 1 to 6 of 6
I am trying to write a bash script that sources another bash script. Essentially, I need a few lines to check to see if a certain variable is set. If ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    3

    Bash Script Help


    I am trying to write a bash script that sources another bash script. Essentially, I need a few lines to check to see if a certain variable is set. If not, I set it manually, and then source a scripts with that variable in the path. I wrote a test script to try it, but for some reason the last line does not work. Here is what I wrote:

    --------------------------------------------------------
    #!/bin/sh

    source ~setupdir/setup.shrc #just a test, this line works

    echo ${#SETUP} # prints 0 if setup is not set, which it isn't

    if [ ${#SETUP} -eq 0 ]
    then
    SETUP="~setupdir"
    fi

    echo $SETUP # prints ~setupdir

    echo $SETUP/setup.shrc #prints ~setupdir/setup.shrc

    source $SETUP/setup.shrc #this is the money line. It fails saying: "~setupdir/setup.shrc: No such file or directory"

    -----------------------------------------------------------------------------------------

    Can someone please help me. It seems to me that somehow the final line is not doing what I intend, since ~setupdir/setup.shrc exists and sourcing it on line 3 of the script is no problem.

    Thanks
    Last edited by Ulrich82; 01-29-2011 at 04:31 PM.

  2. #2
    Linux Engineer rcgreen's Avatar
    Join Date
    May 2006
    Location
    the hills
    Posts
    1,134
    echo $SETUP/setup.sh #prints ~setupdir/setup.sh

    source $SETUP/setup.sh #this is the money line. It fails saying: "~setupdir/setup.shrc: No such file or directory"
    Why does one line refer to setup.sh and the other to setup.shrc?

    Is the variable $SETUP or is it SETUP/setup.sh?

    or is there supposed to be yet another variable setup.sh?

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    3
    Sorry. It should all be .shrc. I'll edit it to fix.

    I believe the answer to your question is that the variable is SETUP. It should point to the path to find setup.shrc.

    Basically, I need to source the file setup.shrc to set up the libraries for the rest of my script. I am submitting this job to several computer farms. The farms should have the variable SETUP set to the correct path to find .shrc. So, I should be able to just call source $SETUP/setup.shrc in the first line of my code.

    However, the computer farms are not standardized as they should be, and some of them do not have the variable SETUP set. So I need to check to see if SETUP is set, and, if it is not, I need to manually set SETUP to the correct path ~setupdir.

  4. #4
    Linux Engineer rcgreen's Avatar
    Join Date
    May 2006
    Location
    the hills
    Posts
    1,134
    SETUP="~setupdir"
    remove the quotes around ~setupdir

    actually what I used was SETUP=~/setupdir

  5. #5
    Just Joined!
    Join Date
    Jan 2011
    Posts
    3
    Thanks. That seems to do it. I don't understand why the quotes caused a problem, but it is working now.

  6. #6
    Linux Engineer rcgreen's Avatar
    Join Date
    May 2006
    Location
    the hills
    Posts
    1,134
    With the quotes, the shell doesn't interpret ~ and /
    as path indicators but literally part of the file name.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •