美文网首页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内不同方式执行脚本

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