Linux Basics Summary (iii)

Source: Internet
Author: User

Third Week Study Summary catalogue

I. Knowledge context diagram
Two. Shell scripts
Three. Shell Login method
Four. File search and compression and tar
Five. Simple SED text processing

I. Knowledge context diagram

Two. Introduction to shell script 1.shell scripting
            linux下经常会遇到重复输入大量重复命令的问题,如果只是在命令行界面执行,就会显得很繁琐。            而shell脚本就解决了这个问题,他是一个可以经过解释执行的文件,在其中保存我i们要执行的命令,            下次仅仅执行这个脚本就可以了,为我们节省里大量的时间。
2.shell Script Content
          shell脚本是一个需要解释后才能执行的文件,不同于Java,c等语言需要编译器来进行编译,她所需要的            是一个脚本解释器,例如/bin/bash            所以我们要在shell脚本里的第一行写入所需调用的解释器:            通常为:#!/bin/bash             而后加入我们所需执行的命令即可,非常方便,如果会命令就可以写出脚本            例如:            写一个生成脚本的脚本,/root/bin/createsh.sh当执行如/root/bin/createsh.sh  test.sh时,            会自动在/root/bin下新建一个名为test.sh的新文件,            该文件会自动给上执行权限,同时自动在文件头加上shabang及注释信息,注释信息如下:            #!/bin/bash            # ------------------------------------------            # Filename:    test.sh(此处会根据文件名自动更换)            # Revision:    1.0            # Date:        2017-01-22(此处会自动变换为当前日期)            # Author:      Nanyibo            # Email:       [email protected]            # ------------------------------------------            # Copyright:   2017 nanyibo            # License:     GPL            之后,自动使用vim打开,等待用户开始编写脚本正文。

Here's the code:

            #!/bin/bash            touch  /root/bin/$1            chmod +x /root/bin/$1            echo "#!/bin/bash                        # ------------------------------------------                        # Filename:    $1                        # Revision:    1.0                        # Date:        $(date +%F)                        # Author:      xiaoyu                        # Email:       [email protected]                        # ------------------------------------------                        # Copyright:   2018 xiaoyu                        # License:     GPL" > /root/bin/$1            vim /root/bin/$1

Shell Scripting considerations:
        当shell脚本非正常退出时,会生成一个以.swp结尾的后缀文件。

这个swp文件保存着改动信息,当你编辑一个shell脚本时会自动生成,当你编辑完正常退出后,这个文件会自动消失。但是当你非正常退出时,这个文件不会消失,所以当你下次编辑脚本时检测到了此文件就会报错所以他会给你两个提示:第一种是询问你是否在另外一个终端或bash在操作这个文件,或者是有不正常退出的行为。这里的策略有两种:1.直接删除swp文件,但是shell脚本会回到上次编辑的初始状态,你所进行的所有编辑都会丢失2.输入R修复shell脚本,继续编辑,编辑完之后保存退出,再将swp文件删除掉,这样就解决这个问题了。
Three. Shell Login method

There are two types of shell login: Interactive login and non-interactive login

Interactive Login
    1)直接通过终端输入账号密码登录    2)使用“su - username” 切换    在使用交互式登录的过程中,会按照以下的顺序来执行这些配置文件,没有的会加上,有的话会进行覆盖    /etc/profile --> /etc/profile.d/*.sh  --> ~/.bash_profile --> ~/.bashrc --> /etc/bashrc
Non-interactive Login
    1)su username    2)图形界面写打开的终端    3)执行脚本    4)任何其他的bash    在使用交互式登录的过程中,会按照以下的顺序来执行这些配置文件,没有的会加上,有的话会进行覆盖    ~/.bashrc --> --> /etc/bashrc --> /etc/profile.d/*.sh
Small summary
    二者都会执行~/.bashrc --> --> /etc/bashrc --> /etc/profile.d/*.sh这些文件,所以要进行某些用户个性化设置    时,这三个文件需要特别注意。    配置文件分为两类:profile 和 bashrc    profile : 为交互式登录的shell提供配置    全局作用范围的:/etc/profie, /etc/proflile.d/*,sh    个人作用范围的:~/.bash_profile    用于定义环境变量    bashrc:为非交互式和交互式登录的shell提供配置    全局:/etc/bashrc    个人:~/.bashrc    用于定义别名和函数,定义本地变量    修改配置文件后不会自动生效:    需要重新启动shell进程或.或source
Four. File search and compression and tar
    这部分之前已经整理过了,    [文件查找] (http://blog.51cto.com/13572413/2065735)     [文件的压缩和解压缩] (http://blog.51cto.com/13572413/2065805)
Five. Simple SED text processing
    sed 是一种常用的处理文本内容行的方式
How SED commands work
    sed命令通过读取文件将文件的一行放置固定的空间,经过处理后显示在终端屏幕上    而不会改动源文件。
Parameters of the SED command
        -n:不输出内容,不自动打印(sed默认方式为自动打印)        -e:多点编辑        -f:/...../path/filename 从指定文件中读取编辑脚本        -r:支持使用扩展正则表达式        -i.bak
SED address delimitation
        1)默认为全文        2)单地址:                    #:指定的行                    /pattern/:被匹配到的每一行        3)地址范围:                    #,#                    #,+#                    /part1/,/part1/                    #,/part1/        4)~:步进                    1~2 奇数行                    2~2 偶数行

Linux Basics Summary (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.