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

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1702 | 回復(fù): 4
打印 上一主題 下一主題

變量賦值問題,大家看看 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2007-06-01 12:25 |只看該作者 |倒序瀏覽
兩個程序,變量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輸出為空

論壇徽章:
0
2 [報告]
發(fā)表于 2007-06-01 12:54 |只看該作者
環(huán)境變量 + sub shell

論壇徽章:
1
榮譽(yù)會員
日期:2011-11-23 16:44:17
3 [報告]
發(fā)表于 2007-06-01 13:33 |只看該作者
while read line;do
....
done<filename

論壇徽章:
0
4 [報告]
發(fā)表于 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.

論壇徽章:
0
5 [報告]
發(fā)表于 2007-06-06 14:54 |只看該作者
謝謝lgfang啦,說的夠詳細(xì)
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP