Linux函数的使用方法

[root@a ~]#cat fun.txt      #定义函数库文件,方便在别的地方使用bash

addnum1() {函数

  echo $[$1+$2]测试

}spa

addnum2(){ip

  echo $[$1*$2]class

}test

deluser(){引用

  userdel -r $user脚本

}di

[root@a ~]#pwd 

/scripts/day2/

[root@a ~]#cat test.sh

#!/bin/bash

source /scripts/day2/fun.txt    #脚本中引用函数文件

#source  ./fun.txt      #相对路径方式

#. ./fun.txt        #相对路径方式,其中.至关于source

read -p "del user: " user

deluser        #调用函数

addnum1 4 5      #调用函数

addnum2 24 3      #调用函数

 

[root@a ~]#sh test.sh      #执行测试脚本

del user: abc

9

72

[root@a ~]#id abc      #查看测试结果

id: abc: no such user

相关文章
相关标签/搜索