当我们给予shell脚本执行的权限后,就可以测试程序了,假设shell脚本文件为hello.sh
放在/root目录下。下面介绍几种在终端执行shell脚本的方法:
1.切换到shell脚本所在的目录,执行:
1234[root@localhost home]# cd /root/[root@localhost ~]# ./hello.shhello guys!welcome to my Blog:linuxboy.org!
2.以绝对路径的方式执行:
123[root@localhost ~]# /root/hello.shhello guys!welcome to my Blog:linuxboy.org!
3.直接用bash或sh执行:
123456[root@localhost ~]# bash hello.shhello guys!welcome to my Blog:linuxboy.org![root@localhost ~]# sh hello.shhello guys!welcome to my Blog:linuxboy.org!
注意:用以上三种方法执行shell脚本,现行的shell会开启一个子shell环境,去执行shell脚本
也可以让shell脚本在现行的shell中执行:
4.现行的shell中执行
1234567[root@localhost ~]# . /hello.shhello guys!welcome to my Blog:linuxboy.org![root@localhost ~]# source hello.shhello guys!welcome to my Blog:linuxboy.org![root@localhost ~]#
注意:.与/之间有空格
执行Shell脚本的多种方法
本文详细介绍了如何在终端中执行Shell脚本,包括切换目录执行、使用绝对路径、通过bash或sh执行以及在当前shell中执行的方法,并强调了在不同场景下选择合适执行方式的重要性。
8933

被折叠的 条评论
为什么被折叠?



