QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 879|回复: 1

she l l 中如何引用其他文件中的函数

[复制链接]
发表于 2004-11-14 20:49:45 | 显示全部楼层 |阅读模式
我看到 FC3 /e t c /init.d 中有个fu n c ti o n  ,里面说明说是一些函数---供其他she l l s c ri p t 函数调用的
另外谁知道这些函数是干什么用的
如:
[code:1]status() {
local base=${1##*/}
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
}[/code:1]
发表于 2004-11-14 22:22:34 | 显示全部楼层
如果一个脚本中都是函数,没有其它的命令的话,可以这样实现:
file function.sh[code:1]
#!/bin/bash
call()
{
        echo "this is funcion1 -- call"
}[/code:1]
file func_call.sh[code:1]
#!/bin/bash
. ./function1.sh
call[/code:1]
然后执行func_call.sh文件:
[code:1]$ chmod +x func_call.sh
$ ./func_call.sh
[/code:1]

至于脚本function.sh中另有其它操作时,该如何调用脚本中的函数而不执行其它操作,小弟还不明白,还请知道的朋友赐教。 :-)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-9-18 17:07 , Processed in 0.077750 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表