现在的位置: 首页 > 综合 > 正文

shell 函数和source命令用法

2013年02月03日 ⁄ 综合 ⁄ 共 359字 ⁄ 字号 评论关闭

source命令可以在一个脚本里载入另一个脚本,下面举个例子:

首先我们定义了一个函数脚本,名字叫myfunc.sh ,如下:

#!/bin/bash
function square
{
   local temp
   let temp=$1*$1 
   echo "$1的平方和为:$temp"
}
function cube
{
   local temp
   let temp=$1*$1*$1
   echo "$1 立方和为:$temp"
}

然后我们再定义另外一个脚本,叫caluate:

#!/bin/bash
source myfunc.sh
echo "请输入一个整数:"
read N
square $N
cube $N

source myfunc.sh 就是载入myfunc.sh这个脚本,然后可以在caluate 脚本中调用myfunc.sh里定义的函数square和cube了。

抱歉!评论已关闭.