Can I call a function of a shell script from anoth

2019-03-08 03:04发布

I have 2 shell scripts.

The second shell script contains following functions second.sh

func1 
func2

The first.sh will call the second shell script with some parameters and will call func1 and func2 with some other parameters specific to that function.

Here is the example of what I am talking about

second.sh

val1=`echo $1`
val2=`echo $2`

function func1 {

fun=`echo $1`
book=`echo $2`

}

function func2 {

fun2=`echo $1`
book2=`echo $2`


}

first.sh

second.sh cricket football

func1 love horror
func2 ball mystery

How can I achieve it?

标签: shell unix
5条回答
干净又极端
2楼-- · 2019-03-08 03:25

You can't directly call a function in another shell script.

You can move your function definitions into a separate file and then load them into your script using the . command, like this:

. /path/to/functions.sh

This will interpret functions.sh as if it's content were actually present in your file at this point. This is a common mechanism for implementing shared libraries of shell functions.

查看更多
Emotional °昔
3楼-- · 2019-03-08 03:26

The problem

The currenly accepted answer works only under important condition. Given /foo/bar/first.sh

function func1 {  
   echo "Hello $1"
}

and /foo/bar/second.sh

#!/bin/bash

source ./second.sh
func1 World

this works only if the first.sh is executed from within the same directory where the first.sh is located. Ie. if the current working path of shell is /foo, the attempt to run command

cd /foo
./bar/first.sh

prints error:

/foo/bar/first.sh: line 4: func1: command not found

That's because the source ./second.sh is relative to current working path, not the path of the script. Hence one solution might be to utilize subshell and run

(cd /foo/bar; ./first.sh)

More generic solution

Given /foo/bar/first.sh

function func1 {  
   echo "Hello $1"
}

and /foo/bar/second.sh

#!/bin/bash

source $(dirname "$0")/second.sh

func1 World

then

cd /foo
./bar/first.sh

prints

Hello World

How it works

  • $0 returns relative or absolute path to the executed script
  • dirname returns relative path to directory, where the $0 script exists
  • $( dirname "$0" ) the dirname "$0" command returns relative path to directory of executed script, which is then used as argument for source command
  • /second.sh just appends the name of imported shell script
  • source loads content of specified file into current shell
查看更多
闹够了就滚
4楼-- · 2019-03-08 03:26

If you define

    #!/bin/bash
        fun1(){
          echo "Fun1 from file1 $1"
        }
fun1 Hello
. file2 
fun1 Hello
exit 0

in file1(chmod 750 file1) and file2

   fun1(){
      echo "Fun1 from file2 $1"
    }
    fun2(){
      echo "Fun1 from file1 $1"
    }

and run ./file2 you'll get Fun1 from file1 Hello Fun1 from file2 Hello Surprise!!! You overwrite fun1 in file1 with fun1 from file2... So as not to do so you must

declare -f pr_fun1=$fun1
. file2
unset -f fun1
fun1=$pr_fun1
unset -f pr_fun1
fun1 Hello

it's save your previous definition for fun1 and restore it with the previous name deleting not needed imported one. Every time you import functions from another file you may remember two aspects:

  1. you may overwrite existing ones with the same names(if that the thing you want you must preserve them as described above)
  2. import all content of import file(functions and global variables too) Be careful! It's dangerous procedure
查看更多
等我变得足够好
5楼-- · 2019-03-08 03:32

Refactor your second.sh script like this:

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

function func2 {
   fun2=$1
   book2=$2
   printf "fun2=%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:

fun=love,book=horror
fun2=ball,book2=mystery
查看更多
Luminary・发光体
6楼-- · 2019-03-08 03:45
#vi function.sh

   #!/bin/bash
`enter code here`f1()

    {
echo "Hello $name"
}
f2(){
echo "Enter your name: "
read name
f1
}
f2
#sh function.sh
Here function 2 will call function 1
查看更多
登录 后发表回答