zoukankan      html  css  js  c++  java
  • 菜鸟的《Linux程序设计》学习—shell script

    1. 认识shell script

    shell script是利用shell的功能缩写的一个“程序”,这个程序是使用纯文本文件,将一些shell的语法与命令(含外部命令)写在里面,搭配正则表达式,管道命令与数据流重定向等功能,以达到我们想要的处理目的。

    shell script有很广泛的应用:
    (1)自动化管理的重要依据
    (2)追踪与管理系统的重要工作
    (3)简单入侵检测功能
    (4)连续命令单一化
    (5)简易的数据处理
    (6)支持跨平台

    所以说,shell script用在系统管理上面是很好的一项工具,但是其也有一定的缺点,在处理大量的数值计算时,速度较慢,占用CPU资源较多,会造成主机资源分配不良。

    2. shell script程序编写

    对于shell script程序的编写,使用编辑工具vim,这是一个很好用的文本编辑命令。
    首先,使用bash命令,创建一个脚本程序
    [yr@localhost shellscript]$ vim helloworld.sh
    
    //在打开的文本中,编辑程序,helloworld.sh
    
    #!/bin/bash
    #Program:
    #       This is my first shell script program. It will show "Hello World!" on
    #       the screen.
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    echo -e "Hello World! a 
    "
    exit 0
    
    程序内容解析:
    (1)第一行 #!/bin/bash 是声明这个script所使用的shell名称,每一个shell script脚本内容,必须在第一行进行对此声明。也就是说,我们使用的是bash命令,必须声明这个文件,来使用bash的语法;
    (2)除了第一行外,以#开头的语句均是批注的作用,也就是类似于普通C程序中的注释;
    (3)主要环境变量的声明,鸟哥建议,PATH与LANG是最重要的,如有使用,建议务必将一些重要的环境变量设置好,这样可以让程序进行时,直接执行外部命令,不必写绝对路径;
    (4)主要程序内容,在本文件中只有echo这一行;
    (5)告知执行结果,利用exit命令,让程序中断,并回传一个数值给系统,0值代表成功,非0值则是代表错误;

    3. 程序的编译运行

    对于shell script有两种运行方式:
    (1)使用 sh命令执行,或者使用chmod改变文件权限,直接用./helloworld.sh 执行 —— 子进程执行
    [yr@localhost shellscript]$ sh helloworld.sh 
    Hello World!  
    [yr@localhost shellscript]$ chmod +x helloworld.sh 
    [yr@localhost shellscript]$ ./helloworld.sh 
    Hello World!
    (2)使用source filename 或者. filename 执行—— 父进程执行
    这儿有个问题需要说明,当程序中有exit 0 这个结束命令时,我们使用source filename 或者. filename 来执行程序时,会出现终端一闪终止的状况,这并不代表程序没有成功运行,而是由于exit 0 这个命令,使得程序运行结束后,结束了父进程,也就是结束了我们的终端。所以要想继续留在该终端界面,我们可以将程序内容的最后一行exit 0删除,再次运行即可。
    [yr@localhost shellscript]$ vim helloworld.sh
    
    //在打开的文本中,编辑程序,helloworld.sh
    
    #!/bin/bash
    #Program:
    #       This is my first shell script program. It will show "Hello World!" on
    #       the screen.
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    echo -e "Hello World! a 
    "
    # 删除最后一行 exit 0
    下面执行命令:
    [yr@localhost shellscript]$ source helloworld.sh 
    
    Hello World!  
    
    <pre code_snippet_id="666753" snippet_file_name="blog_20150514_3_8535676" name="code" class="cpp">[yr@localhost shellscript]$ . helloworld.sh 
    
    Hello World!  

    
    

    我们看到,以上两种方式,都可以顺利成功的将脚本和helloworld.sh执行完毕。

    4. 两种编译方式详解

    (1)利用直接执行的方式来执行脚本:在子进程中运行
    (2)使用source来执行脚本:在父进程中执行
    下面用一个实际例子详细说明:
    我们新建一个新的脚本test.sh
    [yr@localhost shellscript]$ vim test.sh
    
    //下面是程序内容
    #!/bin/bash
    #Program
    #       User inputs his first name and last name. Program shows his full name.
    #History:
    # 2015/05/14 shine_yr First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:usr/local/bin:usr/local/sbin:~/bin
    export PATH
    
    read -p "Please input your first name: "firstname #提示用户输入
    read -p "Please input your last name: "lastname #提示用户输入
    echo -e "
    Your full name is: $firstname $lastname" #结果在屏幕输出
    
    
    # exit 0 我们不用退出命令
    首先,利用直接执行的方式来执行脚本:
    [yr@localhost shellscript]$ sh test.sh
    Please input your first name: shine
    Please input your last name: yr
    
    Your full name is: shine yr
    [yr@localhost shellscript]$ echo $firstname
    
    [yr@localhost shellscript]$ echo $lastname
    
    [yr@localhost shellscript]$ 
    
    
    从上面可以看出,程序顺利执行,然后我利用echo命令打算输出firstname以及lastname的内容,结果却输出为空。
    这是什么原因呢?
    这就是说此方式在当子进程完成后,子进程中的各项变量或者操作都将会结束而不会传回到父进程中,如下图所示:

    然后,使用source filename 或者 . filename的方式执行脚本:
    它是在当前bash环境下读取并执行filename中的命令,而且该命令通常用 . filename来替代。

    [yr@localhost shellscript]$ source test.sh
    Please input your first name: shine
    Please input your last name: yr
    
    Your full name is: shine yr
    [yr@localhost shellscript]$ echo $firstname
    shine
    [yr@localhost shellscript]$ echo $lastname
    yr
    [yr@localhost shellscript]$ 
    我们可以看到,此时变量firstname以及lastname 中是有确切值的。
    也就是说,此时test.sh会在父进程中执行,因此各项操作都会在原本的bash中生效。


    5. 编写shell script的良好习惯

    在头文件处记好:
    (1)script的功能
    (2)script的版本信息
    (3)script的作者与联络方式
    (4)script的版权声明方式
    (5)script的History(历史记录)
    (6)script内较特殊的命令,使用“绝对路径”的方式来执行
    (7)script执行时需要的环境变量预先声明与设置

    除此之外,我们还应该习惯用[Tab]键来控制格式,另外,使用vim而不是vi作为编写工具,因为vim的功能更强大,不仅有颜色提示,还有语法检测功能。
  • 相关阅读:
    Subsets II
    Pow(x, n)
    基本数据结构 —— 二叉搜索树(C++实现)
    基本数据结构 —— 堆以及堆排序(C++实现)
    Symmetric Tree
    Same Tree
    C++中三种传递参数方法的效率分析
    Word Search
    Subsets
    Combinations
  • 原文地址:https://www.cnblogs.com/shine-yr/p/5214954.html
Copyright © 2011-2022 走看看