shell从函数文件里调用函数

碰到一个shell中函数调用的小问题,记录一下。

shell中函数有三种调用方式,一种是在文件前面定义函数,然后在以下直接调用;一种是通过加载shell,在shell中直接调用;第三种是将函数写入文件。然后在其它shell中调用函数。

这里写一下关于第三种方法的样例:

is_it_a_directory()
{
if [ $# -lt 1 ];then
  echo "is_it_a_directory:I need an argument"
  return 1
fi

_DIRECTORY_NAME=$1
if [ ! -d $_DIRECTORY_NAME ];then
  return 1
else
  return 0
fi
}

error_msg()
{
echo -e "\007"
echo $@
echo -e "\007"
  return 0
}

这个文件定义了两个函数,我们在以下的shell中调用者两个函数,这里有一点须要注意,在调用之前,要加载函数文件。加载的方式为 . /路径。注意有个空格

#!/bin/sh
. functions.sh
echo -n "enter destination directory :"
read DIREC
if is_it_a_directory $DIREC
then :
else
  error_mag "$DIREC does not exist...creating it now"
  mkdir #DIREC > /dev/null 2>&1
  if [ $? != 0 ];
  then
    error_msg "could not "
    exit 1
  else :
  fi
fi

echo "extracting files..."

 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值