linux怎么执行shell脚本

王林
发布: 2020-03-11 16:35:19
原创
5146 人浏览过

linux怎么执行shell脚本

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

首先,看下我们的脚本内容

[tan@tan scripts]$ ll total 4 -rw-rw-r--. 1 tan tan 68 May 8 23:18 test.sh [tan@tan scripts]$ cat test.sh #!/usr/bin/bash /usr/bin/python <<-EOF print "Hello Shell" EOF
登录后复制

(推荐教程:linux教程

1、工作目录执行

工作目录执行,指的是执行脚本时,先进入到脚本所在的目录(此时,称为工作目录),然后使用 ./脚本方式执行

[tan@tan scripts]$ ./test.sh -bash: ./test.sh: Permission denied [tan@tan scripts]$ chmod 764 test.sh [tan@tan scripts]$ ./test.sh Hello Shell
登录后复制

报了权限错误,这里需要赋权,使用chmod 764 test.sh 赋权后就可以正常执行了。

2、绝对路径执行

绝对路径中执行,指的是直接从根目录/到脚本目录的绝对路径

[tan@tan scripts]$ pwd /home/tan/scripts [tan@tan scripts]$ `pwd`/test.sh Hello Shell [tan@tan scripts]$ /home/tan/scripts/test.sh Hello Shell
登录后复制

3、sh执行

sh执行,指的是用脚本对应的sh或bash来接着脚本执行

[tan@tan scripts]$ sh test.sh Hello Shell [tan@tan scripts]$ bash test.sh Hello Shell
登录后复制

4、shell环境执行

shell环境执行,指的是在当前的shell环境中执行,可以使用 . 接脚本 或 source 接脚本

[tan@tan scripts]$ . test.sh Hello Shell [tan@tan scripts]$ source test.sh Hello Shell
登录后复制

相关教程推荐:linux视频教程

以上是linux怎么执行shell脚本的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!