- 浏览: 2964521 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (893)
- android (110)
- iphone (198)
- java (79)
- JavaScript手册-目录 (9)
- JavaScript手册-Array (19)
- JavaScript手册-Boolean (5)
- JavaScript手册-Date (50)
- JavaScript手册-Math (30)
- JavaScript手册-Number (14)
- JavaScript手册-RegExp (7)
- JavaScript手册-String (38)
- JavaScript手册-全局函数 (8)
- JavaScript实用脚本 (7)
- Others (21)
- java-jpcap (7)
- java-thread (1)
- ibm文章 (3)
- classloader (2)
- java-filter (2)
- 运行环境 (33)
- java-正则 (2)
- oracle (1)
- linux-shell (26)
- wap (1)
- sqlite (3)
- wow (1)
- jvm (1)
- git (5)
- unity3d (29)
- iap (2)
- mysql (23)
- nginx (14)
- tomcat (9)
- apache (2)
- php (1)
- ubuntu (40)
- rsa (1)
- golang (21)
- appstore (5)
- sftp (2)
- log4j (2)
- netty (18)
- 测试工具 (6)
- memcache (5)
- 设计模式 (1)
- centos (8)
- google_iab (5)
- iOS专题 (4)
- mac (10)
- 安装配置帮助手册 (2)
- im4java_graphicsmagick (5)
- inotify-tools (1)
- erlang (6)
- 微信支付 (1)
- redis (8)
- RabbitMQ (5)
最新评论
-
heng123:
Netty视频教程https://www.douban.com ...
netty4.0.23 初学的demo -
maotou1988:
使用Netty进行Android与Server端通信实现文字发 ...
netty4.0.23 初学的demo -
码革裹尸:
非常感谢,正好用上
android 呼入电话的监听(来电监听) -
rigou:
提示的/222.177.4.242 无法链接到ip地址,是什 ...
通过 itms:services://? 在线安装ipa ,跨过app-store -
duwanbo:
GridView与数据绑定
来源:http://os.51cto.com/art/201003/186827.htm
Linux shell脚本基础已经被分成好几个部分了,这里对控制流程的内容也就马上讲完了,这是最后一部分关于here document,这里举例稍微有点复杂,我们慢慢来分析这个复杂Linux shell脚本。
6. Here documents
当要将几行文字传递给一个命令时,here documents(译者注:目前还没有见到过对该词适合的翻译)一种不错的方法。对每个脚本写一段帮助性的文字是很有用的,此时如果我们四有那个 here documents就不必用echo函数一行行输出。 一个 "Here document" 以 << 开头,后面接上一个字符串,这个字符串还必须出现在here document的末尾。下面是一个例子,在该例子中,我们对多个文件进行重命名,并且使用here documents打印帮助:
#!/bin/sh
# we have less than 3 arguments. Print the help text:
if [ $# -lt 3 ] ; then
cat <
ren -- renames a number of files using sed regular expressions
USAGE: ren 'regexp' 'replacement' files...
EXAMPLE: rename all *.HTM files in *.html:
ren 'HTM$' 'html' *.HTM
HELP
exit 0
fi
OLD="$1"
NEW="$2"
# The shift command removes one argument from the list of
# command line arguments.
shift
shift
# $* contains now all the files:
for file in $*; do
if [ -f "$file" ] ; then
newfile=`echo "$file" | sed "s/${OLD}/${NEW}/g"`
if [ -f "$newfile" ]; then
echo "ERROR: $newfile exists already"
else
echo "renaming $file to $newfile ..."
mv "$file" "$newfile"
fi
fi
done
这是一个复杂一些的例子。让我们详细讨论一下。第一个if表达式判断输入命令行参数是否小于3个 (特殊变量$# 表示包含参数的个数) 。如果输入参数小于3个,则将帮助文字传递给cat命令,然后由cat命令将其打印在屏幕上。打印帮助文字后程序退出。如果输入参数等于或大于3个,我们 就将第一个参数赋值给变量OLD,第二个参数赋值给变量NEW。下一步,我们使用shift命令将第一个和第二个参数从参数列表中删除,这样原来的第三个 参数就成为参数列表$*的第一个参数。然后我们开始循环,命令行参数列表被一个接一个地被赋值给变量$file。接着我们判断该文件是否存在,如果存在则 通过sed命令搜索和替换来产生新的文件名。然后将反短斜线内命令结果赋值给newfile。这样我们就达到了我们的目的:得到了旧文件名和新文件名。然 后使用mv命令进行重命名。这样就明了这个复杂的Linux shell脚本了吧。
发表评论
-
Linux操作系统中,*.zip、*.tar、*.tar.gz、*.tar.bz2、*.tar.xz、*.jar、*.7z等格式的压缩与解压
2016-07-19 18:30 638Linux操作系统中,*.zip、*.tar、*.ta ... -
Linux shell判断文件是否存在
2014-12-09 16:35 1598Linux shell判断文件是否存在 she ... -
linux备份数据库和配置文件shell脚本
2014-12-09 16:33 827linux备份数据库和配置文件shell脚本 ... -
iptables的4表与5链
2014-12-06 02:09 1619iptables -t nat "-t n ... -
MYSQL删除数据库报 ERROR 1010 (HY000): Error dropping database (can't rmdir ..
2014-12-05 16:04 1372MYSQL删除数据库报 ERROR 1010 (HY0 ... -
常用的linux压缩与解压命令
2014-12-05 00:11 962常用的四个命令如下: 压缩成gzip文件: ... -
linux下用logratate进行日志分割处理
2014-11-21 17:48 4781服务器上的tomcat的catalina.out文件 ... -
linux防火墙iptables
2014-11-18 16:47 933目录 /etc/sysconfig/ 有 ... -
Linux下用SCP无需输入密码获取文件
2010-05-21 09:34 3122通常用scp命令通过ssh获取对方lin ... -
shell判断文件,目录是否存在或者具有权限
2010-05-20 10:40 2180shell判断文件,目录是否存在或者具有权限 #! ... -
Linux 指令篇:使用者管理--su
2010-05-20 10:04 1268名称:su 使用权限:所有使用者 使用方式:su ... -
Linux Top命令参详解
2010-04-21 11:28 1548文章来源:http://blog.chinaunix. ... -
tar linux 命令说明
2010-04-19 13:05 1110tar linux 命令说明 简介 参数: -c ... -
Linux shell脚本基础学习(八)
2010-04-17 19:25 1289来源:http://os.51cto.com/art/2010 ... -
Linux shell脚本基础学习(七)
2010-04-17 19:23 1460来源:http://os.51cto.com/art/2010 ... -
Linux shell脚本基础学习(六)
2010-04-17 19:22 1688来源:http://os.51cto.com/art/2010 ... -
Linux shell脚本基础学习(四)
2010-04-17 19:19 1920来源:http://os.51cto.com/art/2010 ... -
Linux shell脚本基础学习(三)
2010-04-17 19:17 2073来源:http://os.51cto.com/art/2010 ... -
Linux shell脚本基础学习(二)
2010-04-17 19:15 2134Linux shell脚本基础课程前面一讲介绍的都是语法基础的 ... -
Linux shell脚本基础学习(一)
2010-04-17 19:11 1798来源:http://os.51cto.com/ar ...
相关推荐
资源名称:Linux Shell脚本学习基础视频资源目录:【】11a00d99b60c4e2eba3440b8aa3a6bdd【】linux_shell脚本编程_01认识shell,如何编写shell脚本和执行【】linux_shell脚本编程_02vivim简单的常用操作【】linux_...
Linux_shell脚本全面学习 Linux_shell脚本全面学习
Linux-shell脚本全面学习.pdf
介绍了linux shell的常见脚本命令的使用
Linux shell脚本基础学习我们将分几讲来慢慢细说,希望能对想学习Linux shell脚本编程的初学者有一个明确的帮助,水平得到提高。
Linux shell脚本编写基础
详细介绍Linux shell脚本基础学习
Linux shell脚本全面学习
shell学习基本命令,可以帮助初学者快速掌握较全面的shell脚本知识
非常全面的介绍linux下shell脚本的教程,入门级教程,如果你是菜鸟,本文档非常适合你。
LinuxShell脚本编程实例.pdf
Linux Shell脚本编写基础
文件包含大作业shell脚本(快速备份系统)源码,实验课做的Linux实验shell脚本:Samba实验,DDNS(DHCP服务器 和DNS服务器的配置)的shell脚本,运行脚本每个实验只需要两三分钟即可完成。 其中sy1为实验一脚本,sy2...
Linux shell 脚本攻略,学习linux新手入门的较好教材,第二版,现已出版第三版
Linux shell脚本全面学习 1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序。在这个例子中...
学习shell脚本经典书籍《Linux shell 脚本攻略-第二版》全书源代码合集
LinuxShell脚本编程实例.doc
shell脚本学习指南 shell scripting linuxshell脚本攻略