linux怎么执行shell脚本


本文摘自PHP中文网,作者V,侵删。

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

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

1

2

3

4

5

6

7

8

9

[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、工作目录执行

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

1

2

3

4

5

[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、绝对路径执行

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

1

2

3

4

5

6

[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来接着脚本执行

1

2

3

4

[tan@tan scripts]$ sh test.sh

Hello Shell

[tan@tan scripts]$ bash test.sh

Hello Shell

4、shell环境执行

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

1

2

3

4

[tan@tan scripts]$ . test.sh

Hello Shell

[tan@tan scripts]$ source test.sh

Hello Shell

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

以上就是linux怎么执行shell脚本的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

如何将一台实例加入到指定的安全组

如何给Linux配置ip地址

Linux下root用户不能通过ssh连接

包月ecs带宽如何按流量计费

Linux修改主机名的方法

Linux怎么清屏

Linux系统下有vscode吗

Linux如何清空某目录内文件

Linux如何使用mkdir命令创建目录?(示例详解)

怎么查看Linux版本

更多相关阅读请进入《Linux》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...