当前位置:网站首页>shell脚本
shell脚本
2022-07-30 09:06:00 【x0757】
目录
1 什么是shell脚本
Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。
Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。
1.1 hello world入门
#!bin/bash
echo "内容"
执行脚本:

修改权限:

1.2 Shell 变量
#!/bin/bash
# echo表示输出语句
echo "hello world"
# 定义变量
name="张三"
# 使用变量${}
echo "nihao ${name} welcome to qy151"
1.3 Shell 传递参数
我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n。n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推……


1.4 Shell 数组
数组中可以存放多个值。Bash Shell 只支持一维数组(不支持多维数组),初始化时不需要定义数组大小(与 PHP 类似)。
与大部分编程语言类似,数组元素的下标由 0 开始。
Shell 数组用括号来表示,元素用"空格"符号分割开,语法格式如下:
1.5 基本运算符
Shell 和其他编程语言一样,支持多种运算符,包括:
算数运算符
关系运算符
布尔运算符
字符串运算符
文件测试运算符
原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用。
expr 是一款表达式计算工具,使用它能完成表达式的求值操作。
例如,两个数相加(注意使用的是反引号 *`* 而不是单引号 *'*):

两点注意:
表达式和运算符之间要有空格,例如 2+2 是不对的,必须写成 2 + 2,这与我们熟悉的大多数编程语言不一样。
完整的表达式要被 包含,注意这个字符不是常用的单引号,在 Esc 键下边。
1.5.1 关系运算符
关系运算符只支持数字,不支持字符串,除非字符串的值是数字 .
1.5.2 布尔运算符


1.5.2 逻辑运算符


2 Shell 流程控制
if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi
ps -ef | grep -c "mysql"统计某个进程的个数


边栏推荐
- 读书笔记:《这才是心理学:看穿伪心理学的本质(第10版)》
- Kotlin value class - value class
- qsort 函数的使用及其模拟实现
- Taosi TDengine 2.6+ optimization parameters
- The FPGA based protocol 2: the I2C read and write E squared PROM
- Test automation selenium (a)
- Liunx服务器安装SVN(安装包版)
- Apache DolphinScheduler's new generation of distributed workflow task scheduling platform in practice - Part 1
- HCIP - MPLS VPN experiment
- 【 HMS core 】 【 】 the FAQ HMS Toolkit collection of typical questions 1
猜你喜欢

leetcode 剑指 Offer 46. 把数字翻译成字符串

Kotlin 值类 - value class

微软 SQL 服务器被黑,带宽遭到破坏

MySQL Explain usage and parameter detailed explanation

Unreal Engine Graphic Notes: could not be compiled. Try rebuilding from source manually. Problem solving

快解析结合友加畅捷通t1飞跃版

快解析结合泛微OA

How to run dist file on local computer

02-课程发布

Unified exception handling causes ResponseBodyAdvice to fail
随机推荐
leetcode 剑指 Offer 42. 连续子数组的最大和
Windows 下安装 MySQL
342 · Valley Sequence
统一异常处理导致ResponseBodyAdvice失效
聊聊 MySQL 事务二阶段提交
STM8L_库函数-模板搭建
69. Sqrt(x)x 的平方根
The FPGA based protocol 2: the I2C read and write E squared PROM
[Fun BLDC series with zero basics] Taking GD32F30x as an example, the timer related functions are explained in detail
Integral Special Notes - Definition of Integral
快解析结合泛微OA
HCIP --- MPLS VPN实验
20220728 Use the bluetooth on the computer and the bluetooth module HC-05 of Huicheng Technology to pair the bluetooth serial port transmission
用示波器揭示以太网传输机制
如何避免CMDB沦为数据孤岛?
包、类及四大权限和static
Unable to locate the program input point ucrtbase.abort on the dynamic link library api-ms-win-crt-runtime-|1-1-0.dll
Explain the problem of change exchange in simple terms - the shell of the backpack problem
【深度学习】(问题记录)<对一个变量求梯度得到什么>-线性回归-小批量随机梯度下降
LeetCode二叉树系列——94.二叉树的中序遍历