博客园连接:
在linux里,source、sh、bash、./都可以执行shell script文件,那它们有什么不同吗?
博客园连接
个人认为source、sh、bash,. 这几个都是将文件当sh文件来执行的,如果是py文件乃至其他文件那,那么他将不能识别(linux中需要在第一行申明文件执行的程序)
直接将文件绝对路径放到命令行或者./xxxxx放到命令行,也会执行,执行是按照你申明的程序来执行的
source 在当前shell内去读取
sh 都是打开一个subshell去读取(子shell)
bash 都是打开一个subshell去读取(子shell)
./ 打开一个subshell去读取
sh文件的执行
[root@iZbp11vz1brexya9wf6w6eZ home]# ll
total 8
drwx------ 2 weihang weihang 4096 Jul 16 21:17 weihang
drwxr-xr-x 2 root root 4096 Jul 5 22:23 weihang2
[root@iZbp11vz1brexya9wf6w6eZ home]# cat /home/weihang/test.sh
#!/usr/bin/bash
echo "11111111111111111111111111111111111"
[root@iZbp11vz1brexya9wf6w6eZ home]# ./weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# ./weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# /weihang/test.sh
-bash: /weihang/test.sh: No such file or directory
[root@iZbp11vz1brexya9wf6w6eZ home]# ./weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# source /weihang/test.sh
-bash: /weihang/test.sh: No such file or directory
[root@iZbp11vz1brexya9wf6w6eZ home]# source /home/weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# . /home/weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# vim /home/weihang/test.sh
[root@iZbp11vz1brexya9wf6w6eZ home]# cat /home/weihang/test.sh
echo "11111111111111111111111111111111111"
[root@iZbp11vz1brexya9wf6w6eZ home]#
[root@iZbp11vz1brexya9wf6w6eZ home]# . /home/weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# source /home/weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# ./home/weihang/test.sh
-bash: ./home/weihang/test.sh: No such file or directory
[root@iZbp11vz1brexya9wf6w6eZ home]# sh /home/weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# bash /home/weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# `/home/weihang/test.sh`
-bash: 11111111111111111111111111111111111: command not found
[root@iZbp11vz1brexya9wf6w6eZ home]#
py文件的执行
[root@iZbp11vz1brexya9wf6w6eZ home]# cat weihang/pytest.py
#!/usr/bin/python
print "sssssssssssssssssss"
a = [1,2,3,4,5]
print a
[root@iZbp11vz1brexya9wf6w6eZ home]#
-rwxrwxrwx 1 root root 70 Jul 16 21:44 pytest.py
-rwxrwxrwx 1 root root 44 Jul 16 21:33 test.sh
[root@iZbp11vz1brexya9wf6w6eZ weihang]# sh pytest.py
pytest.py: line 2: print: command not found
pytest.py: line 3: a: command not found
pytest.py: line 4: print: command not found
[root@iZbp11vz1brexya9wf6w6eZ weihang]# bash pytest.py
pytest.py: line 2: print: command not found
pytest.py: line 3: a: command not found
pytest.py: line 4: print: command not found
[root@iZbp11vz1brexya9wf6w6eZ weihang]# ./pytest.py
sssssssssssssssssss
[1, 2, 3, 4, 5]
[root@iZbp11vz1brexya9wf6w6eZ weihang]# . pytest.py
-bash: print: command not found
-bash: a: command not found
-bash: print: command not found
[root@iZbp11vz1brexya9wf6w6eZ weihang]# . pytest.py
-bash: print: command not found
-bash: a: command not found
-bash: print: command not found
[root@iZbp11vz1brexya9wf6w6eZ weihang]# source pytest.py
-bash: print: command not found
-bash: a: command not found
-bash: print: command not found
[root@iZbp11vz1brexya9wf6w6eZ weihang]# python pytest.py
sssssssssssssssssss
[1, 2, 3, 4, 5]
[root@iZbp11vz1brexya9wf6w6eZ weihang]# cd ..
[root@iZbp11vz1brexya9wf6w6eZ home]# ll
total 8
drwx------ 2 weihang weihang 4096 Jul 16 21:44 weihang
drwxr-xr-x 2 root root 4096 Jul 5 22:23 weihang2
[root@iZbp11vz1brexya9wf6w6eZ home]# pwd
/home
[root@iZbp11vz1brexya9wf6w6eZ home]# /home/weihang/pytest.py
sssssssssssssssssss
[1, 2, 3, 4, 5]
[root@iZbp11vz1brexya9wf6w6eZ home]# ./home/weihang/pytest.py
-bash: ./home/weihang/pytest.py: No such file or directory
[root@iZbp11vz1brexya9wf6w6eZ home]# pwd
/home
[root@iZbp11vz1brexya9wf6w6eZ home]# /weihang/pytest.py
-bash: /weihang/pytest.py: No such file or directory
[root@iZbp11vz1brexya9wf6w6eZ home]# ./weihang/pytest.py
sssssssssssssssssss
[1, 2, 3, 4, 5]
[root@iZbp11vz1brexya9wf6w6eZ home]#
[root@iZbp11vz1brexya9wf6w6eZ weihang]# a=`/home/weihang/pytest.py`
[root@iZbp11vz1brexya9wf6w6eZ weihang]# echo ${a}
sssssssssssssssssss [1, 2, 3, 4, 5]
[root@iZbp11vz1brexya9wf6w6eZ weihang]#