shell -- include 文件

本文介绍了Shell脚本中使用include方法来引入其他脚本文件的技术,包括如何通过相对路径加载函数和变量,以及如何在sysv启动文件中利用此方法。此外还提供了几个示例,如状态检查和进程终止等。

shell文件 include 的方法——“ 。  文件”

[macg@localhost testshell]$ ls
testget  testlib
[macg@localhost testshell]$ cat testlib
#!/bin/bash
test() {
echo "hello "
}

[macg@localhost testshell]$ cat testget
#!/bin/bash

. /home/macg/testshell/testlib     include 文件
test                              调用include文件里的函数
[macg@localhost testshell]$ sh testget
hello
 



   shell include 不止可以包含函数,也可以包含全局变量

[macg@localhost testshell]$ cat testlib
#!/bin/bash

test() {
echo "hello "
}

TESTPRARAM="THIS IS A TEST"

[macg@localhost testshell]$ cat testget
#!/bin/bash

. /home/macg/testshell/testlib
test
echo $TESTPRARAM
[macg@localhost testshell]$ sh testget
hello
THIS IS A TEST



    实际上sys v的启动文件都采用此种办法include 事先定义在/etc/rc.d/init.d/functions中的函数
[root@localhost macg]# cat /etc/init.d/sendmail
#!/bin/bash

# Source function library.
. /etc/rc.d/init.d/functions


   for in 语句include *.sh文件的方法 

for i in /etc/profile.d}
        local pid

        # Test syntax.
        if [ "$#" = 0 ] ; then
                echo $"Usage: status {program}"
                return 1
        fi

        # First try "pidof"
        pid=`pidof -o $$ -o $PPID -o %PPID -x $1 || /
             pidof -o $$ -o $PPID -o %PPID -x ${base}`
        if [ -n "$pid" ]; then
                echo $"${base} (pid $pid) is running..."
                return 0
        fi

        # Next try "/var/run/*.pid" files
        if [ -f /var/run/${base}.pid ] ; then
                read pid < /var/run/${base}.pid
                if [ -n "$pid" ]; then
                        echo $"${base} dead but pid file exists"
                        return 1
                fi
        fi
        # See if /var/lock/subsys/${base} exists
        if [ -f /var/lock/subsys/${base} ]; then
                echo $"${base} dead but subsys locked"
                return 2
        fi
        echo $"${base} is stopped"
        return 3
}    

[root@mm testtip]# vi test.sh

#!/bin/bash
. ./functions

echo -n "input:"
read para
status $para
result=$?
echo "$result" 
[root@mm testtip]# sh test.sh
input:bgpd
bgpd (pid 3430) is running...
0

[root@mm testtip]# sh test.sh
input:xxx
xxx is stopped
3


  •    killproc() 

        以名字的方式杀掉一个进程

[root@mm testtip]# vi test.sh

#!/bin/bash
. ./functions

echo -n "input:"
read para
killproc $para
result=$?
echo "$result"
[root@mm testtip]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root      4769     1  0 14:47 ?        00:00:00 rpc.mountd
root      4795     1  0 14:47 ?        00:00:00 /usr/sbin/dhcpd
root      4942  4408  0 16:32 pts/0    00:00:00 ps –ef

[root@mm testtip]# sh test.sh
input:dhcpd
0  OK  ]


[root@mm testtip]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root      4769     1  0 14:47 ?        00:00:00 rpc.mountd
root      4950  4408  0 16:34 pts/0    00:00:00 ps -ef

   

   confirm()             系统暂停,输入y/n/c

# Confirm whether we really want to run this service
confirm() {
  [ -x /usr/bin/rhgb-client ] && /usr/bin/rhgb-client --details=yes
  while : ; do
      echo -n $"Start service $1 (Y)es/(N)o/(C)ontinue? [Y] "
      read answer
      if strstr $"yY" "$answer" || [ "$answer" = "" ] ; then
         return 0
      elif strstr $"cC" "$answer" ; then
         rm -f /var/run/confirm
         [ -x /usr/bin/rhgb-client ] && /usr/bin/rhgb-client --details=no
         return 2
      elif strstr $"nN" "$answer" ; then
         return 1
      fi
  done
}   
[machome@mm testtip]$ cat test.sh
#!/bin/bash
. ./functions

confirm
result=$?
echo "$result"
    
[machome@mm testtip]$ sh test.sh
Start service  (Y)es/(N)o/(C)ontinue? [Y] y
0
[guan@mm testtip]$ sh test.sh
Start service  (Y)es/(N)o/(C)ontinue? [Y] n
1
[guan@mm testtip]$ sh test.sh
Start service  (Y)es/(N)o/(C)ontinue? [Y] c



   /etc/profile中的的函数pathmunge(),给$PATH增加一个路径

#!/bin/bash

pathmunge () {
        if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
           if [ "$2" = "after" ] ; then
              PATH=$PATH:$1
           else
              PATH=$1:$PATH
           fi
        fi
}
 
[root@vm test]# vi tip.sh

pathmunge /home/macg
pathmunge /home/mac
echo $PATH
[root@vm test]# ./tip.sh
/home/mac:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr
/X11R6/bin:/home/macg/bin:/home/macg
[root@vm test]#
 



<think> 首先,用户的问题是:“我想了解.mk文件-includeinclude的区别 请问mk文件-includeinclude的区别”。这是在问Makefile中-includeinclude指令的区别。 我从系统指令中知道,需要: - 使用中文回答。 - 所有行内数学表达式用$...$格式。 - 独立公式用$$...$$格式并单独成段。 - LaTeX语法正确。 - 生成相关问题。 - 在回答中引用的段落末尾自然地添加引用标识,例如:[^1]。 回顾提供的引用: - 引用[1]:关于include的使用,可以包含路径和通配符,文件名可以是Shell文件模式。 - 引用[2]:解释include-include的区别,如果文件不存在时的行为。 - 引用[3]:-include会忽略缺失的文件,而include会导致错误。 - 引用[4]:类似引用[2],描述文件搜索路径。 - 引用[5]:一个测试示例,展示include-include在实际文件缺失时的行为。 用户的问题直接针对Makefile中的-includeinclude的区别,所以我需要基于这些引用构建一个清晰的回答。 关键区别: 1. **include**:如果指定文件不存在,Make会报告错误并停止执行。 2. **-include**(或sinclude):如果指定文件不存在,Make会忽略错误并继续执行。 引用[2]和[3]明确指出了这一点。引用[3]的例子很好:当文件缺失时,-include不会报错。 此外,include文件搜索路径在引用[2]和[4]中描述:如果文件不是绝对路径,Make会搜索当前目录、-I指定的目录、以及默认目录如/usr/include。 回答结构: - 开头概述区别。 - 解释每个指令的行为。 - 提供示例来澄清。 - 自然地引用来源。 - 最后生成相关问题。 用户提到“.mk文件”,这指的是Makefile文件,所以我会使用Makefile的上下文。 确保回答结构清晰: 1. 介绍include-include。 2. 主要区别:错误处理。 3. 文件搜索路径。 4. 示例。 5. 总结。 生成相关问题:在末尾添加,如示例中的“§§相关问题§§”。 用中文回答。 大纲: - 开头:直接回答问题。 - 例如:"在Makefile中,`include` 和 `-include` 主要用于包含其他文件,但它们的主要区别在于处理文件缺失时的行为。" - 详细解释: - **include**: 如果文件不存在,Make会报错并停止。 - **-include**: 如果文件不存在,忽略错误并继续。 - 支持细节:文件搜索路径相同。 - 示例:简单代码块展示。 - 引用:在相关句子末尾加[^2], [^3]等。 - 结尾:总结。 - 相关问题:生成2-3个相关问题。 生成相关问题: 1. 例如:"如何在Makefile中处理可选配置文件?" 2. "还有哪些Makefile指令用于包含文件?" 3. "在实际项目中,何时应该使用-include?" 确保LaTeX:用户问题中没有数学表达式,所以可能不需要,但如果有数学相关,就使用。 在回答中,没有明显的数学内容,所以跳过数学格式。 最后,添加相关问题部分。 </think> ### Makefile中`include`和`-include`的区别 在Makefile中,`include` 和 `-include` 都用于在当前Makefile中包含其他文件(如`.mk`文件),以实现代码复用或模块化管理。它们的核心区别在于处理文件缺失时的错误行为:`include` 在文件不存在时会导致Make报错并停止执行,而 `-include` (或等效的`sinclude`)则会忽略缺失文件的错误并继续执行剩余命令。下面我将从工作原理、错误处理、文件搜索路径等方面详细解释,并提供示例说明。 #### 1. **基本工作原理** - **`include`**: 此指令强制加载指定文件。如果文件不存在或无法找到,Make会立即报错(如`make: *** No rule to make target 'filename.mk'. Stop.`),并中止整个构建过程。 - **`-include`**: 此指令同样加载指定文件,但如果文件不存在,Make不会报错——它会静默忽略该指令,继续执行后续命令。这使得 `-include` 适合加载可选配置文件或条件依赖文件[^3]。 两者的语法相同:`include filename` 或 `-include filename`,其中`filename`可以是具体文件名、路径或带通配符的模式(如`*.mk`)。在格式上,`include` 前面可以有空格,但不能使用Tab键开头[^1]。 #### 2. **文件搜索路径** `include` 和 `-include` 在文件搜索逻辑上完全一致:如果文件不是绝对路径(如`/path/to/file.mk`),Make会依次搜索以下目录: - 当前目录。 - `-I` 或 `--include-dir` 命令行选项指定的目录。 - 系统默认目录,如`/usr/gnu/include`, `/usr/local/include`, `/usr/include`[^2][^4]。 例如,假设你有文件`a.mk`, `b.mk`, 和一个变量`$(bar)` 包含`e.mk`,命令`include $(bar)` 会尝试加载`e.mk`。如果这些文件不存在于搜索路径中,`include` 会报错,而 `-include` 不会[^1][^2]。 #### 3. **错误处理差异的示例** 以下Makefile示例展示了实际区别: ```makefile # 示例Makefile all: @echo "开始构建..." # 尝试包含一个不存在的文件 -include missing_file.mk # 使用-include,即使文件缺失也不会报错 # include missing_file.mk # 如果使用include,这里会报错并停止 @echo "构建完成!" ``` - **情况1:使用 `-include`** 运行`make`时,输出为: ``` 开始构建... 构建完成! ``` 尽管`missing_file.mk`不存在,Make忽略错误并继续执行[^3][^5]。 - **情况2:使用 `include`** 运行`make`时,输出为: ``` make: *** No rule to make target 'missing_file.mk'. Stop. ``` Make报错后立即停止,不会打印"构建完成!"[^2][^5]。 #### 4. **使用场景建议** - **使用 `include`**: 当被包含文件是必需的(如核心配置或依赖),缺失会导致构建失败时。这有助于及早发现问题。 - **使用 `-include`**: 当文件是可选或条件性的(如开发者本地配置文件),允许构建过程在文件缺失时继续运行。这在跨平台项目中很常见[^3][^4]。 总之,主要区别在于错误容忍度:`include` 严格保证文件存在,而 `-include` 提供更灵活的容错机制。两者在其他方面(如搜索路径)行为一致。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值