当前位置:网站首页>Shell编程规范与变量
Shell编程规范与变量
2022-07-28 14:48:00 【眼下一颗柠檬】
文章目录
一、Shell脚本概述
1.什么是Shell
Shell是一个命令解释器,它在系统的最外层,负责直接与用户进行对话,把用户的输入解释给操作系统,并处理各种各样的操作系统的输出结果,输出到屏幕反馈给用户。这种对话方式可是交互也可以是非交互式的,我们所输入的命令计算机是不是别的,这时就需要一种程序来帮助我们进行翻译,变成计算机能识别的二进制程序,同时又把计算机生成的结果返回给我们。
2.Shell的作用
Linux系统中的Shell是一个特殊的应用程序,它介于操作系统内核与用户之间,充当了一个"命令解释器"的角色,负责接收用户输入的操作指令并进行解释,将需要执行的操作传递给内核执行,并输出执行结果。
常见的Shell解释器程序有很多种,使用不同的Shell时,其内部指令、命令行提示符等方面会存在一些区别。
通过/etc/shells文件可以了解当前系统所支持的Shell脚本种类
/bin/sh #是bash命令的软连接
/bin/bash #基准于GNU的框架下发展出的Shell
/usr/bin/sh #已经被bash所替换
/usr/bin/bash #centos和redhat系统默认使用bash shell(解释器)
/bin/txsh #csh的增强版,与csh完全兼容整合了csh,提供更多的功能
/bin/csh #已经被/bin/bash所替换(整合C shell,提供更多的功能)
3.Shell脚本能干什么
- 自动化完成软件的安装部署,如安装部署LAMP架构服务
- 自动化完成系统的管理,如批量添加用户
- 自动化完成备份,如数据库定时备份
- 自动化分析处理,如网站访问量
4.Shell脚本使用场景
- 重复性操作
- 交互性任务
- 批量事务处理
- 服务运行状态监控
- 定时任务执行
二、Shell脚本的构成
1.脚本声明(解释器):若第一行为"#!/bin/bash",表示此行一下的代码语句是通过/bin/bash程序来解释执行,#!/bin/bash为默认解释器。还有其他类型的解释器,比如:#!/usr/bin/python、#!/usr/bin/expect
2.注释信息:以"#"开头的语句表示为注释信息,被注释的语句再运行脚本时不会被执行
3.可执行语句:比如echo命令,用于输出" "之间的字符串


三、Shell脚本的执行
1.方法一:指定路径
指定路径的命令,要求文件必须有x权限
指定绝对路径
指定相对路径
2.方法二:指定Shell
指定Shell来解释脚本,不要求文件必须有x权限
sh 脚本路径
soucre 脚本路径
bash 脚本路径
四、重定向与管道操作
1.管道操作"|"
将管道符号"|"左侧的命令输出的结果,作为右侧命令的输入(处理对象),同一行命令中可以使用多个管道
2.重定向
2.1交互式硬件设备
| 类型 | 设备文件 | 文件描述编号 | 默认设备 |
|---|---|---|---|
| 标准输入 | /dev/stdin | 0 | 键盘 |
| 标准输出 | /dev/stdout | 1 | 显示器 |
| 标准错误输出 | /dev/stderr | 2 | 显示器 |
2.2重定向操作

重定向输出
覆盖原文件内容
重定向输入

错误输出
总结
边栏推荐
- 一波骚操作解决Laya场景编辑器报错问题
- 记录一下 clearfix 清除浮动
- 语音社交系统——完善有声系统产业链
- 生命的感悟
- The price war of off screen fingerprints has resumed, and second-line manufacturers are expected to win 30% of the market this year?
- 跟我学Rx编程——Concat
- FTP file transfer protocol
- Software architecture and design (II) -- Architecture Model
- Give you a linked list, delete the penultimate node of the linked list, and return the head node of the linked list.
- Docker implements redis cluster mode hash slot partition for 100 million level data storage
猜你喜欢

What is the concept of game testing? What are the test methods and processes?

Matlab exports high-definition pictures without distortion in word compression and PDF conversion

编码器高速脉冲计数器Modbus RTU模块IBF150

比例电磁阀控制阀4-20mA转0-165mA/330mA信号隔离放大器

shell编程规范与变量

About how Simulink generates model coverage reports

Getting started with crawlers (1) -- requests (1)

1路编码器2路DI转速测量RS485串口连接1路DO报警模块IBF151

How as makes intelligent prompts regardless of case
What are the process specifications of software testing? How to do it?
随机推荐
知识点qwer
Open light input / relay output rs485/232 remote data acquisition IO module ibf70
突发!微星CEO江胜昌坠楼身亡
Baidu proposes a dynamic self distillation method to realize dense paragraph retrieval by combining interactive model and double tower model
【直播预约】数据架构演进下的新挑战——上海站
Youdao cloud notes remove the bottom advertisement
String (3)
软件架构与设计(七)-----互动架构
0-75mV/0-100mV转RS485/232通讯接口MODBUS RTU采集模块IBF8
[channel attention mechanism] senet
兆骑科创创业大赛平台,活动路演,投融资对接
电压频率的变换原理
Software architecture and design (VII) -- interactive architecture
5-channel di/do relay output remote IO acquisition module Modbus tcp/ibf95
Software architecture and design (VI) -- hierarchy
在OBS上进行H265推流
一文了解 Rainbond 云原生应用管理平台
About the pictures inserted in the word document, only the following part is displayed
【删除指定数字——leetcode]
软件架构与设计(八)-----分布式架构