avoid-subshell.sh

来自「Shall高级编程」· Shell 代码 · 共 53 行

SH
53
字号
#!/bin/bash# avoid-subshell.sh# Suggested by Matthew Walker.Lines=0echocat myfile.txt | while read line;                 do {                   echo $line                   (( Lines++ ));  #  Incremented values of this variable                                   #+ inaccessible outside loop.                                   #  Subshell problem.                 }                 doneecho "Number of lines read = $Lines"     # 0                                         # Wrong!echo "------------------------"exec 3<> myfile.txtwhile read line <&3do {  echo "$line"  (( Lines++ ));                   #  Incremented values of this variable                                   #+ accessible outside loop.                                   #  No subshell, no problem.}doneexec 3>&-echo "Number of lines read = $Lines"     # 8echoexit 0# Lines below not seen by script.$ cat myfile.txtLine 1.Line 2.Line 3.Line 4.Line 5.Line 6.Line 7.Line 8.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?