Bash shell基础
一.介绍
类比:
shell 语法== == == == = python语法
bash解释器== == == == == Python解释器
二、变量
1、什么是变量
量:记录下的事物的状态
变:事物的状态是可以发生变的
2、为何要用变量
3、如何用变量
[ root@localhost ~]
[ root@localhost ~]
[ root@localhost ~]
2
[ root@localhost ~]
[ root@localhost ~]
egon
[ root@localhost ~]
egon
[ root@localhost ~]
[ root@localhost ~]
[ root@localhost ~]
[ root@localhost ~]
[ root@localhost ~]
[ root@localhost ~]
[ root@localhost ~]
hello egon
示列:
[ root@localhost ~]
[ root@localhost ~]
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 ~]
[ root@localhost ~]
host 192.168.11.20 is down! ! !
三、引号对变量的影响
1.双引号:表示弱引号
[ root@localhost ~]
[ root@localhost ~]
2.双引号:表示强引号
[ root@localhost ~]
hello $name
3.反引号:表示只取结果
[ root@localhost ~]
[ root@localhost ~]
2020-08-11
[ root@localhost ~]
[ root@localhost ~]
22:05:55
示列:
[ root@localhost ~]
4.注意:对于变量包含空格时,应需加上双引号包含
[ root@localhost ~]
[ root@localhost ~]
hello egon
四.变量的作用域
1.环境变量:在当前shell及子shell生效
2.自定义变量: 仅在当前shell中生效
[ root@localhost ~]
[ root@localhost ~]
[ root@localhost ~]
[ root@localhost ~]
1
set 查看所有变量(包含自定义变量和环境变量)
env 查看环节变量
3.系统环境变量所需要配置的文件(系统定义的)
̵/etc/profile
/etc/bashrc
/.bashrc
/.bash_profile
5.系统环境变量
[ root@localhost ~]
[ \u@\h \W] \$
[ root@localhost ~]
localhost.localdomain
[ root@localhost ~]
root
[ root@localhost ~]
0
[ root@localhost ~]
/bin/bash
[ root@localhost ~]
5
[ root@localhost ~]
/var/spool/mail/root
[ root@localhost ~]
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
五.影响bash shell的一些文件
1.系统环境变量配置文件:
̵/etc/profile
/etc/bashrc
/.bashrc
/.bash_profile
login shell:
1. /etc/profile
2. ~/.bash_profile
3. ~/.bashrc
4. /etc/bashrc
non-login shell:
1. /.bashrc #用户级
2. /etc/bashrc #系统级
/etc/motd
/etc/issue
/etc/issue.net
[ root@localhost ~]
+--------------------------------------------+
| |
| 登录时显示的设置内容
|
| |
+--------------------------------------------
六. 元字符
1.shell语法中的特殊字符:
1>. ``与 ( ):取值的结果
[ root@localhost ~]
/root
[ root@localhost ~]
/root
区别:$( ) 可以嵌套,而``不能嵌套
[ root@localhost ~]
2>.~ 表示家目录
3>. . 与…
4>. !取反
root@localhost ~]
[ root@localhost ~]
/test
/test/2.txt
/test/a.txt
/test/aaa_bbb.txt
[ root@localhost ~]
/test/a.txt
[ root@localhost
/test/a.txt
5>.@无特殊意义
6>.#注释
7>.$取值变量
[ root@localhost ~]
[ root@localhost ~]
1
8>.% 、-、+、用算符
[ root@localhost ~]
[ root@localhost ~]
2
[ root@localhost ~]
[ root@localhost ~]
1
[ root@localhost ~]
[ root@localhost ~]
2.5
[ root@localhost ~]
[ root@localhost ~]
8.0
[ root@localhost ~]
[ root@localhost ~]
1.66
[ root@localhost ~]
[ root@localhost ~]
.83
[ root@localhost ~]
[ root@localhost ~]
1
[ root@localhost ~]
[ root@localhost ~]
1+1
[ root@localhost ~]
[ root@localhost ~]
2
[ root@localhost ~]
2
[ root@localhost ~]
-bash: 1.0+2.0: 语法错误:无效的算数运算符(".0+2.0" )
[ root@localhost ~]
[ root@localhost ~]
2
[ root@localhost ~]
[ root@localhost ~]
[ root@localhost ~]
10
[ root@localhost ~]
-bash: let: c= 1.3*3: 语法错误:无效的算数运算符( ".3*3" )
9>. ^ 同!一样
10>. *表示任意多个字符
[ root@localhost ~]
[ root@localhost ~]
[ root@localhost ~]
[ root@localhost ~]
1.txt 2.txt a1c.txt aaa.txt aa.txt
11>.()表示在子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
12>. _下划线:无特殊意义。可用于名字的声明
13>.= 等号:赋值,判断相等性
[ root@localhost ~]
[ root@localhost ~]
0
14>.|管道:把一个进程的结果传递给另一个进程
[ root@localhost ~]
xargs参数传递
[ root@localhost ~]
1.txt 2.txt 3.txt
[ root@localhost ~]
1.txt 2.txt 3.txt
15>.\转移特殊字符
[ root@localhost ~]
[ root@localhost ~]
总用量 0
drwxr-xr-x. 2 root root 6 8์ 13 15:35 a b.txt
[ root@localhost ~]
[ root@localhost ~]
$RMB
[ root@localhost ~]
$RMB
16>.[ ]条件测试
[ root@localhost ~]
[ root@localhost ~]
0
[ root@localhost ~]
[ root@localhost ~]
1
[ root@localhost ~]
17>.’'引号
''
" "
[ root@localhost ~]
[ root@localhost ~]
111
[ root@localhost ~]
$x
18>. ; 与&& 与 || 连接多条命名
[ root@localhost home]
bash: gagaga:
egon
[ root@localhost home]
bash: gagaga:
[ root@localhost home]
0.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
19>./路径分隔符
20>. { }:指示包含
[ root@localhost home]
[ root@localhost home]
0.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
21>.&表示后台运行
[ root@localhost home]
22>.重定向
> >>
< <<
>
>>
[ root@localhost home]
> 111
> 222
> 333
> EOF
0
1
2
&
[ root@localhost home]
[ root@localhost home]
/home
[ root@localhost home]
[ root@localhost home]
bash: gagag:
[ root@localhost home]
< <<
[ root@localhost ~]
[ root@localhost home]
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[ root@localhost home]
1048576字节( 10 MB) ҅已复制,0.024387 秒,430 MB/秒
[ root@localhost home]
10485760字节( 10 MB) ҅已复制,0.0202365 秒,518MB/秒
[ root@localhost home]
-rw-r--r--. 1 root root 10485760 8์ 13 16:02 /a.txt
[ root@localhost home]
-rw-r--r--. 1 root root 10485760 8์ 13 16:03 /b.txt
23>.?任意一个字符
[ root@localhost ~]
aa.txt
[ root@localhost ~]
a1c.txt
[ root@localhost ~]
24.范围中的任意一个字符 [12] [ac] [a-z] [0-9]
[ root@localhost ~]
[ root@localhost ~]
a1c a2c axc aXc
[ root@localhost ~]
a1c axc
[ root@localhost ~]
axc aXc
[ root@localhost ~]
axc aXc
[ root@localhost ~]
axc
[ root@localhost ~]
aXc
[ root@localhost ~]
a1c a2c
[ root@localhost ~]
/dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb1
七 运行shell程序的两种方式
1、交互式环境
每敲一条立即执行一条
不能永久保存代码
2、把shell命令写入文件中,该文件称之为脚本文件
bash 脚本文件的路径
#运行shell脚本经历的三个阶段
(1)先启动bash解释器
(2)bash解释器会将脚本文件的内容从硬盘读入内存
(3)bash解释器会解释执行刚刚读入内存的代码,识别语法
chmod +x nginx_install.sh
./nginx_install.txt # 开启一个子shell来运行程序
八. Bash SHELL 基础
1.案列:
[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
2.案列:
[ root@localhost ~]
[ root@localhost ~]
-rw-r--r--. 1 root root 61 8์ 13 16:17 for.sh
[ root@localhost ~]
egon
tom
jack
3.案列:
[ root@localhost ~]
[ root@localhost ~]
for i in { 1.. 5}
do
echo $i
done
[ root@localhost ~]
1
2
3
4
5
4.案列:
[ root@localhost ~]
[ root@localhost ~]
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 ~]
[ root@localhost ~]
192.168.12.1 is up
192.168.12.2 is down! ! !
192.168.12.3 is up
192.168.12.4 is down! ! !
.. .. ..