@解释器Bash shell基础

1|0Bash shell基础

1|2一.介绍

1|0类比:

shell 语法=========python语法 bash解释器==========Python解释器 #shell是一门解释型,弱类型,动态语言

1|3二、变量

1|01、什么是变量

量:记录下的事物的状态 变:事物的状态是可以发生变的 #变量是一种存取内存的机制

1|02、为何要用变量

#为了让计算机能够像人一样记下事物的状态,记不是目的,目的是为了以后取出来用 #并且事物的状态是可以改变的

1|03、如何用变量

#原则:先定义,后引用
[root@localhost ~]# x=1 [root@localhost ~]# x=2 [root@localhost ~]# echo $x 2 [root@localhost ~]# name1=egon [root@localhost ~]# echo $name1 egon [root@localhost ~]# echo ${name1} egon [root@localhost ~]# unset name1 [root@localhost ~]# echo $name1 [root@localhost ~]# [root@localhost ~]# domain=www.baidu.com [root@localhost ~]# ping -c1 $domain [root@localhost ~]# name=egon [root@localhost ~]# echo "hello $name" hello egon
1|0示列:
[root@localhost ~]# vim heartbeat.sh [root@localhost ~]# cat heartbeat.sh #!/bin/bash ip=192.168.11.20 ping -c1 $ip &>/dev/null if [ $? = 0 ];then echo "host $ip is alive" else echo "host $ip is down!!!" fi [root@localhost ~]# chmod +x heartbeat.sh [root@localhost ~]# ./heartbeat.sh host 192.168.11.20 is down!!!

1|4三、引号对变量的影响

1|01.双引号:表示弱引号

[root@localhost ~]# name=egon [root@localhost ~]# echo "hello $name"

1|02.双引号:表示强引号

[root@localhost ~]# echo 'hello $name' hello $name

1|03.反引号:表示只取结果

[root@localhost ~]# today=`date +%F` [root@localhost ~]# echo $today 2020-08-11 [root@localhost ~]# today=$(date +%H:%M:%S) [root@localhost ~]# echo $today 22:05:55 示列: [root@localhost ~]# tar czf `date +%F`_bak.tar.gz /tmp

1|04.注意:对于变量包含空格时,应需加上双引号包含

[root@localhost ~]# msg="hello egon" [root@localhost ~]# echo $msg hello egon

1|5四.变量的作用域

1|01.环境变量:在当前shell及子shell生效

1|02.自定义变量: 仅在当前shell中生效

[root@localhost ~]# x=1 [root@localhost ~]# export x [root@localhost ~]# bash [root@localhost ~]# echo $x 1
set 查看所有变量(包含自定义变量和环境变量) env 查看环节变量

1|03.系统环境变量所需要配置的文件(系统定义的)

̵/etc/profile /etc/bashrc /.bashrc /.bash_profile

1|05.系统环境变量

[root@localhost ~]# echo $PS1 [\u@\h \W]\$ [root@localhost ~]# echo $HOSTNAME localhost.localdomain [root@localhost ~]# echo $USER root [root@localhost ~]# echo $UID 0 [root@localhost ~]# echo $SHELL /bin/bash [root@localhost ~]# echo $HISTSIZE 5 [root@localhost ~]# echo $MAIL /var/spool/mail/root [root@localhost ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

1|6五.影响bash shell的一些文件

1|01.系统环境变量配置文件:
̵/etc/profile /etc/bashrc /.bashrc /.bash_profile
#以上系统配置文件登录shell执行顺序:su egon
login shell: 1. /etc/profile #系统级别配置文件 2. ~/.bash_profile #用户级别的配置文件(用户定义的环境变量) 3. ~/.bashrc #用户级别(定义别名) 4. /etc/bashrc #系统级
#在不登录时,执行两个文件顺序:su egon
non-login shell: 1. /.bashrc #用户级 2. /etc/bashrc #系统级
#影响bash shell的其他文件,bash登录界面和欢迎界面
/etc/motd #登录后显示的信息 /etc/issue #登录前显示的信息(本地) /etc/issue.net #登录前显示显示的信息(网络登)
[root@localhost ~]# cat /etc/motd +--------------------------------------------+ | | | 登录时显示的设置内容 | | | +--------------------------------------------

1|7六. 元字符

1|01.shell语法中的特殊字符:

1|01>. ``与 ( ):取值的结果
[root@localhost ~]# echo `pwd` /root [root@localhost ~]# echo $(pwd) /root 区别:$()可以嵌套,而``不能嵌套 [root@localhost ~]# echo $(ls $(pwd))
1|02>.~ 表示家目录
1|03>. . 与…
1|04>. !取反
root@localhost ~]# touch /test/{1.txt,2.txt,a.txt,aaa_bbb.txt} [root@localhost ~]# find /test ! -name 1.txt /test /test/2.txt /test/a.txt /test/aaa_bbb.txt [root@localhost ~]# ls /test/[!0-9].txt /test/a.txt [root@localhost# ls /test/[^0-9].txt /test/a.txt
1|05>.@无特殊意义
1|06>.#注释
1|07>.$取值变量
[root@localhost ~]# x=1 [root@localhost ~]# echo $x 1
1|08>.% 、-、+、用算符
#bc是常用的linux计算工具,支持浮点型 [root@localhost ~]# res=`echo 1+1 | bc` [root@localhost ~]# echo $res 2
[root@localhost ~]# res=`echo 10 % 3 | bc` [root@localhost ~]# echo $res 1 [root@localhost ~]# res=`echo 1.2+1.3|bc` [root@localhost ~]# echo $res 2.5 [root@localhost ~]# res=`echo 5.0+3.0|bc` [root@localhost ~]# echo $res 8.0 [root@localhost ~]# res=`echo "scale=2;5.0/3.0"|bc` [root@localhost ~]# echo $res 1.66 [root@localhost ~]# res=`echo "scale=2;5.0/6.0"|bc` [root@localhost ~]# echo $res .83
#expr不支持浮点型计算,且注意数字与运算符中的空格 [root@localhost ~]# res=`expr 5 / 3` [root@localhost ~]# echo $res 1 [root@localhost ~]# res=`expr 1+1` #注意要有空格 [root@localhost ~]# echo $res 1+1 [root@localhost ~]# res=`expr 1 + 1` [root@localhost ~]# echo $res 2
#$,(())同expr,不支持浮点型运算 [root@localhost ~]# echo $((1+1)) 2 [root@localhost ~]# echo $((1.0+2.0)) -bash: 1.0+2.0: 语法错误:无效的算数运算符(".0+2.0"
# let 不支持浮点数运算,而且不支持直接输出,只能赋值 [root@localhost ~]# let res=1+1 [root@localhost ~]# echo $res 2 [root@localhost ~]# [root@localhost ~]# let res=50/5 [root@localhost ~]# echo $res 10 [root@localhost ~]# let c=1.3*3 -bash: let: c=1.3*3: 语法错误:无效的算数运算符( ".3*3"
1|09>. ^ 同!一样
1|010>. *表示任意多个字符
[root@localhost ~]# touch 1.txt 2.txt aa.txt aaa.txt [root@localhost ~]# rm -rf *.txt [root@localhost ~]# touch 1.txt 2.txt aa.txt aaa.txt a1c.txt [root@localhost ~]# ls *.txt 1.txt 2.txt a1c.txt aaa.txt aa.txt
1|011>.()表示在子shell中执行
[root@localhost ~]# (x=1) [root@localhost ~]# echo $x ଫአ [root@localhost ~]# (umask 066;touch a.txt) # umask的设置只在子shell中有效 [root@localhost ~]# ll a.txt -rw-------. 1 root root 0 8์月 13 15:22 a.txt [root@localhost ~]# touch b.txt [root@localhost ~]# ll b.txt -rw-r--r--. 1 root root 0 8์月 13 15:23 b.txt
1|012>. _下划线:无特殊意义。可用于名字的声明
1|013>.= 等号:赋值,判断相等性
[root@localhost ~]# [ 1 = 1 ] #条件1 = 1的左右两边必须有空格 [root@localhost ~]# echo $? # 判断上一条命令结果是否为真0=̾true 0
1|014>.|管道:把一个进程的结果传递给另一个进程
[root@localhost ~]# ps aux | grep python xargs参数传递 [root@localhost ~]# find /home/ -type d -name "test*" |xargs ls 1.txt 2.txt 3.txt [root@localhost ~]# ls /home/test 1.txt 2.txt 3.txt
1|015>.\转移特殊字符
[root@localhost ~]# mkdir a\ b.txt [root@localhost ~]# ll 总用量 0 drwxr-xr-x. 2 root root 6 8์ 13 15:35 a b.txt [root@localhost ~]# echo $RMB #默认会当成变量 [root@localhost ~]# echo '$RMB' #取消特殊意义 $RMB [root@localhost ~]# echo \$RMB #取消特殊意义 $RMB
1|016>.[ ]条件测试
[root@localhost ~]# name="egon" [root@localhost ~]# [ $name = "egon" ];echo $? 0 [root@localhost ~]# name="adf" [root@localhost ~]# [ $name = "egon" ];echo $? 1 [root@localhost ~]# [ -d /test ];echo $?
1|017>.’'引号
'' #表示强引用 " " #弱引用 [root@localhost ~]# x=111 [root@localhost ~]# echo "$x" 111 [root@localhost ~]# echo '$x' $x
1|018>. ; 与&& 与 || 连接多条命名
[root@localhost home]# gagaga;ls # 无论前一条命令执行成功与否,都会执行后续命令 bash: gagaga: #未找到命令 egon [root@localhost home]# gagaga && ls #只有前面一条命令执行成功后,才会执行后面一条命令 bash: gagaga: #未找到命令 [root@localhost home]# ls /test || mkdir /test #前面一条命令执行不成功,才会执行后面一条命令 0.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
1|019>./路径分隔符
1|020>. { }:指示包含
[root@localhost home]# touch /test/{0..9}.txt [root@localhost home]# ls /test/ 0.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
1|021>.&表示后台运行
[root@localhost home]# echo "hello";sleep 3;echo "world" &
1|022>.重定向
> >> #输出重定向 < << #输入重定向 > #覆盖 >> #追加 [root@localhost home]# cat >> a.txt << EOF > 111 > 222 > 333 > EOF 0#标准输入 1#标准正确输出 2#标准错误输出 &#标准正确和错误输出 [root@localhost home]# pwd 1>a.txt [root@localhost home]# cat a.txt /home [root@localhost home]# gagag 2>a.txt [root@localhost home]# cat a.txt bash: gagag: #命令未找到 [root@localhost home]# gagaga &>/dev/null < << #输出重定向 [root@localhost ~]# mysql -uroot -p123 < bbs.sql [root@localhost home]# grep root < /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost home]# dd if=/dev/zero of=/a.txt bs=1M count=10 #记录了10+0 的读入 #记录了10+0 的写出 1048576字节(10 MB)҅已复制,0.024387 秒,430 MB/秒 [root@localhost home]# dd </dev/zero >/b.txt bs=1M count=10 #记录了10+0 的读入 #记录了10+0 的写出 10485760字节(10 MB)҅已复制,0.0202365 秒,518MB/秒 [root@localhost home]# ll /a.txt -rw-r--r--. 1 root root 10485760 8์ 13 16:02 /a.txt [root@localhost home]# ll /b.txt -rw-r--r--. 1 root root 10485760 8์ 13 16:03 /b.txt
1|023>.?任意一个字符
[root@localhost ~]# ls ??.txt aa.txt [root@localhost ~]# ls a?c.txt a1c.txt [root@localhost ~]# rm -rf *.txt
1|024.范围中的任意一个字符 [12] [ac] [a-z] [0-9]
[root@localhost ~]# touch a1c a2c axc aXc axd [root@localhost ~]# ls a?c a1c a2c axc aXc [root@localhost ~]# ls a[1x]c a1c axc [root@localhost ~]# ls a[a-z]c axc aXc [root@localhost ~]# ls a[A-Z]c # ӧړ܄य़ੜٟ axc aXc [root@localhost ~]# ls a[x]c axc [root@localhost ~]# ls a[X]c aXc [root@localhost ~]# ls a[0-9]c a1c a2c [root@localhost ~]# ls /dev/sd[a-z]* /dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb1

1|8七 运行shell程序的两种方式

1|01、交互式环境
每敲一条立即执行一条 不能永久保存代码
1|02、把shell命令写入文件中,该文件称之为脚本文件
bash 脚本文件的路径 #运行shell脚本经历的三个阶段 (1)先启动bash解释器 (2)bash解释器会将脚本文件的内容从硬盘读入内存 (3)bash解释器会解释执行刚刚读入内存的代码,识别语法 chmod +x nginx_install.sh ./nginx_install.txt # 开启一个子shell来运行程序

1|9八. Bash SHELL 基础

1|01.案列:
[root@localhost ~]# vim if.sh [root@localhost ~]# cat if.sh #!/usr/bin/env bash domain="www.baidu.com" ping -c2 $domain &>/dev/null if [ $? = 0 ];then echo "network is ok" else echo "network is down!!!" fi [root@localhost ~]# chmod +x if.sh [root@localhost ~]# ./if.sh network is ok
1|02.案列:
[root@localhost ~]# vim for.sh [root@localhost ~]# ll for.sh -rw-r--r--. 1 root root 61 8์ 13 16:17 for.sh [root@localhost ~]# . for.sh egon tom jack
1|03.案列:
[root@localhost ~]# vim for1.sh [root@localhost ~]# cat for1.sh #!/bin/bash for i in {1..5} do echo $i done [root@localhost ~]# source for1.sh 1 2 3 4 5
1|04.案列:
[root@localhost ~]# vim for2.sh [root@localhost ~]# cat for2.sh #!/bin/bash for i in {1..10} do ip=192.168.12.$i ping -c1 $ip &>/dev/null if [ $? = 0 ];then echo "$i is up" else echo "$i is down!!!" fi done [root@localhost ~]# vim for2.sh [root@localhost ~]# source for2.sh 192.168.12.1 is up 192.168.12.2 is down!!! 192.168.12.3 is up 192.168.12.4 is down!!! ......
#完毕!

__EOF__

本文作者ଲ小何才露煎煎饺
本文链接https://www.cnblogs.com/zeny/p/15121593.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   ଲ小何才露煎煎饺  阅读(51)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
点击右上角即可分享
微信分享提示