当我们给予shell脚本执行的权限后,就可以测试程序了,假设shell脚本文件为hello.sh
放在/root目录下。下面介绍几种在终端执行shell脚本的方法:
1.切换到shell脚本所在的目录,执行:
1234[root@localhost home]
# cd /root/
[root@localhost ~]
# ./hello.sh
hello guys!
welcome to my Blog:linuxboy.org!
2.以绝对路径的方式执行:
123[root@localhost ~]
# /root/hello.sh
hello guys!
welcome to my Blog:linuxboy.org!
3.直接用bash或sh执行:
123456[root@localhost ~]
# bash hello.sh
hello guys!
welcome to my Blog:linuxboy.org!
[root@localhost ~]
# sh hello.sh
hello guys!
welcome to my Blog:linuxboy.org!
注意:用以上三种方法执行shell脚本,现行的shell会开启一个子shell环境,去执行shell脚本
也可以让shell脚本在现行的shell中执行:
4.现行的shell中执行
1234567[root@localhost ~]
# . /hello.sh
hello guys!
welcome to my Blog:linuxboy.org!
[root@localhost ~]
# source hello.sh
hello guys!
welcome to my Blog:linuxboy.org!
[root@localhost ~]
#
注意:.与/之间有空格