Chinaunix首页 | 论坛 | 博客
  • 博客访问: 542167
  • 博文数量: 126
  • 博客积分: 2071
  • 博客等级: 大尉
  • 技术积分: 1522
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-24 16:09
文章分类
文章存档

2013年(8)

2012年(37)

2011年(80)

2010年(1)

分类:

2011-08-24 20:03:38

原文地址:export的简单应用 作者:随1意2o

如果要传递函数给子shell环境使用,可使用export和-f选项
用法:
export -f 函数名

实际例子如下:
function3.sh  如下:
 1 #! /bin/bash                                                                                                                     
  2
  3 #把$2的内容转向附加到$1指定的文件中
  4 #Auter:panda
  5 #Time:2011-08-10
  6 appendfile() {
  7     echo "$2" >> "$1"
  8 }
  9
 10 Dir=~/tmp
 11 OutFile="$Dir/tmp.txt"
 12
 13 #如果目录不存在则会创建新的目录
 14 [ -e $Dir ] || mkdir -p $Dir
 15
 16 appendfile $OutFile "函数可以这样用"
 17 appendfile $OutFile "这是函数的另一种用法"
 18 appendfile $OutFile "Over. "
 19
 20 appendfile $OutFile "函数可以这样用"
 21 export -f appendfile
 22
 23 ./test_path.sh

  test_path.sh如下:        1#!/bin/bash                                                                                                                      
  2
  3 appendfile ~/tmp/tmp.txt "wangpanda"

执行结果如下
panda@panda-pc:~/Code/Shell$ ./function3.sh
panda@panda-pc:~/Code/Shell$ cd ~/tmp
panda@panda-pc:~/tmp$ cat tmp.txt
函数可以这样用
这是函数的另一种用法
Over.
函数可以这样用
wangpanda
panda@panda-pc:~/tmp$

注:子shell向文件写入的是wangpanda

阅读(501) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~