Shell之Here Document
来源:互联网 发布:重启网卡 linux 编辑:程序博客网 时间:2024/05/22 16:40
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jeremiah.blog.51cto.com/539865/339211
最近使用Shell编程编辑文件的时候,学习到了Here Document。感觉不错,分享给大家。
Here Document是已“<<空格变量”为标识的一段Shell程序,不知道这么理解是否准确。
我们知道平常在linux命令行的操作都可以放到文件里面,赋予可执行权限后,就可以将这文件变成一个shell脚本。但是如果我们跟有些程序交互时,shell脚本却帮不了我们太多。比如编辑一个文件和操作数据库。编辑一个文件一般用vi,进去后hjkl的aio的操作什么的,脚本怎么执行?mysql登陆输入mysql -u 用户 -p 密码后,就跟mysql进行一串交互,shell怎么执行?Here Document就可以派上用场了。
这里就写Here Document的两个应用。
1. shell编辑文件。
shell编辑文件最常用的方法就是echo 字符串 >> 文件。但是要删除一行怎么办?Here Document就搞定了。
经Jeremiah测试,在Here Document中使用vi是不行的。替代方法是使用ed命令。在命令行执行以下:
$ touch base.txt
$ ed base.txt
a
this is line1.
this is line2.
this is line3.
this is line4.
.
wq
$ ed base.txt
a
this is line1.
this is line2.
this is line3.
this is line4.
.
wq
先新建一个文件base.txt,然后ed这个文件,输入a表示最后追加,输入了四行文字。.表示结束输入。wq表示保存退出。
那我们再用ed命令,在shell脚本里面对这个文件再次进行操作。如下。
#!/bin/sh
ed base.txt << !ED1_JEREMIAH!
3
d
i
this is line 3 new.
.
w
q
!ED1_JEREMIAH!
ed base.txt << !ED1_JEREMIAH!
3
d
i
this is line 3 new.
.
w
q
!ED1_JEREMIAH!
解释下:ed base.txt << !ED1_JEREMIAH!表示编辑base.txt,用变量!ED1_JEREMIAH!标记,这里搞的变量复杂为了和shell中的其他变量进行区分。3表示到第3行,d表示删除,然后i表示本行增加,输入this is line 3 new。其他的同上述。最后用!ED1_JEREMIAH!结束。也就是说两个!ED1_JEREMIAH!之间的每一行都是类似与命令行输入到ed名中,进行交互。
执行的结果如下所示。
$ sh ed_file.sh && cat base.txt
60
this is line3.
65
this is line1.
this is line2.
this is line 3 new.
this is line4.
60
this is line3.
65
this is line1.
this is line2.
this is line 3 new.
this is line4.
关于ed的操作和参数,可以查看linux帮助或去搜索相关的资料。
2. shell控制数据库
假设执行下面的操作访问数据库。
$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1257
Server version: 5.1.35-community MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql> select * from user;
mysql> exit
Bye
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1257
Server version: 5.1.35-community MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql> select * from user;
mysql> exit
Bye
如果我们要用shell脚本访问,则可以编写如下的脚本。
#!/bin/sh
mysql -u root << !ED2_JEREMIAH!
use mysql
select * from user;
exit
!ED2_JEREMIAH!
mysql -u root << !ED2_JEREMIAH!
use mysql
select * from user;
exit
!ED2_JEREMIAH!
执行如下。
sh mysql_access.sh
可以看到结果相同。
在网上还看到过可以将查询的结果放置到Shell的变量中,这种高级操作大家有兴趣的自己去Google吧。
写在最后:只要是命令行与程序进行交互,用shell脚本的Here Document都可以实现。体会上面两处的用法,Jeremiah估计任何交互都能搞定。上面写的都是Jeremiah自己的理解,不一定很到位,欢迎批评指正。
本文出自 “海狗哥的流媒体空间” 博客,请务必保留此出处http://jeremiah.blog.51cto.com/539865/339211
- Shell之Here Document
- Shell之Here Document
- Shell之Here Document
- Shell之Here Document
- shell 操作之 read、cat 和 here document
- linux shell 下here document 文档
- Shell中使用Expect Here Document
- linux shell的here document用法
- Linux Shell HERE DOCUMENT 重定向
- Here Document
- Here Document
- Here document
- here document
- here document
- Here Document
- Here-Document
- Here document
- shell here document 应用实例--shell控制数据库
- 基于ArcGIS10.0和Oracle10g的空间数据管理平台(C#开发)-AE常用操作代码
- 教大家如何判断CPU是32位还是64位
- linux下如何统计一个目录下的文件个数以及代码总行数的命令
- 记录IT路的点点滴滴
- 教你如何让windows定时关机
- Shell之Here Document
- shell学习笔记二--工具
- 脚本收集
- 网页特效之imageflow
- Unix/Linux Shell编程实战:使用嵌入文档Here Documents
- linux shell 下here document 文档
- kinect 开发笔记
- 常用Shell脚本集
- Ketama 修改支持分布式存储迁移-笨方法