Shell include文件
来源:互联网 发布:淘宝售后工作职责 编辑:程序博客网 时间:2024/06/05 10:00
shell文件 include 的方法——“ 。 文件”
[macg@localhost testshell]$ ls
testgettestlib[macg@localhost testshell]$ cat testlib
#!/bin/bash
test() {
echo "hello "
}
[macg@localhost testshell]$ cat testget
#!/bin/bash
. /home/macg/testshell/testlibinclude 文件
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
[macg@localhost testshell]$ ls
testget
#!/bin/bash
test() {
echo "hello "
}
[macg@localhost testshell]$ cat testget
#!/bin/bash
. /home/macg/testshell/testlib
test
hello
[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
[root@localhost macg]# cat /etc/init.d/sendmail
#!/bin/bash
# Source function library.
. /etc/rc.d/init.d/functions
for i in /etc/profile.d}