shell基础:脚本执行方式

版权声明:转载请注明出处 https://blog.csdn.net/fastlearn/article/details/77751902

假设shell脚本在/tmp/test.sh下并且有执行权限
方式一:以相对路径的方式执行脚本

[root@rgl tmp]# cd /tmp/
[root@rgl tmp]# ./test.sh 
hello shell
[root@rgl tmp]# 

../的意思是说在当前的工作目录下执行hello.sh。如果不加上./,bash可能会响应找到不到hello.sh的错误信息。因为目前的工作目录(/data/shell)可能不在执行程序默认的搜索路径之列,也就是说,不在环境变量PASH的内容之中。查看PATH的内容可用 echo $PASH 命令。现在的/tmp就不在环境变量PASH中的,所以必须加上./才可执行。

方式二:以绝对路径的方式执行脚本

[root@rgl tmp]# /tmp/test.sh 
hello shell
[root@rgl tmp]# 

方式三:直接使用bash或sh来执行脚本

[root@rgl tmp]# cd /tmp/
[root@rgl tmp]# bash test.sh 
hello shell
[root@rgl tmp]# sh test.sh 
hello shell
[root@rgl tmp]#

这种方式用户不需要拥有脚本的执行权限。因为这种方式将hello.sh作为参数传给sh(bash)命令来执行的。这时不是hello.sh自己来执行,而是被人家调用执行,所以不需要执行权限

方式四:使用source启动shell脚本

[root@rgl tmp]# cd /tmp/
[root@rgl tmp]# . test.sh 
hello shell
[root@rgl tmp]# source test.sh 
hello shell
[root@rgl tmp]#

猜你喜欢

转载自blog.csdn.net/fastlearn/article/details/77751902
今日推荐