redir2a.sh

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

SH
46
字号
#!/bin/bash# This is an alternate form of the preceding script.#  Suggested by Heiner Steven#+ as a workaround in those situations when a redirect loop#+ runs as a subshell, and therefore variables inside the loop# +do not keep their values upon loop termination.if [ -z "$1" ]then  Filename=names.data     # Default, if no filename specified.else  Filename=$1fi  exec 3<&0                 # Save stdin to file descriptor 3.exec 0<"$Filename"        # Redirect standard input.count=0echowhile [ "$name" != Smith ]do  read name               # Reads from redirected stdin ($Filename).  echo $name  let "count += 1"done                      #  Loop reads from file $Filename                          #+ because of line 20.#  The original version of this script terminated the "while" loop with#+      done <"$Filename" #  Exercise:#  Why is this unnecessary?exec 0<&3                 # Restore old stdin.exec 3<&-                 # Close temporary fd 3.echo; echo "$count names read"; echoexit 0

⌨️ 快捷键说明

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