【BASH】如何避免Here-Document的奇怪行为
2009-04-14 14:51
211 查看
【BASH】如何避免Here-Document的奇怪行为
如果不了解Here-Document,请看bash
man page中here-documents的部分,也可以看本文后面附录的在man
page 中截取的here-document说明。
如果您的Here-Document行为怪诞,比如您维护捐赠者的名单,并且创建了如下面这样的donor文件:
$ cat donors
#
# simple
lookup of our generous donors
#
grep $1 <<EOF
# name amt
pete $100
joe $200
sam $ 25
bill $ 9
EOF
$
但是当您如果用下面的方式调用,那么会得到如下的奇怪输出
$ ./donors
bill
pete bill00
bill $ 9
$ ./donors
pete
pete pete00
$
解决方法
关掉shell脚本的一个功能:在Here-Document中忽略ending
marker前的任何字符
# solution
grep $1 <</EOF
pete $100
joe $200
sam $ 25
bill $ 9
EOF
讨论:
实际上使用<</EOF
<<'EOF',甚至<<E/OF都可以达到这样的效果,但是<</EOF在语法上面更加的简洁。
通常的情况(除非向上面solution给出的方式),bash的
manpage中这样说明“在here-documents中的行都服从于参数扩展,命令替换,和数学表达式扩展
这样就比较清晰了,$100在./donor
bill的用例中将被视为$1和两个零,这样就等于是bill00
一个建议是除非你明确的想使用命令扩展,参数扩展等功能,还是使用<<'EOF'或者<</EOF这样的方式避免一些bash的怪异行为(看起来的怪异行为)
here-documents
The format of here-documents is:
<<[-]word
here-document
delimiter
No parameter
expansion, command substitution, arithmetic expansion, or
pathname expansion is performed on word. If any characters in word
are
quoted, the delimiter is the result of quote removal on
word, and the
lines in the here-document are not
expanded. If word is unquoted, all
lines of the
here-document are subjected to parameter expansion, com-
mand substitution, and arithmetic expansion. In the latter case,
the
character sequence /<newline> is ignored, and /
must be used to quote
the characters /, $, and `.
If the redirection operator is <<-, then all leading tab
characters are
stripped from input lines and the line
containing delimiter. This
allows here-documents within
shell scripts to be indented in a natural
fashion.
如果英文看不太清楚呢,可以参考下面的这个网页 http://linux.tnc.edu.tw/techdoc/perl_intro/x338.html
如果不了解Here-Document,请看bash
man page中here-documents的部分,也可以看本文后面附录的在man
page 中截取的here-document说明。
如果您的Here-Document行为怪诞,比如您维护捐赠者的名单,并且创建了如下面这样的donor文件:
$ cat donors
#
# simple
lookup of our generous donors
#
grep $1 <<EOF
# name amt
pete $100
joe $200
sam $ 25
bill $ 9
EOF
$
但是当您如果用下面的方式调用,那么会得到如下的奇怪输出
$ ./donors
bill
pete bill00
bill $ 9
$ ./donors
pete
pete pete00
$
解决方法
关掉shell脚本的一个功能:在Here-Document中忽略ending
marker前的任何字符
# solution
grep $1 <</EOF
pete $100
joe $200
sam $ 25
bill $ 9
EOF
讨论:
实际上使用<</EOF
<<'EOF',甚至<<E/OF都可以达到这样的效果,但是<</EOF在语法上面更加的简洁。
通常的情况(除非向上面solution给出的方式),bash的
manpage中这样说明“在here-documents中的行都服从于参数扩展,命令替换,和数学表达式扩展
这样就比较清晰了,$100在./donor
bill的用例中将被视为$1和两个零,这样就等于是bill00
一个建议是除非你明确的想使用命令扩展,参数扩展等功能,还是使用<<'EOF'或者<</EOF这样的方式避免一些bash的怪异行为(看起来的怪异行为)
here-documents
The format of here-documents is:
<<[-]word
here-document
delimiter
No parameter
expansion, command substitution, arithmetic expansion, or
pathname expansion is performed on word. If any characters in word
are
quoted, the delimiter is the result of quote removal on
word, and the
lines in the here-document are not
expanded. If word is unquoted, all
lines of the
here-document are subjected to parameter expansion, com-
mand substitution, and arithmetic expansion. In the latter case,
the
character sequence /<newline> is ignored, and /
must be used to quote
the characters /, $, and `.
If the redirection operator is <<-, then all leading tab
characters are
stripped from input lines and the line
containing delimiter. This
allows here-documents within
shell scripts to be indented in a natural
fashion.
如果英文看不太清楚呢,可以参考下面的这个网页 http://linux.tnc.edu.tw/techdoc/perl_intro/x338.html
相关文章推荐
- 方舟子:如何避免学术不端行为(zz)
- ubuntu cd /h tab键显示错误:bash: cannot create temp file for here-document: No space left on device
- 如何使用Git Bash Here,将本地项目传到github上
- 如何避免渠道商的“养卡”和“劝弃卡”行为的发生
- 『阿男的技术日志』*01 Bash的Here Document的使用方法*
- 如何避免数据库行为监控系统部署问题
- javascript节点拖动如何避免影响某些节点对象的默认行为
- 如何使用Git Bash Here,将本地项目传到github上
- 如何使用Git Bash Here,将本地项目传到github上
- javascript节点拖动如何避免影响某些节点对象的默认行为【转自qiqiboy】
- 如何在Mac中使用Android SDK,使用adb工具避免出现-bash: adb: command not found
- 树莓派使用Tab键后提示bash: cannot create temp file for here-document: No space left on device
- Bash 脚本中如何使用 here 文档将数据写入文件
- Git------如何使用Git Bash Here提交代码
- 如何在Mac中使用Android SDK,使用adb工具避免出现-bash: adb: command not found
- 如何避免上网行为管理产品选型误区
- 如何在Mac中使用Android SDK,使用adb工具避免出现-bash: adb: command not found
- Facebook如何才能避免缓慢而痛苦的死亡
- 扫二维码银行卡诈骗是如何实现的?日常生活中如何避免?
- 如何避免ERP激情上线后被打回原形?