采用bash或sh+脚本的相对路径或绝对路径(不用赋予脚本+x权限)
[root@localhost datas]# touch helloworld.sh
[root@localhost datas]# vi helloworld.sh
#!/bin/bash
echo "helloworld" #在helloworld.sh中输入如下内容
[root@localhost datas]# sh helloworld.sh
Helloworld
[root@localhost datas]# sh /home/shellTest/datas/helloworld.sh
helloworld
采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x)
[root@localhost datas]# chmod 777 helloworld.sh
[root@localhost datas]# ./helloworld.sh
Helloworld
[root@localhost datas]# /home/shellTest/datas/helloworld.sh
helloworld
总结
注意:
第一种执行方法,本质是bash解析器帮你执行脚本,所以脚本本身不需要执行权限。
第二种执行方法,本质是脚本需要自己执行,所以需要执行权限。