Re: Why 'alias' cannot run by shell script?

From: Dana French (dfrench_at_mtxia.com)
Date: 09/08/04

  • Next message: Dana French: "Re: changing string occurencies in a line up to a delimiter"
    Date: 8 Sep 2004 12:41:06 -0700
    
    

    wilsonidv@hotmail.com (wilson) wrote in message news:<5dbdd79a.0409080202.6c7f4b5c@posting.google.com>...
    > Hello all,
    >
    > I wrote a shell script below:
    >
    > #!/bin/bash
    > ### MyScript ###
    >
    > alias m='mount /mnt/floppy'
    > alias r='reboot'
    >
    > After editing, I added the 'x' permission and it can be executed
    > correctly after I input ./MyScript.sh
    > But why the script was of no effect? I input 'm' or 'r', the error
    > message 'command not found' occurred.
    >
    > Please correct me where I miswrote, thanks.

    When you run a script it is normally invoked in a subshell, meaning
    the variables, functions, shell options, and ALIASES you define in the
    script are not seen by the parent shell ( the shell from which you
    executed the script ). You can execute your script as a "dot" script
    to cause it to be executed in the current shell as follows:

      . ./MyScript.sh

    Thats dot-space-dot-slash-MyScript.sh
    However it is more likely you are attempting to set these aliases in
    your login environment so that they are always available. If so, then
    you will need to create a .bashrc file, insert your aliases, shell
    variables, shell options, functions, and anything else you want to be
    defined every time you start a new shell. Then in your .bash_profile
    define the following environment variable:

    BASH_ENV="${HOME}/.bashrc"
    export BASH_ENV

    Then logout and back in again. Now your aliases should be defined in
    your login shell as well as all subshells.

    --------------------------------------------------------
    Dana French dfrench@mtxia.com
    Mt Xia Technical Consulting Group http://www.mtxia.com
    100% Spam Free Email http://www.ridmail.com
    MicroEmacs http://uemacs.tripod.com
    Korn Shell Web http://dfrench.tripod.com/kshweb.html


  • Next message: Dana French: "Re: changing string occurencies in a line up to a delimiter"

    Relevant Pages

    • Re: shell scripting
      ... and am assuming an sh-compatible shell here.... ... command 1 options arguments ... To run the script, do: ... then you need to use the full path to execute it: ...
      (comp.unix.questions)
    • Re: shell scripting
      ... and am assuming an sh-compatible shell here.... ... command 1 options arguments ... To run the script, do: ... then you need to use the full path to execute it: ...
      (comp.unix.shell)
    • Re: Script Header
      ... > On a shell script, ... > You can TRY to execute a garbage file. ... > filename: Exec format error. ... - The shell will check whether this file has the executable flag set, ...
      (comp.unix.shell)
    • Re: [opensuse] alias failure
      ... So it's just an accident that the following aliases all work as I ... It's uncommon for interactive shells to have any positional parameters ... Unlike the very limited capabilities of aliases, shell procedures ... I made a script with nothing but that in it, ...
      (SuSE)
    • Re: newbie question: cant get script to execute
      ... I saved this in a script "process.bsh". ... The typical name for a shell script it .sh, and this is not a shell ... > I chmod to 777 and still get "cannot execute". ... the error message is distinct and far ...
      (comp.lang.perl.misc)