亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 變量賦值問題,大家看看 [打印本頁]

作者: junglegq    時間: 2007-06-01 12:25
標(biāo)題: 變量賦值問題,大家看看
兩個程序,變量name的輸出結(jié)果不同,大家給診斷一下
#!/bin/bash
declare -i a=1
declare name=""
while [ $a -le 5 ]; do
        echo $a
        a=a+1
        name="neil"
done
echo $name
echo $a

name輸出是: neil

#!/bin/bash
declare -i a=1
declare name=""
cat /tmp/db | while read line; do
        echo $a
        a=a+1
        name="neil"
done
echo $name
echo $a
其中,/tmp/db只有兩行,
name輸出為空
作者: Edengundam    時間: 2007-06-01 12:54
環(huán)境變量 + sub shell
作者: 寂寞烈火    時間: 2007-06-01 13:33
while read line;do
....
done<filename
作者: lgfang    時間: 2007-06-01 23:10
comp.unix.shell 的 FAQ

33. Why do I lose the value of global variables that are set in a loop.

    Given the following program

      #!/bin/sh
      x="this is the initial value of x"
      cat dataFile | while read line;do
        x="$line"
      done
      echo x = $x

     You may get the following for output

       x = this is the initial value of x

     This is because in the Bourne shell redirected control structures
     run in a subshell, so the value of x only gets changed in the
     subshell, and is lost when the loop ends.

     In other shells the same result may be seen because of the way
     pipelines are handled. In shells other than ksh (not pdksh) and
     zsh elements of a pipeline are run in subshells. In ksh and zsh,
     the last element of the pipeline is run in the current shell.

     An alternative for non-Bourne shells is to use redirection
     instead of the pipeline

      #!/bin/sh
      x="this is the initial value of x"
      while read line;do
        x="$line"
      done < dataFile
      echo x = $x

    With a Bourne shell you need to reassign file descriptors, so no
    pipline or redirection in the loop is involved.

      exec 3<&0         # save stdin
      exec < file
      while read line; do
        x=$line
      done
      exec 0<&3        # restore stdin

    Note that putting #!/bin/sh at the top of a script doesn't
    guarantee you're using the Bourne shell. Some systems link /bin/sh
    to some other shell. Check your system documentation to find out
    what shell you're really getting in this case.
作者: junglegq    時間: 2007-06-06 14:54
謝謝lgfang啦,說的夠詳細(xì)




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2