shell脚本由基础变量及特殊变量($@、$*、$#等)到实战。
一、shell脚本建立:
shell脚本通常是在编辑器(如vi/vim)中编写,也可以在命令行中直接执行;
1、脚本开头:
规范的脚本第一行需要指出有哪个程序(解释器)来执行脚本中的内容,在Linux中一般为:
#!/bin/sh
或者
#!/bin/bash
“#!”,在执行脚本时,内核会根据“#!/bin/sh”来确定使用bash程序来解释脚本,这行必须在脚本顶端(第一行),如果非第一行则表示注释。
如果不是使用“#!/bin/sh”而是使用其他的例如:“# !/usr/bin/env python” 则表示使用python来解释程序。
2、脚本注释:
在shell中常常会有解释说明脚本所需要实现的功能,或者其他信息,那么久需要对脚 本进行注释说明:如何注释说明呢?
在注释的内容前面增加“#”则可以表示后面内容为注释。如果没有注释,非脚本开发人 员很难理解脚本的实现功能,而且时间长了即使是脚本开发则也可能忘记脚本所实现的功 能。因此良好的习惯在于书写注释,方便别人也方便自己。
二、变量:
在所有编程中都会涉及到变量。那么在shell中如何定义是使用变量呢?
1、直接定义变量内容:
例、ip=10.1.1.1
ip=10.1.1.1-$ip
这种情况下,变量的内容一般为简单的连续的数字,字符串,路径名。那么这个ip输出的 值是多少呢?以下是测试情况。
[root@ipv6-- init.d]# ip=10.1.1.1
[root@ipv6-- init.d]# ip=10.1.1.1-$ip
[root@ipv6-- init.d]# echo $ip
10.1.1.1-10.1.1.1
2、通过单引号来定义变量,此种方式特点是:输出变量是单引号中是什么就输出什么,即使 引号中的内容有变量也会直接把变量原样输出。此法为定义纯字符串变量。
[root@ipv6-- init.d]# ip=10.1.1.1
[root@ipv6-- init.d]# ip='10.1.1.1-$ip'
[root@ipv6-- init.d]# echo $ip
10.1.1.1-$ip
3、双引号定义变量,此种方法特点:输出变量是引号内有变量会经过解析后输出变量内容, 而不是原样输出。此法实用于字符串中附带有变量内容的定义。
[root@ipv6-- init.d]# ip=10.1.1.1
[root@ipv6-- init.d]# ip="10.1.1.1-$ip"
[root@ipv6-- init.d]# echo $ip
10.1.1.1-10.1.1.1
由上可以看出,无引号和双引号实现的功能基本类似,实现功能所差无几,但是实际中最好使用双引号来对含有变量变量的进行定义。
4、常用的把一个命令作为变量:
使用反引号。
[root@ipv6-- home]# cmd=`ls -l`
[root@ipv6-- home]# echo $cmd
total drwx------. tpl tpl Mar : tpl drwx------. xguest xguest Jun xguest
三、特殊标量:

具体测试:
$0使用:
[root@localhost ~]# cat /server/script/echo.sh
#!/bin/sh
echo "hello!"
echo $
[root@localhost ~]# sh /server/script/echo.sh
hello!
/server/script/echo.sh
$0
$n使用:
以下是部分脚本:
以上内容省略
echo $
[root@ipv6-- script]# sh tomcatd.sh status
tomcat is running. [ OK ]
status
$*
[root@localhost script]# cat for.sh
for i in "$*";do echo $i;done
[root@localhost script]# sh for.sh "you are" right
you are right
$#
#!/bin/sh
. /etc/init.d/functions
function status(){
if [ `ps -ef |grep java |grep -v grep|wc -l` -gt ]
then
action "tomcat is running." /bin/true
else
action "tomcat is stopped." /bin/false
exit
fi
}
case $ in
status)
status
;; *)
echo "USAG:start|stop|restart|status"
esac
echo $#
[root@ipv6-- script]# sh $*.sh status
tomcat is running. [ OK ]
#表示一个参数
$@
[root@localhost script]# cat for.sh
for i in "$@";do echo $i;done
[root@localhost script]# sh for.sh "you are" right
you are
right
$$
[root@localhost script]# sh for.sh "you are" right
you are
right
[root@localhost script]# echo $$
$!
[root@localhost script]# sh for.sh asfasdf &
[]
[root@localhost script]# asfasdf []+ Done sh for.sh asfasdf
[root@localhost script]# echo $!
$?
[root@localhost script]# echo "hello"
hello
[root@localhost script]# echo $?
$_
[root@ipv6-- script]# read a b c [root@ipv6-- script]# echo $_
c
随机推荐
- SSL协议、HTTP和HTTPS和区别
SSL协议 SLL协议的握手过程 开始加密通信之前,客户端和服务器首先必须建立连接和交换参数,这个过程叫做握手(handshake). 第一步,客户端给出协议版本号.一个客户端生成的随机数(Clien ...
- html5--5-9 绘制扇形
html5--5-9 绘制扇形 学习要点 综合运用已经学过的知识绘制一个扇形 矩形的绘制方法 rect(x,y,w,h)创建一个矩形 strokeRect(x,y,w,hx,y,w,h) 绘制矩形(无 ...
- hdu-5738 Eureka(组合计数+极角排序)
题目链接: Eureka Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Pr ...
- ubuntu 下编译安装ceph
git clone --recursive https://github.com/ceph/ceph.git cd ceph/ sudo apt-get install libtool sud ...
- luogu 3389 【模板】高斯消元
大概就是对每一行先找到最大的减小误差,然后代入消元 #include<iostream> #include<cstdio> #include<cstring> #i ...
- C++之运算符重载(前置++和后置++)
今天在阅读<google c++ 编程风格>的文档的时候,5.10. 前置自增和自减:有一句话引起了我的注意: 对于迭代器和其他模板对象使用前缀形式 (++i) 的自增, 自减运算符.,理 ...
- poj 2069 Super Star —— 模拟退火
题目:http://poj.org/problem?id=2069 仍是随机地模拟退火,然而却WA了: 看看网上的题解,都是另一种做法——向距离最远的点靠近: 于是也改成那样,竟然真的A了...感觉这 ...
- 微信小程序的ajax数据请求wx.request
微信小程序的ajax数据请求,很多同学找不到api在哪个位置,这里单独把小程序的ajax请求给列出来,微信小程序的请求就是wx.request这个api,wx.request(一些对象参数),微信小程 ...
- heartbeat3.x部署安装
使用Heartbeat构建Linux双机热备系统 本文档版本号: V1.0 版 本 历 史 版本号 更新时间 说 明 创建者 V1.0 2013-3-23 修改版 金桥 1 部署环境 OS: Red ...
- 注销ie中的ActiveX插件
最新在C#下开发ActiveX控件,遇到一个问题,就是在调试的时候,ActiveX就已经注册在了调试目录下,这样即使安装这个插件,也无法注册到ActiveX的安装目录下.为了解决这个问题,需要注销下调 ...