How does the Linux shell call another script file

  
                

Many Linux users don't know that the shell can not only edit the script, but also call another script file on the script, including the php file. How should it be called? The following small series will introduce you to the Linux shell to call another script file, friends who do not call the script can learn.

script first (Test Example 1)

code below

#! /bin/bash

echo ‘your are in first file’

Q) Calling another script file in the current script file?

Method one: Use source

script second (test example 2)

#! /bin/bash

echo ‘your are in second file’

source first

Method 2: Use .

Script second (test example 3)

#! /bin/bash

echo ‘your are in second file’

. First

source filename and . Filename should be the same thing, both in the *current *Shell environment to execute the script. You can also use sh filename, which is to execute the script in the subshell of the current shell.

You can use the following two scripts to understand the difference between the three calling methods:

1.sh

#! /bin/bash

A=B

echo “PID for 1.sh before exec/source/fork:$$”

export A

echo “1.sh: $A is $A”

case $1 in

exec)

echo “using exec…”

exec. /2.sh ;;

source)

echo “using source…”

. . /2.sh ;;

*)

echo “using fork by default…”

. /2.sh ;;

esac

echo “PID for 1.sh after exec/source/fork:$$”

echo “1.sh : $A is $A”

2.sh

#! /bin/bash

echo “PID for 2.sh: $$”

echo “2.sh get $A=$A from 1.sh”

A=C

export A

echo “2.sh: $A is $A”

Implementation:

$ . /1.sh

PID for 1.sh before exec/source/fork:5845364

1.sh: $A is B

using fork by default…

PID for 2.sh: 5242940

2.sh get $A=B from 1.sh

2.sh: $A is C

PID For 1.sh after exec/source/fork:5845364

1.sh: $A is B

$ . /1.sh exec

PID for 1.sh before exec/source/fork:5562668

1.sh: $A is B

using exec…

PID for 2.sh: 5562668

2.sh get $A=B from 1.sh

2.sh: $A is C

$ . /1.sh source

PID for 1.sh before exec/source/fork:5156894

1.sh: $A is B

using source…

PID for 2.sh: 5156894

2.sh get $A=B from 1.sh

2.sh: $A is C

PID for 1.sh after exec/source/fork:5156894

1.sh: $A is C

$

The above is the introduction of the Linux shell call script file. When you are calling a php file, you don't have to use php, you can also use shell commands.

Copyright © Windows knowledge All Rights Reserved