LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Calling a script from within rc.local does not work. (https://www.linuxquestions.org/questions/linux-newbie-8/calling-a-script-from-within-rc-local-does-not-work-452676/)

oferu 06-08-2006 02:14 AM

Calling a script from within rc.local does not work.
 
Hi,
I have a simple script names 'aliases' under /home/oferu/:

#!/bin/sh
alias cdforms='cd /usr/local/forms'
alias cdhome='cd /home/oferu'

In rc.local, on the last line I have
source /home/oferu/aliases

But after I boot, the aliases do not exist.
Why?

Thanks.

timmeke 06-08-2006 02:41 AM

I'm guessing that rc.local, like any scripts in /etc/rc.d (or /etc/init.d for that matter) only get executed when the runlevel changes. So, yes, the script probably does run and sources your aliases stuff.
However, the aliases settings are not retained to your login shell probably.
So, when you start a shell, you will not inherit them either.

To fix this, put the "source ..." command not in /etc/rc.local but rather in /home/you/.profile, /home/you/.<your_shell>rc (ie /home/you/.bashrc) if they are only to be applied to your user. If you want to apply the aliases for all users, put them in /etc/profile or /etc/<some_shell>rc (ie /etc/bashrc).

spirit receiver 06-08-2006 02:42 AM

If you want the user oferu to be able to use those aliases, you should probably place the commands in /home/oferu/.profile, /home/oferu/.bash_profile or /home/oferu/.alias, depending on your configuration. There's also a file for system-wide settings like these (probably /etc/profile).

Edit: Well, at least the answer I gave looks similar to that of timmeke:D

dimsh 06-08-2006 02:43 AM

hi,

I would do it like this:

1st: remove the first line from "/home/oferu/aliases" which is "#!/bin/sh"
2nd: instead of "source /home/oferu/aliases" use ". /home/oferu/aliases" (dot space filename), this is the include statement for bash.

it should work this way.

edit: NOTE: source command will execute the command line given to it and exit (like exec command), any commands below the source command in your rc.local will not be executed.

Regards.

zhangmaike 06-08-2006 03:30 AM

Quote:

1st: remove the first line from "/home/oferu/aliases" which is "#!/bin/sh"
Any line beginning with a # will be ignored, even if the file is being sourced. The presence of this line shouldn't be a problem.

Quote:

source command will execute the command line given to it and exit (like exec command), any commands below the source command in your rc.local will not be executed.
This is not so. An excerpt from the bash texinfo manual:

Code:

`.    (a period)'
          . FILENAME [ARGUMENTS]
    Read and execute commands from the FILENAME argument in the
    current shell context.  If FILENAME does not contain a slash, the
    `PATH' variable is used to find FILENAME.  When Bash is not in
    POSIX mode, the current directory is searched if FILENAME is not
    found in `$PATH'.  If any ARGUMENTS are supplied, they become the
    positional parameters when FILENAME is executed.  Otherwise the
    positional parameters are unchanged.  The return status is the
    exit status of the last command executed, or zero if no commands
    are executed.  If FILENAME is not found, or cannot be read, the
    return status is non-zero.  This builtin is equivalent to `source'.

The source (or .) command will not prevent the rest of your script from executing.

As timmeke and spirit receiver have noted, aliases set from within a startup shell script (or any shell script, for that matter) will not be inherited by the login shell.

Simply moving the source (or .) line into /etc/profile or ~/.profile will solve your problem.

oferu 06-08-2006 04:57 AM

Thank You all for your quick replies.

worzel68 06-08-2006 07:24 AM

home directory
 
To change quickly to your home directory from anywhere you can do this:

cd ~

A small thing I know, but it could be worthwhile!:D

timmeke 06-08-2006 10:24 AM

@worzel68:
Code:

cd
is even simpler.

worzel68 06-09-2006 12:56 AM

Excellent!! ha ha ha ha

Maybe we should explain to oferu that one can use the ~ to expand to one's home directory like this:

instead of;

cd /home/dave/kdevelop

or

cd
cd kdevelop

I could do this

cd ~/kdevelop

also one can join commands together with a semi-colon like this:

cd; ls -l

or just do this

ls -l ~

there are many ways of doing stuff

oferu 06-12-2006 02:37 AM

New and simple
 
Nice to learn new and simple little commands that make navigation easier.
Thanks all.


All times are GMT -5. The time now is 08:44 PM.