博客
关于我
shell知识点:脚本执行方法
阅读量:146 次
发布时间:2019-02-27

本文共 1051 字,大约阅读时间需要 3 分钟。

1. Shell脚本执行

在使用Shell脚本时,确保脚本能够正常执行需要注意以下几点:

  • 脚本可执行性

    • 如果脚本文件没有可执行权限(如文件开头没有#!/bin/bash#!/sh),需要预先为脚本文件赋予可执行权限。可以使用以下命令:
    chmod u+x script-name

    或者使用更宽松的权限设置:

    chmod 755 script-name
    • 可以通过两种方式执行脚本:
    bash script-name   # 或 sh script-name./script-name     # 前提是脚本所在目录是当前工作目录
  • 使用source.执行脚本

    • source script-name. script-name 可将脚本的函数和变量环境传递给当前的shell环境。
    source test.sh   # 或 . test.sh
    • 这两种方法的区别在于source.的兼容性。source支持所有Bash脚本,而.仅适用于Bash脚本。
  • 理解不同执行方式

    • 图1展示了source./script.sh之间的区别。
    • 图2展示了bash script.shsh script.sh之间的差异。
  • 2. Shell脚本后台运行

    在实际应用中,特别是在需要长时间运行脚本的情况下,将脚本后台执行可以节省资源,并允许继续使用终端。

  • 使用nohup

    • nohup可以将输出重定向到文件,并在后台运行脚本。
    nohup sh test.sh > out.txt &
    • out.txt 为输出日志文件。
  • 直接使用sh

    • 也可以直接使用sh命令将脚本后台运行并重定向输出。
    sh test.sh > & out.txt &
  • 3. 脚本后台执行的知识点
    功能 用途
    sh脚本 将脚本后台执行
    Ctrl + c 停止当前脚本任务
    Ctrl + z 暂停当前脚本任务
    bg 将当前脚本后台执行
    fg 将当前脚本前台执行
    • 使用`jobs`可以列出当前后台正在运行的任务。 ```bash [root@localhost scripts]# jobs [1]- 运行中 sh jobs.sh & [2]+ 运行中 sh jobs.sh & ```
    • 使用`fg`可以将后台任务调至前台执行。 ```bash [root@localhost scripts]# fg 1 sh jobs.sh ```
    • 使用`nohub`可以将程序后台运行。 ```bash nohup sh test.sh > out.txt & ```

    转载地址:http://fgvd.baihongyu.com/

    你可能感兴趣的文章
    NLP学习笔记:使用 Python 进行NLTK
    查看>>
    NLP的神经网络训练的新模式
    查看>>
    NLP采用Bert进行简单文本情感分类
    查看>>
    NLP问答系统:使用 Deepset SQUAD 和 SQuAD v2 度量评估
    查看>>
    NLP:使用 SciKit Learn 的文本矢量化方法
    查看>>
    Nmap扫描教程之Nmap基础知识
    查看>>
    Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
    查看>>
    NMAP网络扫描工具的安装与使用
    查看>>
    NMF(非负矩阵分解)
    查看>>
    nmon_x86_64_centos7工具如何使用
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>