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
Do I Have to Pthread_Join Each Thread I Create
Does Chrome 12 Really Support CSS 3D Transforms? Including on Linux
Expect Utility Is Not Working When Executing from Jenkins
Where Is The Linux Socket Implementation
Where Do You Send The Kernel Console on an Embedded System
Linux + Verify If File Is Text or Binary
Undelete The Deleted Command in Bash
Install Python 32 Bit on 64 Bit Linux
Docker Can't Write to Directory Mounted Using -V Unless It Has 777 Permissions
How to Play an Audio File from Haskell Code, Cross-Platform
Bash Cut Columns to One File and Save onto The End of Another File
Print Bash Script Result Behind Prompt in The Next Line
Sudo/Apt-Get Command Not Found in Git Bash
Where Is G_Multi Configured in Beaglebone Black