How to Run Sh File from Another Sh File

How to run sh file from another sh file

Take a look at this. If you want to run a script you can use:

./yourscript.sh

If your script has a loop, use:

./yourscript.sh&

If you want to get the console after starting scripts and you don't want to see it's output use:

./yourscript.sh > /dev/null 2>&1 &

So, in the master file you'll have:

./yourscript1.sh > /dev/null 2>&1 &
./yourscript2.sh > /dev/null 2>&1 &
./yourscript3.sh > /dev/null 2>&1 &

They will start together.

How to include file in a bash shell script

Simply put inside your script :

source FILE

Or

. FILE # POSIX compliant
$ LANG=C help source
source: source filename [arguments]
Execute commands from a file in the current shell.

Read and execute commands from FILENAME in the current shell. The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.

Exit Status:
Returns the status of the last command executed in FILENAME; fails if
FILENAME cannot be read.

Can I call a function of a shell script from another shell script?

Refactor your second.sh script like this:

func1 {
fun="$1"
book="$2"
printf "func=%s,book=%s\n" "$fun" "$book"
}

func2 {
fun2="$1"
book2="$2"
printf "func2=%s,book2=%s\n" "$fun2" "$book2"
}

And then call these functions from script first.sh like this:

source ./second.sh
func1 love horror
func2 ball mystery

OUTPUT:

func=love,book=horror
func2=ball,book2=mystery

How best to include other scripts?

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"

Trying to export a bash variable from an .sh file to another file that runs it

Variables are exported from a parent to a child, not vice versa. script_2.sh is called in a different shell whose environment doesn't propagate back to the parent shell.

Source the script (using the .) to call it in the same shell. You then don't even need to export the value.

. ./scripts/script_2.sh

How to call a shell script and pass argument from another shell script

By sourcing the second script with . /home/admin/script2.sh, you're effectively including it in the first script, so you get the command line arguments to the original script in $@. If you really want to call the other script with arguments, then do

/home/admin/script2.sh "$ARG1" "$ARG2" "$ARG3"

(make sure it's executable).



Related Topics



Leave a reply



Submit