博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
20.2 shell脚本结构和执行
阅读量:7053 次
发布时间:2019-06-28

本文共 2720 字,大约阅读时间需要 9 分钟。

hot3.png

shell脚本结构和执行

  • 开头需要加#!/bin/bash
  • 以#开头的行作为解释说明
  • 脚本的名字以.sh结尾,用于区分这是一个shell脚本
  • 执行方法有两种
  • chmod +x 1.sh; ./1.sh
  • bash 1.sh
  • 查看脚本执行过程 bash -x 1.sh
  • 查看脚本是否语法错误 bash -n 1.sh

shell脚本结构和执行

  1. 创建一个shell文件夹,并切入到文件夹中
[root@hanfeng ~]# mkdir shell[root@hanfeng ~]# cd shell/[root@hanfeng shell]#
  1. 写shell脚本
  • #! /bin/bash //第一行必须这么写,固定格式,作用就是脚本若是在当台机器上去执行,可以不加这一行也没关系,因为它知道下面若干条的命令能在这台机器上去执行,去解析
[root@hanfeng shell]# vi 01.sh#! /bin/bash   	//固定格式echo "123"wls保存退出
  1. 执行脚本——>sh 01.sh
[root@hanfeng shell]# sh 01.sh123 22:45:12 up 14 min,  1 user,  load average: 0.00, 0.01, 0.05USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHATroot     pts/0     22:32    0.00s  0.05s  0.03s w01.sh[root@hanfeng shell]#
  1. 在当前终端里,把01.sh中的#! /bin/bash 去掉后在执行脚本,会看到得到的结果相同,不会出现任何的问题,这就说明这台机器是能识别里面一条一条的命令的,去运行这里面的命令;但若是换一台机器,就不一定能执行了
  2. 在第一行,文件头指定 #!/bin/bash ,接下来要运行的命令是通过哪一个解释器来操作的,通常都是 /bin/bash 解释器来执行的
  3. 给01.sh文件一个执行权限
[root@hanfeng shell]# chmod a+x 01.sh[root@hanfeng shell]#

7执行shell脚本 ./01.sh 能这样执行,说明这些命令被解析了,被/bin/bash认识了

[root@hanfeng shell]# ./01.sh123 23:11:21 up 41 min,  1 user,  load average: 0.00, 0.01, 0.05USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHATroot     pts/0     22:32    1.00s  0.05s  0.00s /bin/bash ./01.sh01.sh[root@hanfeng shell]#
  1. /bin/bash也是一条命令, /bin/bash 和 /bin/sh 是同一个文件
[root@hanfeng shell]# ls -l /bin/bash-rwxr-xr-x. 1 root root 960368 6月  10 2014 /bin/bash[root@hanfeng shell]# ls -l /bin/shlrwxrwxrwx. 1 root root 4 10月 21 00:47 /bin/sh -> bash[root@hanfeng shell]#
  1. 01.sh文件内容就是被/bin/bash所解析的
  2. 若没有 /bin/bash ,可以使用 /bin/bash 01.sh去执行
[root@hanfeng shell]# /bin/bash 01.sh123 23:21:43 up 51 min,  1 user,  load average: 0.00, 0.01, 0.05USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHATroot     pts/0     22:32    7.00s  0.03s  0.00s w01.sh[root@hanfeng shell]#
  1. 若是在shell脚本中在写入第二遍 #号开头的行, 就表示解释说明的作用
  2. 脚本的一般都是以.sh结尾,是为了区分这是一个shell脚本,否则需要打开这个文件,才知道是shell脚本
  3. 运行shell脚本有两种方法
  • 一种是sh 01.sh运行shell脚本
  • 另一种方法
    • 先 chmod a+x 1.sh 给文件加一个执行权限
    • 再 ./1.sh 去执行
      • 这里的 ./ 就相当于一个相对路径,相对当前一个路径
      • 也可以使用绝对路径去执行脚本 /root/shell/01.sh ,其实就是找到这个文件去执行
[root@hanfeng shell]# /root/shell/01.sh123 23:45:02 up  1:14,  1 user,  load average: 0.00, 0.01, 0.05USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHATroot     pts/0     22:32    6.00s  0.03s  0.00s w01.sh[root@hanfeng shell]#
  1. 查看脚本执行过程 sh -x 01.sh 或者bash -x 01.sh
  • -x,就是显示脚本执行的过程
  • 每一个加号,表示一个操作步骤
[root@hanfeng shell]# sh -x 01.sh+ echo 123123+ w 23:47:35 up  1:17,  1 user,  load average: 0.00, 0.01, 0.05USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHATroot     pts/0     22:32    7.00s  0.03s  0.00s sh -x 01.sh+ ls01.sh[root@hanfeng shell]#
  1. 查看脚本是否有错误 sh -n 01.sh
  • 若是没有任何的输出,那么脚本则没有错误
  • sh -n 01.sh命令是检测是否存在语法错误
[root@hanfeng shell]# sh -n 01.sh[root@hanfeng shell]#

转载于:https://my.oschina.net/u/3707314/blog/1617198

你可能感兴趣的文章
Eclipse的Spring库导入
查看>>
velocity 判断 变量 是否不是空或empty
查看>>
获得数据库自动生成的主键
查看>>
Hibernate缓存机制
查看>>
【BZOJ】1415 [Noi2005]聪聪和可可 期望DP+记忆化搜索
查看>>
android 7.1 调用相机崩溃解决办法
查看>>
------第二节-----------------第二讲----单链表的基本操作---------
查看>>
delegate代理设计模式
查看>>
花10分钟搞懂开源框架吧 - 【NancyFx.Net】
查看>>
GridView(网格视图)+MotionEvent(触控事件)实现可以拖动排序的网格图
查看>>
显示/隐藏Mac下的隐藏文件
查看>>
POJ-3565 Ants 空间点对不相交匹配-最小权值匹配
查看>>
springmvc(一)
查看>>
Hibernate与 MyBatis的比较
查看>>
awk调用shell命令的两种方法:system与print
查看>>
juqery模板 Templates
查看>>
移动浏览器中实现拨打电话,调用sms,发送email
查看>>
docker 搭建小型的node开发环境。
查看>>
Java第九次作业
查看>>
.Net Discovery系“.NET技术”列之-深入理解平台机制与性能影响 (中)
查看>>