LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   how a activate aliases from a script ? (https://www.linuxquestions.org/questions/linux-server-73/how-a-activate-aliases-from-a-script-782821/)

markraem 01-17-2010 03:43 PM

how a activate aliases from a script ?
 
I wrote a small script :
#cat myscript.sh

NEWFILE=dojas.sh
touch $NEWFILE
rm $NEWFILE
touch $NEWFILE
echo "alias jan1='telnet 10.10.10.1 '" >> $NEWFILE
echo "alias jan2='telnet 10.10.10.2 '" >> $NEWFILE

chmod 777 $NEWFILE
./$NEWFILE
exit 0
#
#
==> in that script I create some alias commands an pipe them to a new shell script (called dojas.sh)

however after execution of that script , I do not see the aliases jan1 and jan2 in my terminal.

Even if I execute the dojas.sh srcipt manually , I do not see the aliases in the alias output of my terminal.

The only way I can make the aliases active is do the following :
cat dojas.sh
copy and paste the alias rules, and they are active in my session.

How can I make sure that I can activate aliases and they are active in my terminal from a script ?
Note : I am not interested in .bashrc file, I want them to be active in my current session.
Thanks in advance

GrapefruiTgirl 01-17-2010 03:49 PM

./$NEWFILE


Instead of the above, try sourcing the new file like this exactly:

Code:

. ./$NEWFILE
So notice it's a period, a space, then the "./filename"

This should "source" the file into your current shells environment, giving you the aliases. But, I'm not really sure at all if this will be different than the way you were doing it. Try and see..

Sasha

PS - also: sourcing the aliases from WITHIN that script, will possibly not make the aliases work for the shell OUTSIDE the script. You might need to source the $NEWFILE after the script exits.
Let us know how it works.

markraem 01-17-2010 04:39 PM

sourcing . ./$NEWFILE in the script does not work.

./$NEWFILE outside the script does not work (as allready mentioned)

. ./$NEWFILE outside the script ( so doing this manauly after the script has run) that WORKS.

If there is nobody that comes with another solution, then I will use that one.

chrism01 01-17-2010 08:00 PM

The pint is that env vars (inc aliases) can only exist at the shell level they were defined, & optionally be exported down to child sub-shells (& sub-sub-shells etc).
They cannot be 'exported' upwards ... unless you use the

. ./file

but even then, that just creates them at the 'current' level, not any higher...


All times are GMT -5. The time now is 12:26 AM.