Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using exec and aliases in a ksh script 2

Status
Not open for further replies.

tg2003

IS-IT--Management
Feb 6, 2003
270
IL
Hello,

I have a ksh script that executes a text file:

...
exec file.txt

inside this file.txt, it defines variables and aliases:
...
export FILE=/bin/ls
alias ls='ls -l'

the problem is that after the 'exec' command is finished, the aliases are not well known to other sh commands.
where is the problem? How to keep the aliases variables to the rest of the commands of the script?

 
You should use . file.txt instead of exec file.txt to load the aliases into the currently running shell.

file.txt is a strange name to give to a shell script??

Annihilannic.
 
thanks.

I change to . instead of 'exec'.
But why the aliases are not familiar to the rest of the script?

(file.txt is an example)
 
When you exec another script you effectively replace the currently running shell process (let's call it foo.ksh) with the new one, i.e. ksh file.txt. When ksh file.txt completes it returns to the parent shell from which you launched foo.ksh rather than continuing to process the foo.ksh.

Even so, if you didn't use exec and simply had file.txt or ksh file.txt in foo.ksh, the aliases would not be inherited by foo.ksh when the child process ksh file.txt completes. They only live as long as the ksh process.

I'm not sure if I've made that clear... :-S

Annihilannic.
 
Hi,

Define your aliases with the switch -x if you want them to be exported.
Code:
alias -x ls='ls -l'
 
That would export them to the child shell, but not back to the parent.

Annihilannic.
 
And what about the ENV file ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Perahps some pictures will help.

Assume [tt]parent[/tt] is the parent process of your main script (let's call it main.sh). Your process tree looks like
Code:
(parent) -> (main.sh)

In the following diagrams, the red process is the one where the aliases get set.

[tt]Exec[/tt]ing it
If you exec your alias script script (let's call it alias.sh), your shell is completely replaced by that script.
Code:
(parent) -> [COLOR=red](alias.sh)[/color]
That script sets aliases, then stops running. The rest of main.sh never runs because it's gone.

Running it as a child process
If, instead, you run file.sh like a normal program, then main.sh spawns a child process:
Code:
(parent) -> (main.sh) -> [COLOR=red](alias.sh)[/color]
[tt]alias.sh[/tt] runs, sets up the aliases in the child process, then dies; all the aliases die with it. [tt]main.sh[/tt] continues running, with no aliases.

"Sourcing" it
Finally, if you use . (the dot operator) to "source" the alias.sh script, the [tt]main.sh[/tt] process will interpret the aliases itself:
Code:
(parent) -> [COLOR=red](main.sh)[/color]
After interpreting the aliases, [tt]main.sh[/tt] will continue, with aliases set in it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top