高级开发必须掌控的知识,Shell编程

Shell描述

  • shell 是一种脚本语言

  • 脚本:本质是一个文件,文件里面存放的是 特定格式的指令,系统可以使用脚本解析器 翻译或解析 指令 并执行(它不需要编译)

  • shell 既是应用程序 又是一种脚本语言(应用程序 解析 脚本语言)

  • shell命令解析器:

    系统提供 shell命令解析器: sh ash bash

    查看自己linux系统的默认解析:echo $SHELL
    在这里插入图片描述
    shell脚本是一种脚本语言,我们只需使用任意文本编辑器,按照语法编写相应程序,增加可执行权限,即可在安装shell命令解释器的环境下执行

Shell脚本入门:介绍

  • 理解Shell是什么
  • 理解Shell脚本是什么
  • 理解为什么学习Shell脚本(Shell脚本程序的作用)
  • linux系统默认的Shell解析器

疑问

linux系统是如何操作计算机硬件、Cpu、内存、磁盘、显示器等?
答案:使用linux的内核操作计算机的硬件

Shell介绍

通过编写Shell命令发送给Linux内核去执行,操作计算机硬件,所以Shell命令是用户操作计算机的桥梁

Shell是命令,类似于windows系统的doc命令

Shell是一门程序设计语言,Shell里面含有变量,函数,逻辑控制语句等等

用户--使用文字或图形界面操作操作系统
使用者界面--接受来自用户的命令与内核进行沟通
内核--真正控制硬件进行进程管理,存储管理等
硬件--CPU/内存/显卡/显示器/硬盘等

Shell脚本

通过Shell命令或程序编程语言编写的Shell文本文件,这就是Shell脚本,也叫Shell程序

为什么学习Shell脚本?

通过Shell命令与编程语言来提高linux的管理工作效率

Shell的运行过程

当用户下达指令给该操作系统的时候,实际上是把指令告诉Shell,经过Shell解释,处理后让内核做出相应的动作,系统的回应和输出的信息也是有Shell去处理的,然后显示在用户的屏幕上。

Shell脚本指令
响应
调用
响应
用户
Shell解析器
linux内核服务

Shell解析器

解析类型

查看linux系统centos支持的Shell解析器

cat /etc/shells

效果:
在这里插入图片描述

解析器类型介绍
/ban/shBoune Shell,是linux最初使用的shell
/ban/bashBourne Again Shell 它是Boune Shell 的扩展,简称bash,是Linux OS默认shell,有灵活和强大的编辑接口,同时又很友好的用户界面,交互性强
/sbin/nologin未登录解析器,shell设置为/sbin/nologin 适用于控制用户禁止登录系统的,有时候有些服务,比如邮件服务,大部分都是用来接收主机的邮件而已,并不需要登录
/ban/dashdash(Debian AImquist Shell),也是一种Unix Shell,它比Bash小,只需要较少的磁盘控件,但是它的对话性功能也比较少,交互性极差
/bin/cshC Shell是C语言风格Shell
/bin/tcsh是C Shell的一个扩展版本

Centos的默认解析器是bash

语法:

echo $SHELL

含义:打印输出当前系统环境使用的Shell解析器类型
echo 用于打印数据输出到终端
$SHELL 是全局共享的读取解析器类型环境环境变量,全局变量时所有的shell程序都可以读取的变量

效果:
在这里插入图片描述

小结

  1. shell是什么?
是命令,类似于windows的doc命令
  1. shell脚本是什么?
是一个文本文件,里面可以编写shell命令或进行编程,形成一个可重用执行的脚本文件
  1. shell脚本的作用?
通过shell编程提高对linux系统管理工作效率
  1. lunix默认的shell解析器?
/bin/bash

Shell脚本入门:编写格式与执行方式

Shell脚本文件编写规范

脚本文件后缀名规范

shell脚本文件就是文本文件,后缀名建议使用.sh结尾

首行格式规范

首行需要设置shell解析器类型,语法

#!/bin/bash

含义:当前shell脚本采用bash解析器

注释格式

  • 单行注释:语法
# 注释内容
  • 多行注释
:<<!
# 注释内容1
# 注释内容2

shell脚本Hello Word入门案例

  • 需求
    创建一个shell脚本文件,输出hello word字符串

  • 效果
    在这里插入图片描述

  • 实现步骤

  1. 创建shell脚本文件
touch helloword.sh
  1. 编辑文件

脚本文件执行方式

  1. sh解析器执行方式
  2. bash解析器执行方式
  3. 仅路径执行方式

sh解析器执行方式

相对路径效果

sh helloword.sh

在这里插入图片描述
绝对路径效果

sh /root/helloword.sh

在这里插入图片描述

bash解析器执行方式

相对路径效果

bash helloword.sh

在这里插入图片描述
绝对路径效果

bash /root/helloword.sh

在这里插入图片描述

仅路径执行方式

  1. 给用户添加可执行权限
chmod a+x 脚本文件
  1. 路径执行脚本文件
./helloword.sh

在这里插入图片描述

Shell脚本入门:多命令处理

shell脚本文件中编写多个shell命令

案例需求

已知目录 /root/demo目录,执行batch.sh脚本,实现该目录下创建一个one.text,在one.txt中添加内容 hello shell 字符串。

步骤分析

  1. 使用mkdir创建/root/demo目录
  2. 创建脚本文件batch.sh
  3. 编辑脚本文件
    3.1 命令1:touch创建文件,文件名 /root/demo/one.txt
    3.2 命令1:输出数据"hello shell"到one.txt中

“数据”>>文件

  1. 执行脚本

实现步骤

  1. 进入root目录,执行创建/root/demo目录命令
mkdir /root/demo
  1. 创建并编辑shell脚本
    在这里插入图片描述

Shell变量

用于存储管理临时的数据,这些数据都是在运行内存中

系统环境变量

是系统提供的共享变量,是lunix系统加载Shell的配置文件中定义的变量共享给所有的Shell程序使用

全局配置文件

  • /etc/profile
  • /etc/profile.d/*.sh
  • /etc/bashrc

个人配置文件

  • 当前用户/.bash_profile
  • 当前用户/.bashrc

环境变量分类

一般情况下,我们都是针对全局配置进行操作。

自定义变量

特殊变量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # --------------------------------------------------------- 3 4 # Suppose I have a large number of jobs that need to be run, with 5 #+ any data that is to be treated in files of a given name pattern in a 6 #+ directory. There are several machines that access this directory, and 7 #+ I want to distribute the work over these different boxen. Then I 8 #+ usually nohup something like the following on every box: 9 10 while true 11 do 12 for n in .iso.* 13 do 14 [ "$n" = ".iso.opts" ] && continue 15 beta=${n#.iso.} 16 [ -r .Iso.$beta ] && continue 17 [ -r .lock.$beta ] && sleep 10 && continue 18 lockfile -r0 .lock.$beta || continue 19 echo -n "$beta: " `date` 20 run-isotherm $beta 21 date 22 ls -alF .Iso.$beta 23 [ -r .Iso.$beta ] && rm -f .lock.$beta 24 continue 2 25 done 26 break 27 done 28 29 # The details, in particular the sleep N, are particular to my 30 #+ application, but the general pattern is: 31 32 while true 33 do 34 for job in {pattern} 35 do 36 {job already done or running} && continue 37 {mark job as running, do job, mark job as done} 38 continue 2 39 done 40 break # Or something like `sleep 600' to avoid termination. 41 done 42 43 # This way the script will stop only when there are no more jobs to do 44 #+ (including jobs that were added during runtime). Through the use 45 #+ of appropriate lockfiles it can be run on several machines 46 #+ concurrently without duplication of calculations [which run a couple 47 #+ of hours in my case, so I really want to avoid this]. Also, as search 48 #+ always starts again from the beginning, one can encode priorities in 49 #+ the file names. Of course, one could also do this without `continue 2', 50 #+ but then one would have to actually check whether or not some job 51 #+ was done (so that we should immediately look for the next job) or not 52 #+ (in which case we terminate or sleep for a long time before checking 53 #+ for a new job).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一名技术极客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值