Scope of exported function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scope of exported function
# 8  
Old 02-14-2011
This thread seems to be using the term "exported functions" incorrectly. When ksh runs a shell script it first forks a copy of itself. This copy will try to invoke the exec system call on the script. If the script has a valid "#!" line the exec will work. If we are running a ksh script, this means that a fresh copy of ksh is brought into core and it runs the script. But if the exec fails, the original forked ksh process runs the script itself. But first it "purifies" itself. It unsets non-exported variables and non-exported functions. This means that an exported function continues to be available as the subsidiary script is run.

I think exported functions are a bad idea. I never use them except just to experiment. I think that any script should always have a "#!" line and this undermines exported functions. This thread is actually discussing autoloaded functions. I don't like them either. I tried them and ran into the issues being discussed. I could find no satifactory solution. So now I just write plain old shell scripts. I use functions a lot and I always just copy the function into any script that needs it. Going further, I try to have the function set its own PATH and any other environment variables that it needs. This way each script is self sufficient and does not require an intricate environment in which to run.

So I don't have any solutions, but now you can curse the correct feature. Smilie
# 9  
Old 02-14-2011
Thanks but....

I need this function to effect a change of directory in the current shell.

If I use a script I will be back where I started when it exits the function.

By loading the script in the current shell it is available for the life of the session.

Thanks anyway for the clarification. Smilie
# 10  
Old 02-14-2011
Or you could launch an interactive subshell. Tools like vi and sudo do that. And it's the way I do stuff like this. When I am finished, a simple cntl-D restores the session I had before. You will either need to code an undo function or require the user to logout and back in to restore the user's original session.
# 11  
Old 02-17-2011
I think the answer is

I think the way to go with the package is have it update /etc/profile as this runs for everyone. I can put a check in there for membership of the group and dot in the files if needed.

I guess I can do this using the postinst script.

I will need to be able to remove the lines on an uninstall though so was thinking of adding a unique comment like the package name to each line so it can be deleted with sed or awk.

If anyone can think why this would be a bad idea the feedback would be welcome.

Cheers
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Loading associative array from exported function

Hello. I have an export of an associative array build using declare -p SOME_ARRAY_NAME > SOME_FILE_NAME.txt. Producing some thing like declare -A SOME_ARRAY_NAME=( ="some_text" ="a_text" ......... ="another_text" ) in a text file. I have a stock of functions which are sourced from... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Beginners Questions & Answers

Sudo has no access to exported bash function

Hello. I am running leap 15.1 sudo behavior may differ from other distrib. I have a file with functions definition .... function_1 { echo "Hello world" } export -f function_1 This file is sourced by both /etc/bash.bashrc.local and profile.local. So the functions are... (3 Replies)
Discussion started by: jcdole
3 Replies

3. Shell Programming and Scripting

BASH: variable and function scope and subscripts

Hi, I'm a Delphi developer new to linux, new to this forums and new to BASH programming and got a new task in my work: maintaining an existing set of BASH scripts. First thing I want to do is making the code more reliable as in my opinion it's really bad written. So here's the quest: I'm... (6 Replies)
Discussion started by: rse
6 Replies

4. Shell Programming and Scripting

Use of exported variable

i have to use the exported variable from one script into another script ex : A.ksh # !/bin/ksh chk1=56 export chk1 B.ksh # !/bin/ksh echo $chk1 i have executed the... (6 Replies)
Discussion started by: urfrnddpk
6 Replies

5. AIX

exported filesystems on a NIM server

I have a filesystem that was exported sometime in the past, probably by NIM processes, but cannot be unexported now. How can I get rid of this? Any ideas? I tried rebooting and it survived it. hostname:/:$ exportfs -v /usr/lpp/bos.sysmgt/nim/methods... (2 Replies)
Discussion started by: kah00na
2 Replies

6. Shell Programming and Scripting

using the exported variable after su

Dear All,How can use a variable which I have exported when I am logged into one user to be used once I su to another user.something like 1.Login to Unix box as user12. export var1="TEST"3. su - user24. User the var1 value ( it should return TEST)I have checked just export does not work. any other... (1 Reply)
Discussion started by: rahulkav
1 Replies

7. Shell Programming and Scripting

Seeing variable which are exported with export

Greeting to all of you! I've small issue related to the variable which we are setting and exporting through scripts, in one of the script there are some variable used but I am not abel to get the detail as where they are set. I tried finding the detail with the help of env but no luck. ... (2 Replies)
Discussion started by: kumarmani
2 Replies

8. Solaris

Remove the exported zpool

I had a pool which was exported and due to some issues on my SAN i was never able to import it again. Can anyone tell me how can i destroy the exported pool to free up the LUN. I tried to create a new pool on the same pool but it gives me following error # zpool create emcpool4 emcpower0c... (0 Replies)
Discussion started by: fugitive
0 Replies

9. Linux

Can USB Devices Be Exported Over a Network?

While I know that it's possible to use something like SANE to share a USB scanner over a network, or use NBD or iSCSI to share a USB flash or external HD over the network, I've been wondering about a raw USB <--> TCP/IP transport. Back in the late 90s, I swear I remember hearing about a project... (2 Replies)
Discussion started by: deckard
2 Replies

10. Shell Programming and Scripting

$0 scope in function and calling script

Hi folks, I'm just running through an oreilly korn shell book but have witnessed an output difference on my own unix machine and the output provided in the book. Can anyone help? create a script called ascript as follows: function afunc { print in function $0: $1 $2 var1="in... (16 Replies)
Discussion started by: beckett
16 Replies
Login or Register to Ask a Question