Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1185
  • 博文数量: 3
  • 博客积分: 90
  • 博客等级: 民兵
  • 技术积分: 25
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-22 17:26
文章分类
文章存档

2011年(3)

我的朋友
最近访客

分类: Python/Ruby

2011-12-28 15:34:22

* 所謂環境變量其實就是那些會傳給子行程的變量。
* 環境變量只能從父行程到子行程單向繼承。換句話說:在子行程中的環境如何變更,均不會影響父行程的環境。
* 正常來說,當我們執行一個 shell script 時,其實是先產生一個 sub-shell 的子行程,然後 sub-shell 再去產生命令行的子行程。
* 所謂 source 就是讓 script 在當前 shell 內執行、而不是產生一個 sub-shell 來執行。
* exec 也是讓 script 在同一個行程上執行,但是原有行程則被結束了。
  1. # cat 1.sh
  2. #!/bin/bash
  3. A=B
  4. echo "PID for 1.sh before exec/source/fork:$$"
  5. export A
  6. echo "1.sh: \$A is $A"
  7. case $1 in
  8. exec)
  9. echo "using exec..."
  10. exec ./2.sh ;;
  11. source)
  12. echo "using source..."
  13. . ./2.sh ;;
  14. *)
  15. echo "using fork by default..."
  16. ./2.sh ;;
  17. esac
  18. echo "PID for 1.sh after exec/source/fork:$$"
  19. echo "1.sh: \$A is $A"

  1. # cat 2.sh
  2. #!/bin/bash
  3. echo "PID for 2.sh: $$"
  4. echo "2.sh get \$A=$A from 1.sh"
  5. A=C
  6. export A
  7. echo "2.sh: \$A is $A"
  1. # ./1.sh fork
  2. PID for 1.sh before exec/source/fork:40125
  3. 1.sh: $A is B
  4. using fork by default...
  5. PID for 2.sh: 40126
  6. 2.sh get $A=B from 1.sh
  7. 2.sh: $A is C
  8. PID for 1.sh after exec/source/fork:40125
  9. 1.sh: $A is B
  1. # ./1.sh source
  2. PID for 1.sh before exec/source/fork:40133
  3. 1.sh: $A is B
  4. using source...
  5. PID for 2.sh: 40133
  6. 2.sh get $A=B from 1.sh
  7. 2.sh: $A is C
  8. PID for 1.sh after exec/source/fork:40133
  9. 1.sh: $A is C
  1. # ./1.sh exec
  2. PID for 1.sh before exec/source/fork:40137
  3. 1.sh: $A is B
  4. using exec...
  5. PID for 2.sh: 40137
  6. 2.sh get $A=B from 1.sh
  7. 2.sh: $A is C
阅读(134) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~