Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent direct bash script execution and allow only usage from other script?

Tags:

bash

I have one script with common functions that is included in other my scripts with:

. ~/bin/fns

Since my ~/bin path is on the PATH, is there a way to prevent users to execute fns from command line (by returning from the script with a message), but to allow other scripts to include this file?

(Bash >= 4)

like image 315
igr Avatar asked Apr 09 '14 12:04

igr


People also ask

How do I make sure only one instance of a Bash script runs?

Just add the pidof line at the top of your Bash script, and you'll be sure that only one instance of your script can be running at a time.

How do I stop a Bash script execution?

If you are executing a Bash script in your terminal and need to stop it before it exits on its own, you can use the Ctrl + C combination on your keyboard.

How do you stop a shell script from output?

Silencing Output To silence the output of a command, we redirect either stdout or stderr — or both — to /dev/null. To select which stream to redirect, we need to provide the FD number to the redirection operator.


1 Answers

Just remove the executable bit with chmod -x . ~/bin/fns. It will still work when sourced, but you can't call it (accidentally) by its name anymore.

like image 78
Axel Avatar answered Sep 24 '22 17:09

Axel