|NO.Z.00034|——————————|LinuxShell|——|Linux&Shell$基础.V03|——|脚本执行方式|

一、shell脚本执行方式
### --- 编写一个shell脚本

!/bin/Bash 
~~~     # 注:
~~~     这个不是注释,是bash bang,可以理解为标识的是以下程序写的是shell程序,
~~~     告诉系统应该需要使用什么程序来解释这个语言。这句话对于shell脚本是不可以执行的。
~~~     若是写入的是纯shell脚本且是在linux环境下,不写!/bin/bash是可以执行的
~~~     若是在shell中嵌套更高级的语言:比如在人际交互的模式下,
~~~     需要使用其它语言,若是你不写!/bin/bash这个程序是不会去执行的
echo "Mr. yanqi is the most honest man! "
~~~     # 注:
~~~     若是在后面加入!需要加空格,若是不加,系统会报错
[root@localhost ~]# vim hello.sh
#!/bin/bash
echo "Mr. yanqi is the most honest man!  "
 
[root@localhost ~]# echo  "Mr. yanqi is the most honest man!"
-bash: !": event not found
[root@localhost ~]# echo  "Mr. yanqi is the most honest man! "
Mr. yanqi is the most honest man!  
二、shell执行一:
### --- shell执行一: 
~~~     Shell 脚本写好了,那么这个脚本该如何运行呢?
~~~     在 Linux 中脚本的执行主要有这样两种种方法:

~~~     # 赋予执行权限,直接运行
~~~     这种方法是最常用的 Shell 脚本运行方法,也最为直接简单。
~~~     就是赋予执行权限之后,直接运行。
~~~     当然运行时可以使用绝对路径,也可以使用相对路径运行。命令如下:

~~~     #注:
~~~     因为我们已经在/root/目录当中,所以也可以使用相对路径运行
### --- 赋予执行权限
[root@localhost ~]# chmod 755 hello.sh 
 
### --- 使用绝对路径运行
[root@localhost ~]# /root/hello.sh 
Mr. yanqi is the most honest man! 
[root@localhost ~]# ./hello.sh 
Mr. yanqi is the most honest man!  
三、shell执行二:
### --- shell执行二:
~~~     通过 Bash 调用执行脚本;直接使用bash直接调用,不需要执行权限;
~~~     这种方式并不提倡使用

[root@localhost ~]# bash hello.sh 
Mr. yanqi is the most honest man! 

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on 2022-03-26 23:14  yanqi_vip  阅读(29)  评论(0)    收藏  举报

导航