一.shell
[root@room9pc01 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@room9pc01 ~]# head -1 /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@room9pc01 ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
[root@room9pc01 ~]# sh
sh-4.2#
[root@room9pc01 ~]# pstree
systemd─┬─ModemManager───2*[{ModemManager}]
├─gnome-terminal-─┬─bash───sh───bash───pstree
---------------------------------------------------------------------------------------------------
[root@room9pc01 ~]# ssh 127.0.0.1
├─sshd───sshd───bash───pstree
######################################################
[root@room9pc01 ~]# ksh (没有tab键,也没有光标左右)
#
#########################################################
[root@room9pc01 ~]# sh
sh-4.2# ls --color (有颜色)
- jdk-8u231-linux-x64.tar.gz original-ks.cfg user 文档
1.doc lnmp_soft.tar.gz PycharmProjects 公共 下载
anaconda-ks.cfg nginx-1.12.2 R 模板 音乐
bin nginx-1.12.2.tar.gz SamsungPortableSSD.exe 视频 桌面
debian-binary opt tabdir 图片
sh-4.2# ls (无颜色)
- jdk-8u231-linux-x64.tar.gz original-ks.cfg user 文档
1.doc lnmp_soft.tar.gz PycharmProjects 公共 下载
anaconda-ks.cfg nginx-1.12.2 R 模板 音乐
bin nginx-1.12.2.tar.gz SamsungPortableSSD.exe 视频 桌面
debian-binary opt tabdir 图片
######################################################################
在opt里面写,不怕错删
[root@room9pc01 ~]# cd /opt
[root@room9pc01 opt]# ls
123 abc hn.txt lnmp_soft pysoft test01.sh time.txt
1.txt google hs.txt node_tedu.xml rh test02.sh xys
aa hh kingsoft NSD-2018-1-12 shadow test03.sh
[root@room9pc01 opt]# rm -rf * (全删)
[root@room9pc01 opt]# ls
_______________________________
脚本的执行:
1.[[root@room9pc01 opt]# chmod +x test01.sh (赋予x权限)
[root@room9pc01 opt]# /opt/test01.sh (用绝对路径)
abc (生产环境中用)
2.[root@room9pc01 opt]# bash test01.sh (不需要x权限)
abc (测试环境用)
3.[root@room9pc01 opt]# source test01.sh (用当前解释器解释) 可以将source换成 '.'
abc
$$$$$$$$$$$$$$$$$$$$$$
[root@room9pc01 opt]# cat test01.sh
#!/bin/bash
#
echo abc
exit
1. [root@room9pc01 opt]# bash test01.sh
abc
[root@room9pc01 opt]# (退出了bash)
2. [root@room9pc01 ~]# source test01.sh (会退出当前)
-------------------------------------------------------------------------
案例 ,YUM仓库
1.删除YUM
[root@room9pc01 opt]# cd /etc/yum.repos.d/
[root@room9pc01 yum.repos.d]# ls
CentOS.repo haha repo
[root@room9pc01 yum.repos.d]# mv CentOS.repo CentOS7.5 (删除或者改名)
2.
[root@room9pc01 yum.repos.d]# cat CentOS7.5
[CentOS7]
name=CentOS 7
baseurl=file:///var/ftp/centos-1804
enabled=1
gpgcheck=0
[root@room9pc01 opt]# cat test03.sh
#!/bin/bash
#搭建yum仓库
echo "[test]
name=test
baseurl=file:///var/ftp/centos-1804
enabled=1
gpgcheck=0"> /etc/yum.repos.d/xxx.repo
[root@room9pc01 opt]# bash test03.sh
[root@room9pc01 opt]# cd /etc/yum.repos.d/
[root@room9pc01 yum.repos.d]# ls
CentOS7.5 haha repo xxx.repo
[root@room9pc01 yum.repos.d]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sda2 103080888 20330320 77491304 21% /
devtmpfs 8137428 0 8137428 0% /dev
tmpfs 8155428 278608 7876820 4% /dev/shm
tmpfs 8155428 26708 8128720 1% /run
tmpfs 8155428 0 8155428 0% /sys/fs/cgroup
/dev/sda1 307084240 45266752 246195344 16% /var/lib/libvirt/images
/dev/loop0 9176232 9176232 0 100% /var/ftp/centos-1804
tmpfs 1631088 56 1631032 1% /run/user/0
[root@room9pc01 yum.repos.d]# yum repolist
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
源标识 源名称 状态
CentOS7 CentOS 7 9,911
test test 9,911
repolist: 19,822
---------------------------------------------------------------------------------------------------
搭建vsftpd
[root@room9pc01 opt]# cat test04.sh
#!/bin/bash
#vtftpd
yum -y install vsftpd &> /dev/null #将信息扔到黑洞
systemctl restart vsftpd
systemctl enable vsftpd
[root@room9pc01 opt]# systemctl status vsftpd
● vsftpd.service - Vsftpd ftp daemon
Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; enabled; vendor preset: disabled)
Active: active (running) since 四 2020-04-09 11:53:31 CST; 5min ago
Main PID: 15270 (vsftpd)
CGroup: /system.slice/vsftpd.service
└─15270 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
4月 09 11:53:31 room9pc01.tedu.cn systemd[1]: Starting Vsftpd ftp daemon...
4月 09 11:53:31 room9pc01.tedu.cn systemd[1]: Started Vsftpd ftp daemon.
##########################################
[root@room9pc01 opt]# echo ${a}RMB ( 加 {} 后, 只对$后的 {} 中的符号进行别名调用)
10RMB
[root@room9pc01 opt]# unset a
[root@room9pc01 opt]# a= (两种删除方法)
2.环境变量 UID PWD HOME PATH SHELL PS1 PS2
[root@room9pc01 opt]# echo $USER
root
[lisi@room9pc01 root]$ echo $USER
lisi
[root@room9pc01 opt]# echo $PS1 (一级提示符)
[\u@\h \W]\$
[root@room9pc01 opt]# sh
sh-4.2# echo $PS1
\s-\v\$
dachuiPS1='[\u@\h \W]\$' (引号)
[root@room9pc01 opt]# (还原)3
-------------------------------------------------------------------------
3.预定义变量与位置变量
预定义变量 :$0 $# $^ $$ $?
位置变量 : $1 $2 $3
[root@room9pc01 opt]#cat test02.sh\
>
#!/bin/bash
#预定义变量的使用
echo $1
echo $2
echo $3
echo $0 执行脚本的名称
echo $# 参数数量
echo $* 所有位置变量
echo $$ 进程号
echo $? 上一条任务有没有执行成功 [ 0就是成功 ,非0 失败]
[root@room9pc01 opt]#bash test02.sh a b c d
a
b
c
test02.sh
4
a b c d
21415
0
-----------------------------------------------------------------
env 查看大部分环境变量
set 查看所有变量 (自定义变量)
[root@room9pc01 opt]#env | grep HOSTNAME (最好结合grep,不然全部都显示)
HOSTNAME=room9pc01.tedu.cn
----------------------------------------------------------------------------
变量的扩展应用
1.引号
"" 双引号 : 界定范围
'' 界定范围 \ 屏蔽特殊符号
`` 或$() 调用命令的执行结果 (一般用() 因为`` 比较小)
[root@room9pc01 opt]#ls
test01.sh test02.sh test03.sh test04.sh test05.sh test06.sh
[root@room9pc01 opt]#touch a b\
>
[root@room9pc01 opt]#ls
a b test01.sh test02.sh test03.sh test04.sh test05.sh test06.sh
[root@room9pc01 opt]#touch "a b"
[root@room9pc01 opt]#ls
a a b b test01.sh test02.sh test03.sh test04.sh test05.sh test06.sh
------------------------------------------------
[root@room9pc01 opt]#echo "$a"
10
[root@room9pc01 opt]#echo '$a'
$a
------------------------------------------
[root@room9pc01 opt]#a=`date +%F`
[root@room9pc01 opt]#echo $a
2020-04-09
####################3
2.read指令
read -p ' ' (空格) x (将输入的结果赋值给x)
stty -echo 屏蔽回显
stty echo 恢复回显
3.全局变量
一般都是局部变量,但是子进程,和他程序都不能识别
[root@room9pc01 opt]# a=20
[root@room9pc01 opt]# export a
[root@room9pc01 opt]# export b=10
root@room9pc01 opt]# export -n a (取消全局变量)
###################################################
运算
1. expr 1 + 1 2 - 1 2 `*` 2 2 / 2 2 \* 2 ( \转义符 .可以屏蔽一个符号) 2 % 2 运算符号两边有space
2.echo $[1+1]
3.let c=1+1 a++ a-- (用于处理变量)
4.bc (可以用小数) echo "scale=3;45*85" | bc (scale是位数)