zoukankan      html  css  js  c++  java
  • Linux

    1. 概述

      1. shell 的执行方式
    2. 背景

      1. 偶尔执行个 shell 脚本
        1. 一般都用 './script' 执行
      2. 最近忽然看到 有不同的执行方式, 感觉有必要整理一下, 然后和大家分享
    3. 准备

      1. os

        1. centos7
      2. shell

        1. bash

    1. shell 脚本的普通执行

    1. shell 脚本执行

      1. 最常用的执行方式

        > cd <path/to/scriptDir>
        > ./<script> [option and args]
        
    2. 其实 shell 脚本, 还有 其他的 执行方式

    2. 其他的执行方式

    1. 普通方式

      > cd <path/to/scriptDir>
      > ./<script> [option and args]
      
    2. source

      # 纳尼? source 也是用来执行脚本的?
      > cd <path/to/scriptDir>
      > source <script> [option and args]
      
    3. 普通加 &

      > cd <path/to/scriptDir>
      > ./<script> [option and args] &
      

    3. 场景: 设置环境变量

    1. 一般的变量

      1. 只能在 当前 shell 里生效
        1. 其他 shell 里无法生效
    2. 环境变量

      1. 可以在 当前 shell 和 子 shell 里生效
    3. 设置环境变量

      1. 命令

        # 方法1: 设置变量后, 直接 export
        > foo=bar
        > export foo
        # 方法2: 在 ~/.bash_profile 或者 /etc/profile 里添加, 然后 source 配置文件
        
      2. 方法2 的疑问

        1. 疑问1: source 不是执行 shell 脚本的吗?

          1. 解答
            1. 没错, 这些配置文件, 本质上就是 shell 脚本
        2. 疑问2: 为啥要用 source, 直接 ./不行吗?

          1. 卧槽, 可以试试啊...

    4. 实验: 配置 环境变量

    1. 场景
      1. 配置环境变量

        1. 脚本

          #!/bin/bash
          # demo.sh
          foo=bar
          export foo
          
      2. 验证变量是否生效

        1. 命令

          > env | grep foo
          

    1. 方案1: source

    1. 命令

      > source demo.sh
      
    2. 结果

      1. 没有问题

    2. 方案2: ./

    1. 命令

      > ./demo.sh
      
    2. 结果

      1. 发现检测不到 foo 的值

    3. 方案3; ./ &

    1. 命令

      > ./demo.sh &
      
    2. 结果

      1. 发现检测不到 foo 的值

    4. 疑问: 为啥只有 方法1 可以呢?

    4. 再次尝试: 是不是环境变量没有设置上啊

    1. 疑问

      1. 方法2 真的设置好变量了吗
    2. 脚本

      #!/bin/bash
      foo=bar
      export foo
      env | grep foo
      
    3. 结果

      1. 脚本正常运行

        1. 运行完成
        2. 显示了 foo=bar
      2. 然后, 我再次尝试寻找环境变量

        1. 还是没有

          > env | grep foo
          
    4. 疑问

      1. 这次确实设置上来, 为啥还是没有呢?

    5. 环境变量

    1. 一般变量

      1. 作用域
        1. 只能在当前 shell 中使用
        2. 其他 shell 都不行
    2. 环境变量

      1. 作用域

        1. 当前 shell 和 子shell
      2. 回想一下

        1. 在 当前shell 配置了环境变量, 你重新开一个 ssh 是不是用不了这个变量?
    3. 用户变量

      1. 作用域

        1. 以 特定用户身份 登录的所有 shell
      2. 配置

        1. 用户目录的 .bash_profile 文件
          1. 好像 .bashrc 也有
            1. 废话, bashrc 会被 bash_profile 执行
      3. 机制

        1. 以 用户身份启动 shell 前, 会执行这个脚本
    4. 初始变量

      1. 作用域

        1. 所有 shell
      2. 配置

        1. /etc/bash_profile
      3. 机制

        1. 启动 shell 前, 会执行这个脚本
    5. 疑问

      1. 既然环境变量是 当前 shell 和 子 shell
      2. 那会不会是 shell 的关系?

    6. shell 执行

    1. 执行方式

      1. source <script>
      2. ./<script>
      3. ./<scirpt> &
    2. 区别

      1. 他们最主要区别, 是 shell 的区别

    1. source <script>

    1. shell
      1. 使用 当前shell 执行
        1. 当前 shell 的 stdin, stdout, stderr 占用 console

    2. ./<script>

    1. shell
      1. 使用 当前shell 的 子shell 执行

        1. 当前 shell 直接挂机
        2. 子shell 接管 console
          1. stdin, stdout, stderr
      2. bash <script> 跟这个一样, 就不单独列出来了

    3. ./<scirpt> &

    1. shell
      1. 使用 当前shell 的 子shell 执行
        1. 当前 shell 继续占用 console
        2. 子 shell 在后台执行
          1. 会返回一个 job 的编号

    7. 结合之前的环境变量, 大概可以做出如下的推理

    1. source 方法生效

      1. source 给当前 shell 配置了 环境变量
      2. 配置成功了后, 直接找到, 没有问题
    2. ./<script> 不生效

      1. 用 script 执行命令后, 会首先生成 子shell
      2. 命令在 子shell 中, 配置了一个 环境变量
      3. 执行结束, 控制权返回 父shell
      4. 可是环境变量在 父shell 里不生效
    3. ./<script> & 不生效

      1. 情况同上面的 类似

    ps

    1. ref

      1. 学习 bash
      2. Linux - Shell - 变量简介
    2. &

      1. 这玩意是什么鬼
      2. 相关的 job 命令, 又是什么鬼
    尽量尝试解释清楚; 自己校对能力有限, 如果有错误欢迎指出
  • 相关阅读:
    CF666E. Forensic Examination
    bzoj1396 识别子串
    bzoj2839 集合计数
    unknown
    Hibernate中一级缓存和二级缓存
    亲, 我们来再重申一遍"=="和"equals的区别
    BigDecimal类
    序列化详解
    利用简单的参数传递来实现单条查询的easyui-datagrid
    Oracl 动态执行表不可访问,本会话的自动统计被禁止
  • 原文地址:https://www.cnblogs.com/xy14/p/12041127.html
Copyright © 2011-2022 走看看