zoukankan      html  css  js  c++  java
  • 转:linux执行shell脚本的方式及一些区别

    假设shell脚本文件为hello.sh
    放在/root目录下。下面介绍几种在终端执行shell脚本的方法:

    [root@localhost home]# cd /root/

    [root@localhost ~]#vim hello.sh

    #!  /bin/bash

    cd /tmp

    echo "hello guys!"

    echo "welcome to my Blog:linuxboy.org!"

     

    1.切换到shell脚本所在的目录,执行:

    [root@localhost ~]# ./hello.sh

    -bash: ./ hello.sh: 权限不够(没有加权限)

     

    2.以绝对路径的方式执行:

    [root@localhost ~]# /root/Desktop/hello.sh

    -bash: /root/Desktop/ hello.sh: 权限不够

     

    3.直接用bash或sh执行:(这种方式不需要权限)

    [root@localhost ~]# bash hello.sh

    hello guys!

    welcome to my Blog:linuxboy.org!

    [root@localhost ~]# pwd

    /root

     

    [root@localhost ~]# sh hello.sh

    hello guys!

    welcome to my Blog:linuxboy.org!

    [root@localhost ~]# pwd

    /root

    注意:用以上三种方法执行shell脚本,现行的shell会开启一个子shell环境,去执行shell脚本,前两种必须要有执行权限才能够执行

     

    也可以让shell脚本在现行的shell中执行:

    4.现行的shell中执行

    [root@localhost ~]# . hello.sh

    hello guys!

    welcome to my Blog:linuxboy.org!

    [root@localhost tmp]# pwd

    /tmp

     

     

    [root@localhost ~]# source hello.sh

    hello guys!

    welcome to my Blog:linuxboy.org!

    [root@localhost tmp]# pwd

    /tmp

     

    对于第4种不会创建子进程,而是在父进程中直接执行

    上面的差异是因为子进程不能改变父进程的执行环境,所以CD(内建命令,只有内建命令才可以改变shell 的执行环境)没有成功,但是第4种没有子进程,所以CD成功。

    转自;http://4554480.blog.51cto.com/4544480/837006

    (source命令

    source命令:

    source命令也称为“点命令”,也就是一个点符号(.)。source命令通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录。

    用法: 

    source filename 或 . filename.

    )

    更多:

    http://learn.akae.cn/media/ch31s02.html

  • 相关阅读:
    Codeforces 985G. Team Players
    关于Hall定理的学习
    bzoj 4561: [JLoi2016]圆的异或并
    UOJ #188. 【UR #13】Sanrd
    LOJ #6053. 简单的函数
    Codeforces F. Cowmpany Cowmpensation
    POJ 3710:Matrix Power Series
    codeforces533E
    luogu2885
    codeforces722E
  • 原文地址:https://www.cnblogs.com/youxin/p/3544634.html
Copyright © 2011-2022 走看看