• Shell-基础(一):Shell解释器、Shell脚本


    二、Shell解释器

    1. Shell解析器种类

    Linux提供的Shell解析器有以下几种(linux是开源的,不同的人写了不同风格的解释器):

    • /bin/sh(UNIX默认的shell,已经被/bin/bash取代)
    • /bin/bash(Linux默认的shell)
    • /sbin/nologin
    • /bin/dash
    • /bin/tcsh
    • /bin/csh
    [root@localhost ~]$ cat /etc/shells
    /bin/sh
    /bin/bash
    /sbin/nologin
    /usr/bin/sh
    /usr/bin/bash
    /usr/sbin/nologin
    /bin/tcsh
    /bin/csh
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    不同的shell解析器,功能不同,比如csh,符合c语言风格的shell解析器。Centos默认使用的是“/bin/bash”和“/bin/sh”作为shell解析器。“/bin/sh”是“/bin/bash”的软连接,两者等价。

    2. 查看当前解释器

    通过“echo $SHELL”命令查看当前解释器

    [root@localhost ~]$ echo $SHELL
    /bin/bash
    
    • 1
    • 2

    在终端中输入:cat /etc/shells等价于:/bin/bash -c ‘cat /etc/shells’

    默认/bin/bash必须接收一个脚本作为输入!如果是一条命令,需要加-c (command)
    
    • 1
    [root@localhost ~]$ /bin/bash -c 'cat /etc/shells'
    /bin/sh
    /bin/bash
    /sbin/nologin
    /usr/bin/sh
    /usr/bin/bash
    /usr/sbin/nologin
    /bin/tcsh
    /bin/csh
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3. Shell脚本的编写要求

    1. 声明解释器: #!/bin/bash
    2. 正文: 必须是shell解释器能解释的命令
    [root@localhost /]$ cd home
    [root@localhost home]$ ls
    whx
    [root@localhost home]$ cd whx
    [root@localhost whx]$ ls
    Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
    [root@localhost whx]$ mkdir myshells
    [root@localhost whx]$ ll
    total 0
    drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Desktop
    drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Documents
    drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Downloads
    drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Music
    drwxr-xr-x. 2 root root 6 Oct 28 22:13 myshells
    drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Pictures
    drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Public
    drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Templates
    drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Videos
    [root@localhost whx]$ cd myshells
    [root@localhost myshells]$ ll
    total 0
    [root@localhost myshells]$ vim first.sh
    [root@localhost myshells]$ ll
    total 4
    -rw-r--r--. 1 root root 51 Oct 28 22:15 first.sh
    [root@localhost myshells]$ bash first.sh
    这是shell脚本文件first.sh
    [root@localhost myshells]$ sh first.sh
    这是shell脚本文件first.sh
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    first.sh文件内容为:

    #!/bin/bash
    echo 'first.sh脚本文件执行完毕'
    
    • 1
    • 2

    second.sh文件内容为:

    #!/bin/bash
    b='这是second.sh'
    sleep 10s
    echo $b
    echo 'second.sh脚本文件执行完毕'
    
    • 1
    • 2
    • 3
    • 4
    • 5

    third.sh文件内容为:

    #!/bin/bash
    echo $b
    echo 'third.sh脚本文件执行完毕'
    
    • 1
    • 2
    • 3

    虽然linux系统的文件不需要用后缀,但是一般用后缀.sh表明该文件是一个shell脚本文件,方便识别。

    4. Shell脚本的执行方式

    1. bash 脚本sh 脚本
    [root@localhost myshells]$ bash first.sh
    这是shell脚本文件first.sh
    [root@localhost myshells]$
    
    • 1
    • 2
    • 3
    特点: 
    	①新开一个bash执行脚本;
    	②一旦脚本执行完毕,bash自动关闭!
    
    • 1
    • 2
    • 3

    使用pstree查看进程

            ├─sshd─┬─sshd───bash───bash───sleep
            │      └─sshd───bash───pstree
    
    • 1
    • 2
    1. ./脚本

      特点:
      ①前提是当前用户对脚本有执行权限,使用当前默认的解释器执行脚本;
      ②新开一个bash执行脚本;
      ③一旦脚本执行完毕,bash自动关闭!
      添加权限:chmod u+x first.sh
      删除权限:chmod u-x first.sh

    [root@localhost myshells]$ ll
    total 4
    -rw-r--r--. 1 root root 51 Oct 28 22:15 first.sh
    [root@localhost myshells]$ ./first.sh
    -bash: ./first.sh: Permission denied
    [root@localhost myshells]$ chmod u+x first.sh
    [root@localhost myshells]$ ll
    total 4
    -rwxr--r--. 1 root root 51 Oct 28 22:15 first.sh
    [root@localhost myshells]$ ./first.sh
    这是shell脚本文件first.sh
    [root@localhost myshells]$ 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    使用pstree查看进程

            ├─sshd─┬─sshd───bash───second.sh───sleep
            │      └─sshd───bash───pstree
    
    • 1
    • 2
    1. . 脚本
    [root@localhost myshells]$ . first.sh
    这是shell脚本文件first.sh
    [root@localhost myshells]$ 
    
    • 1
    • 2
    • 3
    特点: 
    	①使用当前默认的解释器执行脚本,并不要求当前用户对脚本有执行权限;
    	②使用当前默认的解释器执行脚本
    
    • 1
    • 2
    • 3

    使用pstree查看进程

    ├─sshd─┬─sshd───bash───sleep
    │      └─sshd───bash───pstree
    
    • 1
    • 2
    1. source 脚本
    [root@localhost myshells]$ source first.sh
    这是shell脚本文件first.sh
    [root@localhost myshells]$ 
    
    • 1
    • 2
    • 3
    特点: 
    	①使用当前默认的解释器执行脚本,并不要求当前用户对脚本有执行权限;	
    	②使用当前默认的解释器执行脚本
    
    • 1
    • 2
    • 3

    使用pstree查看进程

    ├─sshd─┬─sshd───bash───sleep
    │      └─sshd───bash───pstree
    
    • 1
    • 2
  • 相关阅读:
    数组扁平化(es6)
    计算机毕业设计Java酒店管理系统设计与实现(源码+系统+mysql数据库+lw文档)
    微信小程序注册指引
    聊聊微服务架构思想
    【爬虫--必须要了解的请求头 user-agent】
    MultiBank Group宣布在阿联酋和新加坡取得两项新牌照
    ArcGIS:如何简单地制作一幅专题地图?
    python flask 入门-helloworld
    【GPU】Nvidia CUDA 编程高级教程——利用蒙特卡罗法求解近似值(MPI方法)
    rsync远程同步
  • 原文地址:https://blog.csdn.net/u013250861/article/details/126925088