📄 avoid-subshell.sh
字号:
#!/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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -