第一章 初识shell

第一章 初识shell

1.1,如何连接用户和内核

image.png

1,用户运行命令,shell会调用内核暴露出来的接口,这就是使用内核

2,接口就是函数,使用内核就是调用函数。

1.2,连接其他程序

1,shell本身自带的叫 内置命令;其他的应用程序(一个程序就是一个命令)叫 外部命令

**2,shell功能:**文本或字符串检索,文件的查询或创建,大规模软件的自动部署,更改系统设置,监控服务器性能,发送警报邮件,抓取网页内容,压缩文件等。

3,shell还支持 把一个程序的输出结构传递给另一个程序作为输入信息

image.png

1.3,同样支持编程

1,if…else选择结构,case…in开关语句,for,while,until循环

2,变量,数组,字符串,注释,加减乘除,逻辑运算等概念。

3,函数,包括用户自定义的函数和内置函数(如printf(),export(),eval()

**4,现实功能:**检查计算机的硬件参数,搭建web运行环境,直接分析等。

1.4,是一种脚本语言

1,**编译:**运行程序是会生成可执行文件。例如:C/C++,Go

2,**解释:**一边执行一边翻译。例如:shell

3,**优点:**执行速度快,对硬件要求低,保密性好,适合开发操作系统,对大型应用程序,数据库等。

1.5,各种版本

1,Boure shell;C shell了解

2,bash shell:bash放在Linux系统的/bin/bash文件中,也是所有Shell中较为完美,常用的版本。

3,查看shell版本

#Shell是一一个程序,-般都放在/bin或者/usr/bin目录下。当前Linux系统可用的Shell都记录在/ete/shells文件中。
[root@localhost ~]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh

#查看Linux的默认shell,可以输出SHELL环境变量
[root@localhost ~]# echo $SHELL
/bin/bash

1.6,是运维工程师必备技能

1,运维工程师(OPS)工作内容:搭建运行环境,让程序员写的代码能够高效,稳定,安全地在服务器上运行

2,Ops的工作细节内容如下:

(1)安装操作系统,如CentOs、Ubuntu 等。

(2)部署代码运行环境。例如,网站后台语言采用PHP,就需要安装Nginx、Apache、MySQL等。

(3)及时修复漏洞,防止服务器被攻击,包括Linux 本身漏洞和各个软件的漏洞。

**(4) 根据项目需求升级软件。**例如,PHP 7.0在性能方面取得了重大突破,如果现在服务器压力比较大,就可以考

虑将旧版的PHP 5.x升级到PHP 7.0。

(5)监控服务器压力,避免服务器死机。例如,淘宝在“双十一”的时候会瞬间涌入大量用户导致部分服务器死机,

网页无法访问,甚至连支付宝都不能使用。

**(6)分析日志。**乃时发现代码或者环境问题,通知相关人员修复。

3,变量是shell运行时使用最小数据单元。

1.7,变量的定义

1,程序是在内存中运行的。

2,变量:(用特定的字符串去表示不固定的内容)内存空间变化的值。变量名:对内存空间的命名。

#格式
变量名 = 变量值;
varname = varvalue;

3,当首次使用某个变量时,实际上就定义了这个变量,。如果没有给出变量值,则变量会被赋予一个空字符串。

1.8,变量类型

**变量的使用:**自定义,使用,查看,取消,作用范围。

**1,自定义变量:**临时定义的变量。(局部或普通变量

image.png

[root@localhost ~]# mkdir /tmp/scripts		#自己创建的测试目录
[root@localhost scripts]# vim ping01.sh
#!/bin/bash
ip=192.168.88.120
ping -c1 $ip &>/dev/null

ping -c:指定连接次数
$ip:连接IP地址
&>/dev/null:垃圾桶

**2,环境变量:**指用export内置命令导出的变量,用来等于shell的运行环境。(**全局变量 **)

image.png

#判断主机IP地址是否正常
[root@localhost scripts]# sh ping02.sh
192.168.88.120 is up
[root@localhost scripts]# cat ping02.sh
#!/bin/bash
ip=192.168.88.120
ping -c1 $ip &>/dev/null
if [ $? -eq 0 ];then
        echo "$ip is up"
else
         echo "$ip is down"
fi


$意思是上一条命令的返回值。如果返回值为0,表示正常;否则表示主机宕机。

**3,位置变量:从命令行,函数等传递参数时,$0,$1称为特殊位置变量。**不用自己定义,作用时固定的。

$0表示本身,$1-$9表示9个参数, 10 以上需要用 括起来,例如 10以上需要用{}括起来,例如 10以上需要用括起来,例如{10}

[root@localhost scripts]# cat test.sh
echo $1 $2
[root@localhost scripts]# chmod a+x test.sh
[root@localhost scripts]# ./test.sh shell linux
shell linux

**4,预定义变量:**在bash中已经有的变量。

image.png

[root@localhost scripts]# pwd
/tmp/scripts
[root@localhost scripts]# echo $?
0
#表示pwd命令成功运行

1.9,变量的赋值

**1,直接赋值:**定义变量

禁止在等号两边添加空格

**2,从键盘读入赋值:**将bash的内置命令read读入的额内容赋值给变量。

格式:read -p [提示信息]:[变量名]

read命令:从标准输入读取单行数据。
#从键盘读入赋值
[root@localhost scripts]# vim ping03.sh
[root@localhost scripts]# cat ping03.sh
#!/bin/bash
read -p "input ip:" ip
ping -c2 $ip &>/dev/null
if [ $? == 0 ];then
        echo "host $ip is ok"
else
        echo "host $ip is fail"
fi
[root@localhost scripts]# chmod a+x ping03.sh
[root@localhost scripts]# sh ping03.sh
input ip:192.168.88.120
host 192.168.88.120 is ok

**3,使用命令行参数赋值:**在命令行shell输入的参数内容。

[root@localhost scripts]# cat test.sh
echo $1 $2
[root@localhost scripts]# chmod a+x test.sh
[root@localhost scripts]# sh test.sh 3/15 3/16
3/15 3/16

**4,利用命令的输出结果赋值:**将命令行执行结果赋值给变量。

**原理:**把一个命令的输出结果当做变量的值,在赋值语句中使用反向单引号。

[root@localhost scripts]# cmd=`date +%F`
[root@localhost scripts]# echo $cmd
2024-03-15
[root@localhost scripts]# echo `date +%F`.tar.gz
2024-03-15.tar.gz

#`date +%F`显示当前时间

**5,从文件中读入数据赋值:**把文件内容赋值给变量。

[root@localhost scripts]# cat line.sh
#!/bin/bash
ls *.sh >execfile
while read line
do
        echo $line
done < execfile

#echo $line重定向


[root@localhost scripts]# sh line.sh
line.sh
ping01.sh
ping02.sh
ping03.sh
test.sh

**单引号:**原样输出

**双引号:**先解析在输出内容

**反引号:**命令调用,等价于$()

#反引号举例
[root@localhost scripts]# touch `date +%F`_file1.txt
[root@localhost scripts]# ls
2024-03-15_file1.txt  

#等价于:
[root@localhost scripts]# touch $(date +%F)_file1.txt
[root@localhost scripts]# ls
2024-03-15_file1.txt 

``等于$()

1.10,变量的运算

变量值的类型默认是字符串,不能直接进行运算,需要使用特殊方法。

**1,expr数值:**用于整数运算,相关字符串长度,匹配等运算处理。

#格式:expr expression
[root@localhost scripts]# num1=10
[root@localhost scripts]# num2=20
[root@localhost scripts]# expr $num1+$num2
10+20
[root@localhost scripts]# expr $num1 + $num2
30
#注意数字两边必须有空格,否则会执行失败。
#expr支持乘法运算,但必须使用反斜线转义
[root@localhost scripts]# expr 2 \* 2
4

2,(())或[]数值运算

(())作用:进行整数运算和数值运算

格式:((表达式))或者[表达式]
#空格可省,需要直接运算结果可以$((表达式))
[root@localhost scripts]# num1=10
[root@localhost scripts]# num2=20
[root@localhost scripts]# sum=$(($num1+$num2))
[root@localhost scripts]# echo $sum
30
[root@localhost scripts]# ehco $sum[$sum1+$sum2]

3,let数值运算

可以直接运算,且不带回显功能,不再使用$引用变量。

#格式:
let 赋值表达式 等同于((赋值表达式))

[root@localhost scripts]# sum=2
[root@localhost scripts]# sum=sum+8
[root@localhost scripts]# echo $sum
sum+8
[root@localhost scripts]# unset sum
[root@localhost scripts]# sum=2
[root@localhost scripts]# let sum=sum+8
[root@localhost scripts]# echo $sum
10

4,小数运算

bc是UNIX/linux下的计算器,用于交互和非交互。

1.11,变量的删除,替换和替代

1,删除

image.png

[root@localhost scripts]# file=/dir1/dir2/dir3/my.file.txt
[root@localhost scripts]# echo $file
/dir1/dir2/dir3/my.file.txt
[root@localhost scripts]# echo ${file#*/}
dir1/dir2/dir3/my.file.txt
[root@localhost scripts]# echo ${file##*/}
my.file.txt
[root@localhost scripts]# echo ${file%/*}
/dir1/dir2/dir3
[root@localhost scripts]# echo ${file%%/*}

2,替换

image.png

[root@localhost scripts]# var=www.baidu.com
[root@localhost scripts]# echo ${var/baidu/qfedu}
www.qfedu.com
[root@localhost scripts]# var=www.sina.com.cn
[root@localhost scripts]# echo ${var//c/C}
www.sina.Com.Cn

3,替代:如果变量名没有被赋值,则会使用“新的变量值”,如果变量已被赋值(包括空值),则不会被替代。

#格式:$(变量名-新的变量值)
[root@localhost scripts]# unset port		//取消变量,确认port没有被赋值
[root@localhost scripts]# echo ${port-3306}
3306
[root@localhost scripts]# port=3307
[root@localhost scripts]# echo ${port-3306}
3307

1.12,变量的自增

i++:先赋值后自加。

++i:先自加后赋值。

#对变量值的影响
[root@localhost scripts]# i=1
[root@localhost scripts]# let i++
[root@localhost scripts]# echo $i
2
[root@localhost scripts]# j=1
[root@localhost scripts]# let ++j
[root@localhost scripts]# echo $j
2

#对表达式的影响
[root@localhost scripts]# unset i
[root@localhost scripts]# unset j
[root@localhost scripts]# i=1
[root@localhost scripts]# j=1
[root@localhost scripts]# let x=i++
[root@localhost scripts]# let y=++j
[root@localhost scripts]# echo $i
2
[root@localhost scripts]# echo $j
2
[root@localhost scripts]# echo $x
1
[root@localhost scripts]# echo $y
2

1.13,变量中的特殊符号

1,#:表注释。(#!表示找解释器)

2,;:在同一行中分隔两个及以上命令。(循环中也有)

3,;;:用于终止case选项。

4,.:等价于source命令。

  • 如果放开头表示隐藏文件。
  • .表当前目录
  • …表上一级目录

**5,:**和true命令作用相同(只设置退出码为0)

6, ∗ ∗ : 表示替换或引用变量的内容( ∗ ∗ **:表示替换或引用变量的内容(** :表示替换或引用变量的内容(在变量前表引用变量值;$在行尾表行结束符image.png

image.png

  • 24
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值