ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, I wonder if there is anyway to make a user-defined bash shell function global, meaning the function can be use in any bash shell scripts, interactively or not.
This is what I attempted:
Code:
$ tail -n 3 /etc/bashrc
echotm () {
echo "[`date`] $@"
}
$ echotm hello world
[Mon Sep 13 17:15:28 HKT 2010] hello world
$ cat a.sh
#!/bin/sh
echotm hello world
$ sh a.sh
a.sh: line 2: echotm: command not found
wait wait...
There is an undesired side-effect of sourcing the file.
As you can see, the command 'echotm' displays timestamp when echo'ing. It is widely used in writing logs in my systems. However, as a habit whether good or bad, I have an 'exit' statement in all the scripts. The exit statement down in some sub-script (script called by another) terminates the entire script in the first level, rendering the whole thing broken.
Code:
$ cat lv1.sh
#!/bin/sh
echo lv 1 in
sh lv2.sh
echo lv 1 out
exit 0
$ cat lv2.sh
#!/bin/sh
echo lv 2 in
echo lv 2 out
exit 0
$ sh lv1.sh
lv 1 in
lv 2 in
lv 2 out
lv 1 out
$ source lv1.sh
lv 1 in
lv 2 in
lv 2 out
Of course, replacing 'echo' with 'echotm' won't work in the first case. But then, rear part of lv1.sh is never executed in the second case.
My last resort is to make 'echotm' a executable shell script file in /usr/local/bin.
It is accessible from the shell and from within scripts.
You can also make a dedicated file that you can source that holds all the functions you need, there shouldn't be a hash-bang or exit in that file. You can decide to source this file by hand when you need it or source this file from for example .bashrc so that all the functions are accessible in all shells.
$ cat lv1.sh
#!/bin/sh
echo lv 1 in
sh lv2.sh
echo lv 1 out
exit 0
$ cat lv2.sh
#!/bin/sh
echo lv 2 in
echo lv 2 out
exit 0
$ sh lv1.sh
lv 1 in
lv 2 in
lv 2 out
lv 1 out
$ source lv1.sh
lv 1 in
lv 2 in
lv 2 out
The output's quite odd. Before you called 'source lv1.sh', did you also replaced 'sh lv2.sh' in lv1.sh with 'source lv2.sh' ?
Anyway, the solution I think would be to just call the subscript inside a subshell:
a.sh
Code:
#!/bin/sh
echofunc() {
echo "echofunc: $1"
}
echofunc "a 0"
( . ./b.sh; )
echofunc "a 1"
exit 0
Functions can also be exported in the same way variables can (at least in bash). Just use the -f flag.
export -f functionname
This will make it available to any script run within that shell.
Overall though, I think it's best simply to incorporate everything directly into your script in some way. The more external dependencies you have, the more difficult it will be to keep track of it all in the long run.
Functions can also be exported in the same way variables can (at least in bash). Just use the -f flag.
export -f functionname
This will make it available to any script run within that shell.
Only that he needed something that will resolve with the exit builtin. Also, the return builtin might have also been a better alternative (wrapped inside a function).
Quote:
Overall though, I think it's best simply to incorporate everything directly into your script in some way. The more external dependencies you have, the more difficult it will be to keep track of it all in the long run.
Well there may still be some coders that will instead prefer not to place all codes in one script.. especially when the size of the code is large. Dependencies may be made less of a problem if you leave them independent and stable. It may also be easy to track them if they are organized, and that the names and codes are written.. uniformly.
I somehow remember something similar with this conversation. It was quite an odd event of the past .
I did the lv1-lv2-test one machine, and then re-type the output in LQ on another machine, which is why.
----
why odd?
Method 1:
~/.bashrc:
Code:
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
echo2 () {
echo hello world
}
export -f echo2
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.