编程测试 . test.sh是否是在当前shell当中执行

. test.sh 
注意,中间有个空格

本次测试主要有三个sh脚本
1、test01.sh

#!/bin/bash
echo $0

ENV_PATH=$(cd $(dirname $0); pwd)
echo $ENV_PATH
echo "test1:" $$
echo "ppid:" $PPID
. $ENV_PATH/test02.sh
$ENV_PATH/test03.sh

    说明: $0表示当前执行的命令,所以后面的结果为 "./test01.sh " 如果使用绝对路径执行的,则为"/tmp/sh/test01.sh"
                $$为执行当前shell脚本所开启的shell的进程id, $PPID为执行test01.sh脚本的shell进程id
2、test02.sh

#!/bin/bash

echo "hello everyone"
echo $$
echo "ppid:" $PPID

说明:同上
3、test03.sh

#!/bin/bash

echo "test03 files"
echo "test3:" $$
echo "ppid:" $PPID

说明:同上
执行的脚本为:

cd /tmp/sh
./test01.sh


输出的结果为:

./test01.sh
/tmp/sh
test1: 32052
ppid: 26836
hello everyone
32052
ppid: 26836
test03 files
test3: 32056
ppid: 32052

说明:每个分块就是一个shell脚本执行的结果
          第一个ppid为26836, 执行test01.sh脚本的父shell就是它,当前运行test01.sh的shell是26836父shell下的子shell,进程pid为32052
          第二个ppid为26836,运行test02.sh脚本的shell是26836父shell下的子shell,也就是32052,说明 . /test02.sh 是在当前shell下运行的
          第三个ppid为32052,为运行test01.sh脚本的shell,在这个基础之上又开启了一个shell,shell的pid为32056.

综合说明: . test02.sh 确实为当前shell所执行的脚本。


猜你喜欢

转载自blog.csdn.net/u012225679/article/details/78900896