• shell脚本--文件包含


      首先介绍一下shell中包含文件的方法,在C,C++,PHP中都是用include来包含文件,Go和Java使用import来包含(导入)包,而在shell中,很简单,只需要一个点“.”,然后跟着文件路径及文件名,或者使用source关键字也可以,注意文件路径可以使用绝对路径和相对路径。

      下面是一个文件包含的例子:three.sh包含one.sh和two.sh

    #!/bin/bash
    #one.sh
    
    one="the is one in file one.sh"
    

      

    #!/bin/bash
    #two.sh
    
    two="this is two in file two.sh"
    

      

    #!/bin/bash
    #three.sh
    
    #以下包含文件的两种方法等效,推荐使用source关键字
    . one.sh 
    source two.sh
    
    echo $one
    echo $two
    

      运行结果:

    [root@localhost ~]# ./three.sh
    the is one in file one.sh
    this is two in file two.sh
    [root@localhost ~]# 
    

      

      需要注意的是,一次只能包含一个文件,不要包含多个文件,比如下例中,尝试使用source一次性包含两个文件,最终,包含进来的只有第一个文件,后面的文件没有成功包含

    #!/bin/bash
    #three.sh
    
    source one.sh two.sh
    
    echo $one
    echo $two
    

      运行结果:

    [root@localhost ~]# ./three.sh
    the is one in file one.sh
    
    [root@localhost ~]# 
    

      

      还要注意的是,在其他语言中,重复包含同一个文件(B包含了A,C包含了A和B,造成C包含了A两次)会报错,而在shell中是不会报错的,仍旧会正常运行!!!!

  • 相关阅读:
    十 mybatis逆向工程
    九 spring和mybatis整合
    八 mybatis查询缓存(一级缓存,二级缓存)和ehcache整合
    七 mybatis的延迟加载
    利用游标循环删除数据
    oracle11g空表导不出来的解决办法
    关闭启动数据库AWR功能
    数据库维护操作
    开启和关闭oracle数据库中的审计功能
    Oracle序列
  • 原文地址:https://www.cnblogs.com/-beyond/p/8410583.html
Copyright © 2020-2023  润新知