8.1-8.9 笔记

知识点 shell基础知识

history,tab补全(bash-completion),alias,通配符,管道符,变量,输入输出重定向


history

历史命令,可以记录用户敲过的命令。

[root@localhost ~]# history 
    1  ls
    2  cat /etc/passwd
    3  useradd -u www
    4  cat /etc/passwd
    5  man useradd
    6  LANG=en
    7  man ls
    .....
[root@localhost ~]# history -c

默认最多可以存放1000条,使用history -c可以删除历史!但是只是删除内存中的history,只有你正常退出终端才会写入.bash_history中!

[root@localhost ~]# echo $HISTSIZE
1000

通过修改配置文档 /etc/profile里修改默认的存放条数!修改完之后还不生效,可以使用 source /etc/profile 或者. /etc/profile 重启服务使之生效!

[root@localhost ~]# vi /etc/profile
[root@localhost ~]# echo $HISTSIZE
1000
[root@localhost ~]# . /etc/profile
[root@localhost ~]# echo $HISTSIZE
5000

当存满的时候会从最早的那条开始删。在这里可以用到以前学过的chattr命令,使用chattr -a ~/.bahs_history 命令可以长时间保存!就算只保存1000条,但是还是会写入,不删除

几个小命令

  • !! 最后一条命令
  • !n n代表第n行
  • !+命令 比如!mkdir代表从尾开始找的第一条以mkdir开始的命令

bash-completion

使用tab补命令,能提示命令、提示命令参数等!安装完之后需要重启。

[root@localhost ~]# yum install -y bash-completion

alias

重命名命令,用户可利用alias,自定指令的别名。若仅输入alias,则可列出目前所有的别名设置。alias的效力仅及于该次登入的操作。若要每次登入是即自动设好别名,可在./bashrc或/etc/profile.d中设定指令的别名

[root@localhost ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]# alias xxoo='touch aaa.txt'  //设置重命名
[root@localhost ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
alias xxoo='touch aaa.txt'
[root@localhost ~]# xxoo
[root@localhost ~]# ls
aaa.txt  anaconda-ks.cfg
[root@localhost ~]# unalias xxoo  // 取消重命名

也可以写入配置文件中,同样需要. .bashrc或者source .bashrc重启服务

通配符

  • * 代表所有字符
  • ? 代表单个字符
  • [] 代表范围里的一个字符比如 [123]选1或者2或者3
  • {} 跟[]差不多,不过需要用,隔开
  • \ 脱衣字符,有些字符是具有特殊意义的,使用\字符可以归元该字符
  • | 管道符

输入输出重定向

  • > 重定向
  • >> 追加重定向
  • 2> 错误重定向
  • 2>> 错误追加重定向
  • &> 正确错误一起重定向
  • < 输入重定向
[root@localhost test]# cat /etc/passwd >passwd.txt
[root@localhost test]# cat passwd.txt 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
...
[root@localhost test]# cat hello.txt >> passwd.txt 
[root@localhost test]# cat hello 2> middle.txt
[root@localhost test]# cat middle.txt 
cat: hello: 没有那个文件或目录
[root@localhost test]# cat hello 2>> middle.txt
[root@localhost test]# cat middle.txt 
cat: hello: 没有那个文件或目录
cat: hello: 没有那个文件或目录
[root@localhost test]# ls hello.txt middle.txt 123.txt &> wwww.txt
[root@localhost test]# cat wwww.txt 
ls: 无法访问123.txt: 没有那个文件或目录
hello.txt
middle.txt
[root@localhost test]# wc -l < passwd.txt 
93

管道符和作业控制

‘|’管道符它可以轻易地连接两个毫不相关的程序,把一个程序的结果交给另一个来处理,甚至,不停地交接处理。

[root@localhost test]# rpm -qa |grep dhcp
dhcp-libs-4.2.5-58.el7.centos.x86_64
dhcp-common-4.2.5-58.el7.centos.x86_64
[root@localhost test]# cat /etc/profile |grep HISTSIZE
HISTSIZE=5000
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
[root@localhost test]# cat /etc/profile |grep HISTSIZE |wc -l
2

jobs命令可以查看当前挂起的应用,比如在使用vim的时候突然想cat个文件,可以不用退出,使用快捷键ctrl+z暂时停止。cat完之后可以fg回来!

[root@localhost test]# vi passwd.txt 

[1]+  已停止               vi passwd.txt
[root@localhost test]# jobs
[1]+  已停止               vi passwd.txt
[root@localhost test]# sleep 1000&  //加了&就直接丢到后台执行了!
[2] 3205
[root@localhost test]# jobs
[1]+  已停止               vi passwd.txt
[2]-  运行中               sleep 1000 &
[root@localhost test]# fg 1
vi passwd.txt

也可以使用bg将一个应用放到后台执行,jobs显示出来的标志不是已停止而是运行中。

变量

系统有很多自己的变量,使用env和set可以查看变量,env可以查看系统的变量,set在env的基础上还可以查看自己设定的变量!

[root@localhost test]# env
XDG_SESSION_ID=1
HOSTNAME=localhost.localdomain
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=5000     //这个是刚才修改的history  max存储条数
SSH_CLIENT=192.168.139.1 49221 22  //ssh显示为远端192.168.139.1 的随机高端口49221 来访问本机的22端口
SELINUX_USE_CURRENT_RANGE=
SSH_TTY=/dev/pts/0  //192.168.139.1 登录的pts号
USER=root
[root@localhost test]# set

除了系统定义的变量,自己也可以定义变量,但是定义变量也有规则。

  • 首位不能为数字
  • 变量值如果只表示一个整体字符需要用单引号' '(像脱衣字符\一样),该字符里有变量需要调用的话用双引号""
  • 最好不要使用特殊文字true,false,while,if这些
[root@localhost test]# xxoo=Huawei12#$
[root@localhost test]# echo $xxoo
Huawei12#$
[root@localhost test]# set |grep xxoo
xxoo='Huawei12#$'
[root@localhost test]# ooxx='demon hunter'
[root@localhost test]# echo $ooxx
demon hunter
[root@localhost test]# set |grep ooxx
ooxx='demon hunter'
[root@localhost test]# unset xxoo   //取消变量赋值
[root@localhost test]# unset ooxx
[root@localhost test]# set |grep xxoo 
[root@localhost test]# set |grep ooxx
[root@localhost test]# a=11
[root@localhost test]# b=12
[root@localhost test]# c=$a$b
[root@localhost test]# echo $c
1112
[root@localhost test]# d="$a"'$b'
[root@localhost test]# echo $d
11$b

定义的变量作用范围是有区别的,普通定义的变量只在本地生效,对子bash或者父bash都无效,而全局定义的变量可以对本地和子bash有效。

[root@localhost ~]# yum install -y psmisc  //安装pstree
[root@localhost ~]# bash
[root@localhost ~]# a=999
[root@localhost ~]# b=888
[root@localhost ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─agetty
        ├─auditd───{auditd}
        ├─chronyd
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd───sshd───bash───bash───pstree
        [root@localhost ~]# export c=777
[root@localhost ~]# bash
[root@localhost ~]# echo $a

[root@localhost ~]# echo $b

[root@localhost ~]# echo $c
777
[root@localhost ~]# exit
exit
[root@localhost ~]# exit
exit
[root@localhost ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─agetty
        ├─auditd───{auditd}
        ├─chronyd
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd───sshd───bash───pstree
[root@localhost ~]# echo $a

[root@localhost ~]# echo $b

[root@localhost ~]# echo $c
[root@localhost ~]# unset c  //取消变量赋值

环境变量配置文件

系统层面

  • /etc/profile 用户环境变量,交互,登录才执行
  • /etc/bashrc 用户不用登录,执行shell就生效

用户层面

  • /.bashrc 存放alias
  • /.bash_profile
  • /.bash_hostory 存放hostory
  • /.bash_logout 存放用户退出是的操作,比如退出是删除hostory

/etc/profile : 设置系统全局环境变量和启动程序,当用户登录Linux时,该文件被执行,并从/etc/profile.d目录的配置文件中搜集shell的设置。预设的系统全局变量包括PATH、USER、LOGNAME、MAIL、INPUTRC、HOSTNAME、HISTSIZE、umask等。

/etc/bashrc : 为每一个运行bash shell的用户执行此文件。当bash shell被打开时,该文件被读取。默认情况下,这个文件主要预设umask以及PS1等。

.bash_profile : 定义用户专有的环境变量和启动程序。当用户登录时,该文件仅仅执行一次。 默认情况下,它设置了一些环境变量,执行用户的.bashrc文件。

.bashrc :该文件包含专用于用户的shell的bash信息,当用户登录时以及每次打开新的shell时,该该文件被读取。可以将用户自定义的alias和变量写到这个文件中。

PS1

hostname 和sysname一样,想怎么改怎么改。不过有些字符是具有特殊意义的!

[root@localhost ~]# PS1='[\u@\h \W]\$'
[root@localhost ~]#PS1='[\jugg@\h \W]\$'
[0ugg@localhost ~]#PS1='[\huawei@\h \W]\$'
[localhostuawei@localhost ~]#PS1='[\99999999999@\h \W]\$'
[\99999999999@localhost ~]#

转载于:https://my.oschina.net/u/3997678/blog/2874626

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值