bash script confuse - Mandrake

This is a discussion on bash script confuse - Mandrake ; Am on Mandriva 2006, and wish to have a shell script to set environment variables. So, when I export them at the usr bash prompt, they appear when I query with set. However when I include those exports in a ...

+ Reply to Thread
Results 1 to 9 of 9

Thread: bash script confuse

  1. bash script confuse

    Am on Mandriva 2006, and wish to have a shell
    script to set environment variables.

    So, when I export them at the usr bash prompt,
    they appear when I query with set.
    However when I include those exports in a
    ..sh shell script, they do not appear with set.

    $ export TEST1=/home/usr/prog1
    $ set
    ...
    TEST1=/home/usr/prog1
    ...

    whereas, setup-prog1.sh

    #!bin/bash
    export TEST1=/home/usr/prog1

    or

    #!bin/bash
    TEST1=/home/usr/prog1
    export TEST1

    then,
    $ set
    -not visible-

    I do chmod ugo+x setup-prog1.sh before I
    run it.


    What am I doing wrong ? I seem unable to
    create a shell script to set and export
    some simple environment declarations.
    I am totally baffled by this.


    Just a couple of things:

    Should I include such declarations in
    ..bashrc or is this bad manners ?
    Should such trivial user tinkering be done
    in .bash_profile ?

    I got a fright when I included a line in .bashrc
    along the lines of "exec /../setup-prog1.sh".
    Suddenly no bash and no normal login or reboot.
    Struck with horror, I did a failsafe restart
    and poked around in sh-3.00# for a while.
    Then type $SHELL which got me to a bash prompt,
    and fortunately midnight commander mc would
    start from there. I found .bashrc and edited
    it back to its original - and thank god normal
    service was resumed. That's my big fright for
    the day.

    Any help re export above much appreciated.


  2. Re: bash script confuse


    I notice this below, describing .bashrc and .bash_profile,
    from http://gabriel.physics.ucsb.edu/~pcs...ntro-bash.html

    However it doesn't explain why my shell script as above
    doesn't export as I hoped.



    ~/.bash_profile
    The contents of this file are read and executed when you log into the
    system. Any changes you make to this file will not come into effect until
    you log out and log in again. It is usually a good idea to have this file
    execute your ~/.bashrc file as well. Just add the line "source ~/.bashrc"
    to the end of this file.. Beware that if you define your aliases here they
    will not be available in your subshells.
    ~/.bashrc
    The contents of this file are read and executed only when you start a new
    subshell. Add your aliases here.
    ~/.bash_logout
    This is executed when you log out. You may want to add some commands here
    to remove any temporary files or do any other tidying up.


  3. Re: bash script confuse

    Adam writes:

    > Am on Mandriva 2006, and wish to have a shell
    > script to set environment variables.
    >
    > So, when I export them at the usr bash prompt,
    > they appear when I query with set.
    > However when I include those exports in a
    > .sh shell script, they do not appear with set.


    To change variables in the session you've already
    started, you need to "source" the script.

    "source" is a bash builtin command, but
    it's rarely spelled "source" instead the
    "." is used instead, for example:

    .. ./myscript

    Try:

    man source

    > Should I include such declarations in
    > .bashrc or is this bad manners ?
    > Should such trivial user tinkering be done
    > in .bash_profile ?


    If you mean $HOME/.bash_profile or $HOME/.bashrc
    I can't see how that would be bad manners.
    If fact setting permanent variables is one of their
    purposes.

    > I got a fright when I included a line in .bashrc
    > along the lines of "exec /../setup-prog1.sh".


    Yeah, don't use exec in there.

  4. Re: bash script confuse

    Thanks Dan. That seems to have done it.

    Two dots mean another dir, but dot space dot
    had the script run.

    $. ./myprog.sh

    and a positive result with set showing the
    variables having been exported.

    I notice script headers as being any of;
    #!/bin/bash
    #!/bin/sh
    #! /bin/sh
    #! /bin/bash

    One more question if I may; if running such
    a script from another script, such as
    $HOME/.bash_profile running myscript.sh

    then myscript.sh is just entered into
    ..bash_profile as such, (no dot-space-dot
    required) ?
    I expect myscript.sh would need to be chmod'ed
    to be executable.










  5. Re: bash script confuse

    On Sun, 04 Feb 2007 15:43:40 +1300, Adam wrote:
    >
    > One more question if I may; if running such
    > a script from another script, such as
    > $HOME/.bash_profile running myscript.sh
    > then myscript.sh is just entered into
    > .bash_profile as such, (no dot-space-dot
    > required) ?
    > I expect myscript.sh would need to be chmod'ed
    > to be executable.


    Try it


    echo "aa=bb" > junk

    echo "$/HOME/junk" > junk1
    echo 'echo aa= $aa ' >> junk1
    echo 'source $/HOME/junk' >> junk1
    echo 'echo aa= $aa ' >> junk1

    chmod +x junk1
    ../junk1


  6. Re: bash script confuse

    > echo "aa=bb" > junk
    >
    > echo "$/HOME/junk" > junk1
    > echo 'echo aa= $aa ' >> junk1
    > echo 'source $/HOME/junk' >> junk1
    > echo 'echo aa= $aa ' >> junk1
    >
    > chmod +x junk1
    > ./junk1


    Is a little confusing - but I think I see what
    you're getting at. Assuming $/HOME is a typo,
    then, within a script;

    $HOME/junk
    returns an error, whereas

    source $HOME/junk
    indeed runs the script


    man echo
    doesn't explain if there's any difference
    between ' ' quotes or " " quotes. Perhaps
    there isn't any.

    man source
    refers to bash. And deep deep inside the
    bash man entry there is a section on source.

    [des]$ echo "aa=bb" > junk
    [des]$ echo "$HOME/junk" > junk1
    [des]$ echo 'echo aa=$aa' >> junk1
    [des]$ echo 'source $HOME/junk' >> junk1
    [des]$ echo 'echo aa=$aa '>> junk1
    [des]$ chmod +x junk1

    [des]$ ./junk1
    ../junk1: line 1: /home/des/junk: Permission denied
    aa=
    aa=bb

    [des]$ cat junk1
    /home/des/junk
    echo aa=$aa
    source $HOME/junk
    echo aa=$aa

    [des]$ cat $HOME/junk
    aa=bb

    Many thanks !



  7. Re: bash script confuse

    On Sun, 04 Feb 2007 16:59:45 +1300, Adam wrote:
    >
    > Is a little confusing - but I think I see what
    > you're getting at. Assuming $/HOME is a typo,


    Frap, yes.

    > then, within a script;
    >
    > $HOME/junk
    > returns an error, whereas


    That's the answer to your chmod question.

    > source $HOME/junk
    > indeed runs the script



    > man echo
    > doesn't explain if there's any difference
    > between ' ' quotes or " " quotes.


    Single/double quote question is a command line tidbit not an echo feature.

    Perhaps
    > there isn't any.


    Oh, you can be suprisied.

    >
    > Many thanks !
    >


    forgot to add

    For extra points there is
    http://tldp.org/LDP/abs/html/index.html


  8. Re: bash script confuse

    Adam writes:

    > One more question if I may; if running such
    > a script from another script, such as
    > $HOME/.bash_profile running myscript.sh
    >
    > then myscript.sh is just entered into
    > .bash_profile as such, (no dot-space-dot
    > required) ?


    The "." or "source" is required in both cases
    for the same reason.

    Shells are run in a sub-environment by default.

    You need to source something to affect the parent shell.

  9. Re: bash script confuse

    Its a big document, the abs-guide. And looks
    a definitive one.

    http://tldp.org/LDP/abs/

    Following on from Dan above, the abs-guide says
    sh
    can be used. Presently it seems these following
    are all valid;

    .. ./myscript (unpathed)
    source myscript (pathed)
    sh myscript (pathed)



+ Reply to Thread