一、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

  

随机推荐

  1. C# GDI生成清晰【高质量】图片

    对于GDI+,在正常的操作,Bitmap,Graphcis,DrawImage或者DrawString ,生成图片的话,会产生很多杂点,或者是图片质量不稳定.尤其是在读取图片后,生成缩略图之后,文件会 ...

  2. hdu-5761 Rower Bo(数学)

    题目链接: Rower Bo Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others ...

  3. 转,如果linux不能用yum安装asterisk时,可以库参照以下办法添加asterisk仓库

    LinuxCentOSRedHat Installing a binary distribution of Asterisk makes it easier to maintain your syst ...

  4. Tensorflow基础知识

    基本知识 使用 TensorFlow, 你必须明白 TensorFlow: 使用图 (graph) 来表示计算任务. 在被称之为 会话 (Session) 的上下文 (context) 中执行图. 使 ...

  5. hdu 3932 Groundhog Build Home —— 模拟退火

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3932 找一个位置使距离最远的点的距离最小: 上模拟退火: 每次向距离最远的点移动,注意判断一下距离最远的点 ...

  6. 八、MyEclipse多次重装、删除注册表、重装系统激活都不成功,终极解决方法 - imsoft.cnblogs

    MyEclipse(2010,2014)激活不成功的结论: [问题原因]激活不成功时,主要是激活的密钥文件.myeclipse.properties不在指定的位置.(一般都在D.E.F.G等盘符根目录 ...

  7. 解决warning: LF will be replaced by CRLF in **(filename)

    使用Windows的Git使用 git add 时出现warning: LF will be replaced by CRLF in **(filename) 原因: CRLF -- Carriage ...

  8. Lecture0 -- Introduction&&Linear Regression with One Variable

    Introduction What is machine learning? Tom Mitchell provides a more modern definition: "A compu ...

  9. 读取关联数据(EF Core2.1.1)

    对象-关系映射框架比如EF有三种 方式使用 模型中的导航属性来加载关联数据. 一..Lazy Loading.(关联数据在访问导航属性时被透明的加载,不需要特别的代码,自动的加载) 当一个实体第一次读 ...

  10. CF-805B

    B. 3-palindrome time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...