The variants of ksh

Points

1. If you want to share the variants in a shell to the the children of the current processes. There are 2 ways in ksh,
  1) . setEnv.ksh
  2) eval "setEnv.ksh"

2. You can define a variants in the 3 ways, they are similar
  
  1) OUTPUT="test"
  2) OUTPUT="test"
      export OUTPUT
  3) typeset OUTPUT="test"

   The difference are,

   1) Typeset can set the variant as read-only.

   2) The exported variants can be inherited by child processes.

3. What is diference between $VARIANT_NAME and ${VARIANT_NAME}?
  1) $VARIANT_NAME populates the VARIANT_NAME to characters as many as possible. For example, $Fest equels ${Fest}.
  2) ${VARIANT_NAME} can use REG to populate the expression values. For example, TEST="/opt/sfw/gcc" echo ${TEST%/*}, will print /opt/sfw


4. Today, I made a test for the variants in ksh. I summarize the deals about ksh variants here.

 1). All of the variants defined both in global area and inside function share the same variant area. Please see #2, #3 and #4.
 2). If you define a variant in global area, you can get it inside a function.
 3). If you define a variant inside a function, you can get it in a global area.
 4). If you define a variant inside a function, you can get it in another function.
 5). It is not the same situation with export keywork or not. That is to say, the exported variants can be inherited by child processes. In the same process, with the export or not, it doesn't matter. But if you want child process to share the variant, you must export them. 
 6). In global area, $0, $1... is the process parameters while inside function is function parameters.

Reference

 http://topic.csdn.net/u/20080602/22/cce87943-b9b1-4117-a138-9ac0b102257f.html

Source

 test1() {
   echo "before test()"
  echo "TEST0: ${TEST0}"
  echo "TEST1: ${TEST1}"
  echo "TEST2: ${TEST2}"
  echo "TEST3: ${TEST3}"
  
  TEST0=$1
  TEST1=$2
  TEST2=$3
   TEST3=$4
  
   echo "after assign parameter inside test()"
   echo "TEST0: ${TEST0}"
  echo "TEST1: ${TEST1}"
  echo "TEST2: ${TEST2}"
  echo "TEST3: ${TEST3}"
 
 
  TEST0="bde"
  TEST1="bde"
  TEST2="bde"
   TEST3="bde"
  
   export TEST2
   export TEST3
 
   echo "after test()"
  echo "TEST0: ${TEST0}"
  echo "TEST1: ${TEST1}"
  echo "TEST2: ${TEST2}"
  echo "TEST3: ${TEST3}"
  
  TEST4="hahaha"
  TEST5="hihihi"
  export TEST5
  echo "TEST4: ${TEST4}"
  echo "TEST5: ${TEST5}"
 }
 
 {
  TEST0="abc"
  TEST1="abc"
  TEST2="abc"
   TEST3="abc"
  
   export TEST1
   export TEST3
  
   echo "before call method"
  
   echo "TEST0: ${TEST0}"
  echo "TEST1: ${TEST1}"
  echo "TEST2: ${TEST2}"
  echo "TEST3: ${TEST3}"
 
   test1 $TEST0 $TEST1 $TEST2 $TEST3
 
   echo "after call method"
  echo "TEST0: ${TEST0}"
  echo "TEST1: ${TEST1}"
  echo "TEST2: ${TEST2}"
  echo "TEST3: ${TEST3}"
  
  echo "TEST4: ${TEST4}"
  echo "TEST5: ${TEST5}"
 }
 
 
 before call method
 TEST0: abc
 TEST1: abc
 TEST2: abc
 TEST3: abc
 before test()
 TEST0: abc
 TEST1: abc
 TEST2: abc
 TEST3: abc
 after assign parameter inside test()
 TEST0: abc
 TEST1: abc
 TEST2: abc
 TEST3: abc
 after test()
 TEST0: bde
 TEST1: bde
 TEST2: bde
 TEST3: bde
 TEST4: hahaha
 TEST5: hihihi
 after call method
 TEST0: bde
 TEST1: bde
 TEST2: bde
 TEST3: bde
 TEST4: hahaha
 TEST5: hihihi

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值