美文网首页linux tools
linux shell内不同方式执行脚本

linux shell内不同方式执行脚本

作者: 疾飞 | 来源:发表于2019-05-31 11:04 被阅读1次
在linux里,source、sh、bash、./都可以执行script文件,以我目前使用的信息来看,有一些区别

1.source
在当前shell内去读取、执行scripe,script不需要有x权限
同时source script可以简写为. script

root@VM:~# cat script
date
root@VM:~# ll script
-rw-r--r-- 1 root root 5 May 31 04:59 script
root@VM:~# source script
Fri May 31 05:00:49 EEST 2019
root@VM:~# . script
Fri May 31 05:00:52 EEST 2019

2.sh/bash
实际是在当前shell内再创建一个shell,专业名为subshell,发现有bash是sh的超集,sh是UNIX下的,bash改编扩展了,某些脚本不通用,所以建议一律使用#!/bin/bash

root@VM:~# ll script_*
-rw-r--r-- 1 root root 26 May 31 05:47 script_1
-rw-r--r-- 1 root root 10 May 31 05:43 script_2
root@VM:~# cat script_*
ping 127.1 >> /root/out

sleep 100
root@VM:~# sh script_1 & ###subshell
[1] 8021
root@VM:~# bash script_2 & ###subshell_2
[2] 8024
root@VM:~# pstree -l | grep -A3 bash
        |-sshd---sshd---bash-+-bash---sleep
        |                    |-grep
        |                    |-pstree
        |                    `-sh---ping

3../script要求script必须有x权限,也是创建了subshell

root@VM:~# ll script_1
-rw-r--r-- 1 root root 26 May 31 05:47 script_1
root@VM:~# ./script_1
-bash: ./script_1: Permission denied
root@VM:~# chmod +x script_1
root@VM:~# ./script_1 &
[2] 8079
root@VM:~# pstree -l | grep -A3 bash
        |-sshd---sshd---bash-+-2*[bash---ping]
        |                    |-grep
        |                    `-pstree
        |-systemd---(sd-pam)
VIA:

linux里source、sh、bash、./有什么区别
shell与subshell与执行脚本的几种方式

相关文章

  • linux shell内不同方式执行脚本

    在linux里,source、sh、bash、./都可以执行script文件,以我目前使用的信息来看,有一些区别 ...

  • [Linux]Shell

    shell:命令解释器,驱动linux内核;应用程序调用shell命令 1.Shell脚本的执行方式 脚本格式要求...

  • shell脚本运行的方式

    Linux中Shell脚本的执行通常有4种方式,分别为:工作目录执行,绝对路径执行,sh执行,Shell环境执行。...

  • 6、shell脚本执行方式

    在Linux中shell脚本的执行方式有以下三种,其执行方式与区别如下: 直接执行脚本,即 路径/script.s...

  • shell脚本4种执行方式

    Linux中shell脚本的执行通常有4种方式,分别为工作目录执行,绝对路径执行,sh执行,shell环境执行。脚...

  • 【Linux shell学习笔记-02-特殊参数变量】

    在Linux shell中,在命令行执行shell脚本时,通过用空格间隔不同的值作为参数传递到脚本中执行相应的计算...

  • shell基础

    shell脚本 1.shell脚本执行方式 1.1 添加执行权 1.2 指定shell命令 2.shell变量 2...

  • 使用 SHC 加密 Shell 脚本

    如何在Linux环境中加密shell脚本?shell脚本包含密码,不希望其他具有执行权限的人查看shell脚本并获...

  • 17. Interview-Linux

    1 用过哪些Linux命令? 2 写过shell脚本吗?shell脚本基本格式? 3 Linux I/O读写方式 ...

  • 如何执行Shell脚本

    通常执行shell脚本有两种方式。以脚本/data/shell/test.sh为例: 当前目录的方式执行,进入脚本...

网友评论

    本文标题:linux shell内不同方式执行脚本

    本文链接:https://www.haomeiwen.com/subject/dtaitctx.html