redir2.sh

来自「一本完整的描述Unix Shell 编程的工具书的所有范例」· Shell 代码 · 共 48 行

SH
48
字号
#!/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 *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.)

⌨️ 快捷键说明

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