注意:和普通脚本不一样,库脚本(被调用文件不需要有x权限)

即一个shell脚本含有执行shell脚本的命令

像其他语言一样,Shell 也可以包含外部脚本,将外部脚本的内容合并到当前脚本。

Shell 中包含脚本可以使用:

. filename

 或

source filename

 一个是主文件 t1.sh,内容如下:

shell之路 shell核心语法【第六篇】文件调用shell之路 shell核心语法【第六篇】文件调用
#!/bin/bash
add (){
    local res
    let res=$1+$2
    echo $res
}

reduce (){
    local res
    let res=$1-$2
    echo $res
}

multiple (){
    local res
    let res=$1*$2
    echo $res   
}

divide (){
    local res
    let res=$1/$2
    echo $res
}
mathlib.sh
shell之路 shell核心语法【第六篇】文件调用shell之路 shell核心语法【第六篇】文件调用
#!/bin/bash

# 当前文件路径: /root/wulei/shellScripts
# mathlib.sh文件路径: /root/wulei/shellScripts/lib

###### 绝对导入 ######
# . /root/wulei/shellScripts/lib/mathlib.sh 

###### 相对导入 ######
. ./lib/mathlib.sh


res1=$(add 3 4)
res2=$(reduce 3 4)
res3=$(multiple 3 4)
res4=$(divide 3 4)

echo $res1 $res2 $res3 $res4
t1.sh

shell之路 shell核心语法【第六篇】文件调用

 

 shell之路 shell核心语法【第六篇】文件调用

 

转载于:https://www.cnblogs.com/hyit/articles/11138724.html

相关文章:

  • 2021-11-04
  • 2021-06-07
  • 2021-10-17
  • 2022-12-23
  • 2022-12-23
  • 2021-07-09
  • 2021-08-08
  • 2021-10-10
猜你喜欢
  • 2022-12-23
  • 2021-12-04
  • 2021-12-12
  • 2021-07-21
  • 2022-12-23
  • 2021-08-17
  • 2021-09-18
相关资源
相似解决方案