|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
分类:
cdv008-shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通