10月5日 学习预习笔记 shell脚本

shell介绍

什么是shell

shell是一个命令解释器,提供用户和机器之间的交互
支持特定语法,比如逻辑判断、循环
每个用户都可以有自己的特定shell
CentOS7默认shell为bash(Bourne Agin Shell)
还有zsh、ksh等

history 命令历史

-c 只能清空内存中历史命令,不能清空历史命令文件

历史命令配置文件
用户家目录下
[root@localhost ~]# ls /root/.bash_history
/root/.bash_history

最大记录历史命令1000条
当退出终端连接时,history记录的命令才会被记录到配置文件中

修改最大记录
[root@localhost ~]# vim /etc/profile 修改环境变量配置文件

找到HISTSIZE修改后面的数值
HOSTNAME=/usr/bin/hostname 2>/dev/null
HISTSIZE=1000
if [ “$HISTCONTROL” = “ignorespace” ] ; then

刷新环境变量
[root@localhost ~]# source /etc/profile

修改HISTSIZE使之增加环境时间
[root@localhost ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M%:%S"

查看修改是否生效
[root@localhost ~]# history
1 2018/11/04 17:59%:41df -h
2 2018/11/04 17:59%:41df
3 2018/11/04 17:59%:41df man
4 2018/11/04 17:59%:41man df
5 2018/11/04 17:59%:41man ls
6 2018/11/04 17:59%:41free
7 2018/11/04 17:59%:41free man
8 2018/11/04 17:59%:41man df

此时修改只对当前连接有效,再次打开其他连接时不生效,若要针对整个系统起作用需修改环境变量

编辑环境变量配置文件
[root@localhost ~]# vim /etc/profile

插入变量命令
HOSTNAME=/usr/bin/hostname 2>/dev/null
HISTSIZE=5000
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 将本条命令加入配置文件中
if [ “$HISTCONTROL” = “ignorespace” ] ; then

刷新配置文件
[root@localhost ~]# source /etc/profile

永久记录历史命令,加特殊权限+a 只可追加不可删除
[root@localhost ~]# chattr +a ~/.bash_history

显示上一条命令
[root@localhost ~]# !!

指定使用以往命令
[root@localhost ~]# !98 指定使用记录中第98条命令
cp /etc/dnsmasq.conf /tmp/1.txt
cp:是否覆盖"/tmp/1.txt"?

centos7中tab不仅可以补全命令,还可以补全参数,需bash-completion
安装rpm包
[root@localhost ~]# yum -y install bash-completion

自定义别名restartnet
[root@localhost ~]# alias restartnet=“systemctl restart network.service”

删除别名restartnet
[root@localhost ~]# unalias restartnet

alias
系统默认alias别名文件目录
/etc/profile.d/

[root@localhost ~]# ls /etc/profile.d/
256term.csh colorgrep.sh lang.csh sh.local which2.sh
256term.sh colorls.csh lang.sh vim.csh
abrt-console-notification.sh colorls.sh less.csh vim.sh
bash_completion.sh csh.local less.sh vte.sh
colorgrep.csh flatpak.sh PackageKit.sh which2.csh

自定义脚本
~/.bashrc

通配符

*.txt 查找txt结尾的文件
.txt 查找文件名中有txt字符的文件

查找txt结尾的文件
[root@localhost ~]# ls *txt
新建文本文档_(2).txt

55.txt:
21.txt

查找文件名中有txt字符的文件
[root@localhost ~]# ls txt
12.txt.gz 新建文本文档_(2).txt

55.txt:
21.txt

?.txt 查找所有txt结尾文件
[23].txt

查找所有txt结尾文件
[root@localhost ~]# ls ?.txt
1.txt 2.txt 3.txt

查找支持(0-9)(a-z)
[root@localhost ~]# ls [0-6a-z].txt
1.txt 2.txt 3.txt 5.txt a.txt s.txt y.txt
[root@localhost ~]# ls [a-z].txt
a.txt s.txt y.txt
[root@localhost ~]# ls [123].txt
1.txt 2.txt 3.txt

{}花括号查询文件
[root@localhost ~]# ls {1,2,a,s}.txt
1.txt 2.txt a.txt s.txt

输入输出重定向

cat 1.txt > 2.txt 输出定向 将1.txt内容输出到2.txt中,此操作会覆盖原来的内容
cat 1.txt >> 2.txt 输出追加定向 将1.txt内容追加到2.txt中
lsad 2> 3.txt 输出错误定向 将lsad错误命令输出到3.txt
lsad 2>> 3.txt 输出错误追加重定向 将lsad错误命令输出追加到3.txt

+2>=&> 结合正确和错误输出

将1.txt内容输出到2.txt中
[root@localhost ~]# echo wefefefefegwgwf > 1.txt
[root@localhost ~]# cat 1.txt
wefefefefegwgwf
[root@localhost ~]# echo wefefefefegw3434535gwf > 2.txt
[root@localhost ~]# cat 2.txt
wefefefefegw3434535gwf
[root@localhost ~]# cat 1.txt > 2.txt
[root@localhost ~]# cat 2.txt
wefefefefegwgwf

将1.txt内容追加到2.txt中
[root@localhost ~]# cat 1.txt >> 2.txt
[root@localhost ~]# cat 2.txt
wefefefefegwgwf
wefefefefegwgwf

将lsad错误命令输出到3.txt
[root@localhost ~]# lsad 2> 3.txt
[root@localhost ~]# cat 3.txt
bash: lsad: 未找到命令…

将lsad错误命令输出追加到3.txt
[root@localhost ~]# lsad 2>> 3.txt
[root@localhost ~]# cat 3.txt
bash: lsad: 未找到命令…
bash: lsad: 未找到命令…

将1.txt 2.txt与不存在的aa.txt输出到a.txt
[root@localhost ~]# ls [12].txt aa.txt &> a.txt
[root@localhost ~]# cat a.txt
ls: 无法访问aa.txt: 没有那个文件或目录
1.txt
2.txt

将1.txt 2.txt与不存在的aa.txt输出追加到a.txt
[root@localhost ~]# cat a.txt
ls: 无法访问aa.txt: 没有那个文件或目录
1.txt
2.txt
[root@localhost ~]# ls [12].txt aa.txt &>> a.txt
[root@localhost ~]# !cat
cat a.txt
ls: 无法访问aa.txt: 没有那个文件或目录
1.txt
2.txt
ls: 无法访问aa.txt: 没有那个文件或目录
1.txt
2.txt

wc -l 查看文本文档行数

查看a.txt内容为8行
[root@localhost ~]# wc -l < a.txt
8

[root@localhost ~]# wc -l < a.txt 输出

[root@localhost ~]# wc -l > a.txt 输入

管道符 “|”
将前面命令的输出结果交给后面的命令

查找lrzsz包
[root@localhost ~]# rpm -qa | grep lrzsz
lrzsz-0.12.20-36.el7.x86_64

crtl+z 暂时停止进程
fg 前台继续进行上一次的命令
bg 后台继续运行命令
jobs 查看正暂时停止的所有命令
& 将命令后台运行

如编辑文件时使用ctrl+z退出编辑进行其他操作

[root@localhost ~]# vim a.txt

[1]+ 已停止 vim a.txt

[root@localhost ~]# fg 继续进行编辑a.txt
vim a.txt

[root@localhost ~]# jobs 查看正暂时停止的所有命令
[1]- 已停止 vim a.txt
[2]+ 已停止 vim 1.txt

[root@localhost ~]# fg 2 选择编号进行上次的编辑
vim 1.txt

变量

env 查看系统变量
set 查看系统所有变量

env 是一个外部命令,程序文件/bin/env,列出所有环境变量及其赋值
[root@localhost ~]# env
XDG_SESSION_ID=25
HOSTNAME=localhost.localdomain
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=5000
SSH_CLIENT=192.168.131.1 52205 22
SELINUX_USE_CURRENT_RANGE=
SSH_TTY=/dev/pts/0
USER=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:.tar=01;31:.tgz=01;31:.arc=01;31:.arj=01;31:.taz=01;31:.lha=01;31:.lz4=01;31:.lzh=01;31:.lzma=01;31:.tlz=01;31:.txz=01;31:.tzo=01;31:.t7z=01;31:.zip=01;31:.z=01;31:.Z=01;31:.dz=01;31:.gz=01;31:.lrz=01;31:.lz=01;31:.lzo=01;31:.xz=01;31:.bz2=01;31:.bz=01;31:.tbz=01;31:.tbz2=01;31:.tz=01;31:.deb=01;31:.rpm=01;31:.jar=01;31:.war=01;31:.ear=01;31:.sar=01;31:.rar=01;31:.alz=01;31:.ace=01;31:.zoo=01;31:.cpio=01;31:.7z=01;31:.rz=01;31:.cab=01;31:.jpg=01;35:.jpeg=01;35:.gif=01;35:.bmp=01;35:.pbm=01;35:.pgm=01;35:.ppm=01;35:.tga=01;35:.xbm=01;35:.xpm=01;35:.tif=01;35:.tiff=01;35:.png=01;35:.svg=01;35:.svgz=01;35:.mng=01;35:.pcx=01;35:.mov=01;35:.mpg=01;35:.mpeg=01;35:.m2v=01;35:.mkv=01;35:.webm=01;35:.ogm=01;35:.mp4=01;35:.m4v=01;35:.mp4v=01;35:.vob=01;35:.qt=01;35:.nuv=01;35:.wmv=01;35:.asf=01;35:.rm=01;35:.rmvb=01;35:.flc=01;35:.avi=01;35:.fli=01;35:.flv=01;35:.gl=01;35:.dl=01;35:.xcf=01;35:.xwd=01;35:.yuv=01;35:.cgm=01;35:.emf=01;35:.axv=01;35:.anx=01;35:.ogv=01;35:.ogx=01;35:.aac=01;36:.au=01;36:.flac=01;36:.mid=01;36:.midi=01;36:.mka=01;36:.mp3=01;36:.mpc=01;36:.ogg=01;36:.ra=01;36:.wav=01;36:.axa=01;36:.oga=01;36:.spx=01;36:*.xspf=01;36:
MAIL=/var/spool/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/root
LANG=zh_CN.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LOGNAME=root
XDG_DATA_DIRS=/root/.local/share/flatpak/exports/share/:/var/lib/flatpak/exports/share/:/usr/local/share/:/usr/share/
SSH_CONNECTION=192.168.131.1 52205 192.168.131.128 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env

自定义变量
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# a=111 自定义变量a
[root@localhost ~]# echo $a
111
[root@localhost ~]# set |grep 111
_=111
a=111

变量名规则:字母、数字下划线、首位不能为数字。
变量值有特殊符号时需要用单引号扩起来
单变量表达式比较复杂时尽量用单引号和双引号引起来

新建自定义环境变量
[root@localhost ~]# a2=2
[root@localhost ~]# echo $a2
2

[root@localhost ~]# a_2=2
[root@localhost ~]# echo $a_2
2

[root@localhost ~]# _a23=344
[root@localhost ~]# echo $_a23
344

[root@localhost ~]# aa=‘a$vf’
[root@localhost ~]# echo a a a aa a aaavf

连续查看多个变量
[root@localhost ~]# echo a a a aa a aaavf
[root@localhost ~]# echo $a
111
[root@localhost ~]# echo a a aa aaa
a$vf111

查看多个变量且引用其他变量
[root@localhost ~]# a=‘a$bd’
[root@localhost ~]# echo a a a a aabd
[root@localhost ~]# b=2
[root@localhost ~]# echo KaTeX parse error: Expected 'EOF', got '#' at position 23: …ot@localhost ~]#̲ c=a"b"c
[root@localhost ~]# echo $c
a2c

查看自己当前登录终端属于哪个TTY
[root@localhost ~]# w
22:13:23 up 7:33, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.131.1 17:59 3.00s 2.90s 0.02s w
root pts/1 192.168.131.1 22:13 7.00s 0.06s 0.06s -bash
[root@localhost ~]# echo $SSH_TTY 当前属于/dev/pts/0
/dev/pts/0

全局变量
新建全局变量linux
[root@localhost ~]# export linux=zhounan
新进bash
[root@localhost ~]# bash
列出进程树
[root@localhost ~]# pstree
├─rtkit-daemon───2*[{rtkit-daemon}]
├─smartd
├─sshd──sshd───bash───bash───pstree 找到sshd查看bash出现了几个则登录进程
├─systemd-journal
├─systemd-logind
├─systemd-udevd

当打开第二个登录链接时,则新生产一个进程sshd
├─rtkit-daemon───2*[{rtkit-daemon}]
├─smartd
├─sshd─┬─sshd───bash───bash───bash
│ └─sshd───bash───pstree
├─systemd-journal
├─systemd-logind

此时在pst1设置的全局变量对pst2无效
而在全局变量下expotr对当前登录有效。

环境变量配置文件
系统层次
/etc/profile.d/ 用户环境变量,交互,登录才执行
/etc/bashrc 用户不用登录,指定shell就生效
上述两个配置文件为系统环境变量配置文件,其添加的参数对所有额用户生效
/etc/profile.d在启动时会自动调用/etc/bashrc里面的配置。

用户层次
以下四个为所有用户家目录下均存在的环境变量配置文件。
~/.bashrc

~/.bash_history

~/.bash_logout 定义用户退出时需要做的操作(如每次退出时自动删除命令历史)

~/.bash_profile

PS1
查看PS1变量
[root@localhost network-scripts]# echo $PS1
[\u@\h \W]$
修改大写"W"为小写"w"将路径显示为绝对路径
root@localhost:/etc/sysconfig/network-scripts#

修改颜色 PS1=’[\033[01;32m]\u@\h[\033[00m]:[\033[01;36m]\w[\033[00m]$ ’

root@localhost:~# PS2="#"
root@localhost:~# echo $PS2

root@localhost:~# for i in ‘seq 1 10’

[root@localhost network-scripts]# PS1=’[\u@\h \w]$’

你可能感兴趣的:(学习笔记)