- 浏览: 2966947 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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/186828.htm
4)函数
如果您写了一些稍微复杂一些的程序,您就会发现在程序中可能在几个地方使用了相同的代码,并且您也会发现,如果我们使用了函数,会方便很多。一个函数是这个样子的:
functionname()
{
# inside the body $1 is the first argument given to the function
# $2 the second ...
body
}
您需要在每个程序的开始对函数进行声明。
下面是一个叫做xtitlebar的脚本,使用这个脚本您可以改变终端窗口的名称。
这里使用了一个叫做help的函数。正如您可以看到的那样,这个定义的函数被使用了两次。
#!/bin/sh
# vim: set sw=4 ts=4 et:
help()
{
cat <
xtitlebar -- change the name of an xterm, gnome-terminal or kde konsole
USAGE: xtitlebar [-h] "string_for_titelbar"
OPTIONS: -h help text
EXAMPLE: xtitlebar "cvs"
HELP
exit 0
}
# in case of error or if -h is given we call the function help:
[ -z "$1" ] && help
[ "$1" = "-h" ] && help
# send the escape sequence to change the xterm titelbar:
echo -e "33]0;$107"
#
在脚本中提供帮助是一种很好的编程习惯,这样方便其他用户(和您)使用和理解脚本。
命令行参数
我们已经见过$* 和 $1, $2 ... $9 等特殊变量,这些特殊变量包含了用户从命令行输入的参数。迄今为止,我们仅仅了解了一些简单的命令行语法(比如一些强制性的参数和查看帮助的-h选项)。 但是在编写更复杂的程序时,您可能会发现您需要更多的自定义的选项。通常的惯例是在所有可选的参数之前加一个减号,后面再加上参数值 (比如文件名)
。
有好多方法可以实现对输入参数的分析,但是下面的使用case表达式的例子无遗是一个不错的方法。
#!/bin/sh
help()
{
cat <
This is a generic command line parser demo.
USAGE EXAMPLE: cmdparser -l hello -f -- -somefile1 somefile2
HELP
exit 0
}
while [ -n "$1" ]; do
case $1 in
-h) help;shift 1;; # function help is called
-f) opt_f=1;shift 1;; # variable opt_f is set
-l) opt_l=$2;shift 2;; # -l takes an argument -> shift by 2
--) shift;break;; # end of options
-*) echo "error: no such option $1. -h for help";exit 1;;
*) break;;
esac
done
echo "opt_f is $opt_f"
echo "opt_l is $opt_l"
echo "first arg is $1"
echo "2nd arg is $2"
您可以这样运行该脚本:
cmdparser -l hello -f -- -somefile1 somefile2
返回的结果是:
opt_f is 1
opt_l is hello
first arg is -somefile1
2nd arg is somefile2
这个脚本是如何工作的呢?脚本首先在所有输入命令行参数中进行循环,将输入参数与case表达式进行比较,如果匹配则设置一个变量并且移除该参数。根据unix系统的惯例,首先输入的应该是包含减号的参数。
发表评论
-
Linux操作系统中,*.zip、*.tar、*.tar.gz、*.tar.bz2、*.tar.xz、*.jar、*.7z等格式的压缩与解压
2016-07-19 18:30 645Linux操作系统中,*.zip、*.tar、*.ta ... -
Linux shell判断文件是否存在
2014-12-09 16:35 1607Linux shell判断文件是否存在 she ... -
linux备份数据库和配置文件shell脚本
2014-12-09 16:33 831linux备份数据库和配置文件shell脚本 ... -
iptables的4表与5链
2014-12-06 02:09 1626iptables -t nat "-t n ... -
MYSQL删除数据库报 ERROR 1010 (HY000): Error dropping database (can't rmdir ..
2014-12-05 16:04 1379MYSQL删除数据库报 ERROR 1010 (HY0 ... -
常用的linux压缩与解压命令
2014-12-05 00:11 968常用的四个命令如下: 压缩成gzip文件: ... -
linux下用logratate进行日志分割处理
2014-11-21 17:48 4783服务器上的tomcat的catalina.out文件 ... -
linux防火墙iptables
2014-11-18 16:47 937目录 /etc/sysconfig/ 有 ... -
Linux下用SCP无需输入密码获取文件
2010-05-21 09:34 3124通常用scp命令通过ssh获取对方lin ... -
shell判断文件,目录是否存在或者具有权限
2010-05-20 10:40 2183shell判断文件,目录是否存在或者具有权限 #! ... -
Linux 指令篇:使用者管理--su
2010-05-20 10:04 1270名称:su 使用权限:所有使用者 使用方式:su ... -
Linux Top命令参详解
2010-04-21 11:28 1551文章来源:http://blog.chinaunix. ... -
tar linux 命令说明
2010-04-19 13:05 1112tar linux 命令说明 简介 参数: -c ... -
Linux shell脚本基础学习(八)
2010-04-17 19:25 1290来源:http://os.51cto.com/art/2010 ... -
Linux shell脚本基础学习(七)
2010-04-17 19:23 1461来源:http://os.51cto.com/art/2010 ... -
Linux shell脚本基础学习(五)
2010-04-17 19:20 1712来源:http://os.51cto.com/art/2010 ... -
Linux shell脚本基础学习(四)
2010-04-17 19:19 1922来源:http://os.51cto.com/art/2010 ... -
Linux shell脚本基础学习(三)
2010-04-17 19:17 2075来源:http://os.51cto.com/art/2010 ... -
Linux shell脚本基础学习(二)
2010-04-17 19:15 2138Linux 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脚本攻略