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