📄 redir2.sh
字号:
#!/bin/bash# redir2.shif [ -z "$1" ]then Filename=names.data # Default, if no filename specified.else Filename=$1fi #+ Filename=${1:-names.data}# can replace the above test (parameter substitution).count=0echowhile [ "$name" != Smith ] # Why is variable $name in quotes?do read name # Reads from $Filename, rather than stdin. echo $name let "count += 1"done <"$Filename" # Redirects stdin to file $Filename. # ^^^^^^^^^^^^echo; echo "$count names read"; echoexit 0# Note that in some older shell scripting languages,#+ the redirected loop would run as a subshell.# Therefore, $count would return 0, the initialized value outside the loop.# Bash and ksh avoid starting a subshell *whenever possible*,#+ so that this script, for example, runs correctly.# (Thanks to Heiner Steven for pointing this out.)# However . . .# Bash *can* sometimes start a subshell in a PIPED "while-read" loop,#+ as distinct from a REDIRECTED "while" loop.abc=hiecho -e "1\n2\n3" | while read l do abc="$l" echo $abc doneecho $abc# Thanks, Bruno de Oliveira Schneider, for demonstrating this#+ with the above snippet of code.# And, thanks, Brian Onn, for correcting an annotation error.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -