Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How best to include other scripts?

Tags:

bash

The way you would normally include a script is with "source"

eg:

main.sh:

#!/bin/bash  source incl.sh  echo "The main script" 

incl.sh:

echo "The included script" 

The output of executing "./main.sh" is:

The included script The main script 

... Now, if you attempt to execute that shell script from another location, it can't find the include unless it's in your path.

What's a good way to ensure that your script can find the include script, especially if for instance, the script needs to be portable?

like image 525
Aaron H. Avatar asked Oct 10 '08 17:10

Aaron H.


People also ask

Where should I put custom scripts?

If your scripts are intended to run by a single user you can place them in ~/bin. If your scripts are system-wide you can probably place them in /usr/local/bin. If your scripts are meant to be only used under special circumstances or environments you can put them even in /opt/myscripts and add this directory to $PATH.

How do I run two scripts at the same time?

The command to do this is CTRL-B :setw synchronize-panes on. Now you can run scriptName $fruit and view the output on all panes at the same time.


1 Answers

I tend to make my scripts all be relative to one another. That way I can use dirname:

#!/bin/sh  my_dir="$(dirname "$0")"  "$my_dir/other_script.sh" 
like image 179
Chris Boran Avatar answered Nov 02 '22 03:11

Chris Boran