网站备案 网安备案新闻资讯帮助中心    业务咨询:4008 313 665    24小时技术支持:18922898210
深圳主机托管,深圳主机租用,深圳服务器租用,深圳服务器托管,深圳idc,广州服务器托管,广州服务器租用,香港服务器托管,机柜宽带租用,南方联合
当前位置:首页->帮助中心

linux执行shell脚本的四种方式

作者:admin      来源:南方联合      发布日期:2020/4/13 0:00:00

本文主要介绍Linux为您执行Shell脚本的四种方式。本文中的示例代码非常详细,并具有一定的参考价值。有兴趣的朋友可以参考一下。

linux执行shell脚本的四种方式

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
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执行shell脚本的四种方式,希望南方联合小编的分享对大家有所帮助,南方联合专注IDC14年,为大家提供专业化服务器托管,服务器租用,主机托管,云服务器租用,香港服务器租用等海内外服务器资源,详情欢迎咨询客服了解。

开始体验免费套餐

联系客服,只要有业务需求,即可获得独立服务器测试!
注册成为会员,为您提供最佳上云实践机会
南方联合在线客服 立即登录或注册

深圳服务器托管 深圳服务器租用 深圳主机托管 深圳主机租用 CDN加速 专线接入 独享带宽 数据中心机房机柜租用idc公司旧版网站登陆南方联合站点地图友情链接:裸金属服务器

Copyright © 2005-2020 深圳市南方联合科技有限公司 版权所有 备案号:粤ICP备05107047号 经营许可证编号:A2.B1-20170254 统一社会信用代码:91440300779894166R

网络社会征信网    深圳网警    深圳工商    南方联合备案号    深圳市市场监督管理局企业主体身份公示

安备号

粤公网安备 44030602000558号 非法信息举报电话0755-88833581

深圳主机托管,深圳主机租用,深圳服务器租用,深圳服务器托管,深圳idc,广州服务器托管,广州服务器租用,香港服务器托管,机柜宽带租用,南方联合