电脑技术学习

对话 UNIX: !$#@*%

dn001

  例如,Unix 必须单独处理下面五个 echo 语句:

# echo "Line 1"
Line 1
# echo "Line 2"
Line 2
# echo "Line 3"
Line 3
# echo "Line 4"
Line 4
# echo "Line 5"
Line 5

  可以用以下代码替换多个 echo 语句,UNIX 只需处理一次执行:

# cat << EOF
> Line 1
> Line 2
> Line 3
> Line 4
> Line 5
> EOF
Line 1
Line 2
Line 3
Line 4
Line 5

  还可以使用制表符让 shell 脚本中的内容更整洁一点,这只需要在 << 和终止标识符之间放上一个连字符(-):

# cat <<- ATC
>  Line 1
>  Line 2
>  Line 3
>  Line 4
>  Line 5
> ATC
Line 1
Line 2
Line 3
Line 4
Line 5

  清单 7 给出的示例演示如何结合使用本文到目前为止讨论的东西。

  清单 7. 组合 CLI

# cat redirect_example
#!/usr/bin/ksh
cat <<- ATC | sed "s/^/Redirect Example => /g" >> atc.out
This is an example of how to redirect
stdout to a file as well as pipe stdout into stdin
of another command (i.e. sed), all done inside
a here-document.
Cool eh?
ATC

  现在,看看关于重定向和管道的脚本。

# ./redirect_example
# cat atc.out
Redirect Example => This is an example of how to redirect
Redirect Example => stdout to a file as well as pipe stdout into stdin
Redirect Example => of another command (i.e. sed), all done inside
Redirect Example => a here-document.
Redirect Example =>
Redirect Example => Cool eh?

标签: