shell基础(上)

shell介绍

一、什么是shell

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

二、命令历史

  • history 命令
  • .bash_history
  • 最大1000条 可以在环境变量配置文件/et/profile中进行修改
  • 变量HISTSIZE
  • /etc/profile修改
  • HISTIMEFORMAT="%Y/%m/%d %H:%M:%S"
  • 永久保存chattr +a ~/.bash_history
  • 使用上下箭头可以调用以前的历史命令
  • !! 重复执行上一条命令
  • !n 重复执行第n条命令
  • !word 重复执行最后哦一条以该字符串开头的命令
  • history -c 清空历史命令
# 历史命令的存放位置 /root/.bash_history

将历史命令保存条数修改为10000条

[root@localhost ~]# echo $HISTSIZE
1000
[root@localhost ~]# vi /etc/profile
···
# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=10000  这边可以更改历史命令的条数
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi
···

历史命令的使用演示

[root@localhost ~]# history 
    1  vi /etc/profile
    2  history 
    3  ls
    4  cat data/
    5  history 
[root@localhost ~]# !! 执行上一条命令
history 
    1  vi /etc/profile
    2  history 
    3  ls
    4  cat data/
    5  history 
[root@localhost ~]# !5 执行第五条命令
history 
    1  vi /etc/profile
    2  history 
    3  ls
    4  cat data/
    5  history 
[root@localhost ~]# !his 执行以his字符串出现的命令,如果有相同的字符串,执行最后一次出现的命令。
history 
    1  vi /etc/profile
    2  history 
    3  ls
    4  cat data/
    5  history 

三、命令补全和别名

命令补全 tab

在输入命令或文件时,按“Tab”键就会自动进行补全 前提是必须有有这个命令和文件

[root@localhost ~]# user 输入命令的前几个字符串 然后按tab 键,按一次没有补全,连续按两次出现可以补全的命令,然后输入你需要输入的命令字符“a”就可以补全命令useradd命令了。
useradd     userdel     usermod     usernetctl  users
[root@localhost ~]# useradd 

别名

  • alias 查看系统中所有命令的别名
  • alias 别名 =‘原命令’ 设定别名
  • 别名永久生效 修改配置文件 ~/.bashrc
  • 删除别名 unalias 查看系统中的别名
[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 ls='ls --color=never' ls 不显示颜色 临时生效

[root@localhost ~]# alias ls='ls --color=never'
[root@localhost ~]# ls
1.repo	anaconda-ks.cfg  data
[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=never'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

修改配置文件 ~/.bashrc 使别名永久生效,修改完配置文件后还需要执行一条命令source .bashrc 才立即生效,如果不执行,重启系统后生效

······
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias vi='vim'
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
······
[root@localhost ~]# source .bashrc
删除别名 unalias 别名
[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 vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]# unalias vi
[root@localhost ~]# !al
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'

vi 设置的别名已经删除,这只是临时删除,如果想永久删除,需要修改配置文件 .bashrc

四、通配符

通配符 作用
匹配一个任意字符
* 匹配0个或多个任意字符,也就是可以匹配任何内容
[ ] 匹配中括号中任意一个字符。例如:[abc]代表一定匹配一个字符,或者a,或者b,或者c。
[-] 匹配中括号中任意一个字符,-代表一个范围。例如:[a-z]代表匹配a-z一个小写字母。
[^] 逻辑非,表示匹配不是中扩括号内的一个字符。例如:[^0-9]代表匹配一个不是数字的字符。
[root@localhost 123]# ls
yuanke  yuanke1  yuanke2  yuanke3  yuanke34  yuankea  yuankeaming
[root@localhost 123]# ls yuanke······ 如果后面什么都不跟只列出单独的文件
yuanke
[root@localhost 123]# ls yuanke* ·····使用*表示通配,将yuanke后面带有所有的字符的文件列出
yuanke  yuanke1  yuanke2  yuanke3  yuanke34  yuankea  yuankeaming
[root@localhost 123]# ls yuanke?······使用?表示匹配任意一个字符,yuanke后面带有一个字符的列出
yuanke1  yuanke2  yuanke3  yuankea
[root@localhost 123]# ls yuanke[0-9]······使用[]表示yuanke后面0-9匹配的任意一个数字
yuanke1  yuanke2  yuanke3
[root@localhost 123]# ls yuanke[0-9][0-9]如果想匹配两位数字就写两个范围
yuanke34
[root@localhost 123]# ls yuanke[^0-9]······便是yuanke后面不带有0-9数字的一个字符
yuankea
[root@localhost 123]# ls yuanke[^0-9]*······表示yuanke后面不带0-9数字的所有文件
yuankea  yuankeaming

五、输入输出重定向

输出重定向
类型 符号 作用
标准输出重定向 命令>文件 以覆盖的方式,把命令的正确输出输出到指定的文件或设备当中
标准输出重定向 命令>>文件 以追加的方式,把命令的正确输出输出到指定的文件或设备当中
错误输出重定向 错误命令 2>文件 以覆盖的方式,把命令的错误输出输出到文件或设备当中
标准错误输出 错误命令 2>>文件 以追加的方式,把命令的错误输出输出到指定的文件或设备当中

以覆盖的方式,把命令的正确输出输出到指定的文件或设备当中

[root@localhost ~]# ifconfig > test.log 
[root@localhost ~]# cat test.log 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.5.129  netmask 255.255.255.0  broadcast 192.168.5.255
        inet6 fe80::8335:b418:bf20:3d39  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:33:21:53  txqueuelen 1000  (Ethernet)
        RX packets 631  bytes 48243 (47.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 304  bytes 33849 (33.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 68  bytes 5912 (5.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 5912 (5.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# ls > test.log 
[root@localhost ~]# cat test.log 
123
1.repo
anaconda-ks.cfg
data
test.log

以追加的方式,把命令的正确输出输出到指定的文件或设备当中

[root@localhost ~]# ls >> test.log 
[root@localhost ~]# cat test.log 
123
1.repo
anaconda-ks.cfg
data
test.log
123
1.repo
anaconda-ks.cfg
data
test.log

正确输出和错误输出同时保存

符号 作用
命令 > 文件 2>&1 以覆盖的方式,把正确输出和错误输出都保存到同一个文件当中
命令 >> 文件 2>&1 以追加的方式,把正确输出和错误输出都保存到同一个文件当中
命令 &>文件 以覆盖的方式,把正确输出和错误输出都保存到同一个文件当中
命令 &>>文件 以追加的方式,把正确输出和错误输出都保存到同一个文件当中
命令 >>文件1 2>>文件2 把正确的输出追加到文件1中,把错误的输出追加到文件2中。
覆盖的形式

1、命令 > 文件 2>&1 以覆盖的方式,把正确输出和错误输出都保存到同一个文件当中

[root@localhost ~]# ls > test2.log 2>&1 ······将ls执行的命令结果输出到test2.log 
[root@localhost ~]# ls ······ ls命令执行的结果
123  1.repo  anaconda-ks.cfg  data  test2.log  test.log
[root@localhost ~]# cat test2.log ······查看正确输出到test2.log的结果
123
1.repo
anaconda-ks.cfg
data
test2.log
test.log
[root@localhost ~]# lsyuanke > test2.log 2>&1······执行完错误命令后并没有显示到当前页面,而是将错误的命令结果输出到test2.log
[root@localhost ~]# cat test2.log ······查看输出命令结果
-bash: lsyuanke: 未找到命令···· ······错误的命令结果

2、命令 &>文件

[root@localhost ~]# ls &>test.log 
[root@localhost ~]# cat test.log 
123
1.repo
anaconda-ks.cfg
data
test.log
[root@localhost ~]# lss &>test.log 
[root@localhost ~]# cat test.log 
-bash: lss: 未找到命令
追加的形式

1、命令 >> 文件 2>&1 | 以追加的方式,把正确输出和错误输出都保存到同一个文件当中

[root@localhost ~]# ls >> test.log 2>&1
[root@localhost ~]# cat test.log 
-bash: lss: 未找到命令
123
1.repo
anaconda-ks.cfg
data
test.log

2、命令 &>>文件

[root@localhost ~]# cattt &>>test.log 
[root@localhost ~]# cat test.log 
-bash: lss: 未找到命令
123
1.repo
anaconda-ks.cfg
data
test.log
-bash: lss: 未找到命令
-bash: cattt: 未找到命令

命令 >>文件1 2>>文件2 | 把正确的输出追加到文件1中,把错误的输出追加到文件2中。

[root@localhost ~]# ls >>right.log 2>>error.log
[root@localhost ~]# cat right.log   ······ ls是正确的命令 输出到rigth.log 文件当中
123
1.repo
anaconda-ks.cfg
data
error.log
right.log
test.log
[root@localhost ~]# cat error.log ······  错误的日志里面并没有记录
[root@localhost ~]# lsss >>right.log 2>>error.log ······执行一条错误的命令 lssss
[root@localhost ~]# cat right.log ······正确的日志里面并没有追加
123
1.repo
anaconda-ks.cfg
data
error.log
right.log
test.log
[root@localhost ~]# cat error.log ······错误的日志文件里面显示了 错误输出命令结果
-bash: lsss: 未找到命令

/dev/null 系统中预留的黑洞,没有用的文件可以放到这里面

六、管道符

[root@localhost ~]# 命令1 | 命令2

# 命令1的正确输出作为命令2的操作对象
[root@localhost ~]# ls 
123  1.repo  anaconda-ks.cfg  data  error.log  right.log  test.log
[root@localhost ~]# ls -l | grep right.log 
-rw-r--r--  1 root root   61 5月  31 22:17 right.log
[root@localhost ~]# ls -l | wc -l
8

这里需要注意的是命令2必须是能够正确执行命令1的输出结果

常用的快捷键

快捷键 作用
ctrl+c 强制终止当前命令
ctrl+l 清屏
ctrl+a 光标移动到命令行首
ctrl+e 光标移动到命令行尾
ctrl+u 从光标所在位置删除到行首
ctrl+z 把命令放入后台
ctrl+r 在历史命令中搜索

ctrl+c 强制终止当前命令

[root@localhost ~]# cat right.log ^C

ctrl+z|把命令放入后台

[root@localhost ~]# vim error.log 
-bash: lsss: 未找到命令
"error.log" 1L, 29C                               1,1          全部

[1]+  已停止               vim error.log

使用命令fg可以进入后台任务 ctrl+r|在历史命令中搜索

(reverse-i-search)`vim': vim error.log ······在历史命令中搜索出现的字符命令,只能搜索出出现在历史命令中的最后一条。
  • jobs查看后台任务
[root@localhost ~]# jobs
[1]-  已停止               vim aa.txt
[2]+  已停止               vim bb.txt
  • bg[id]把任务调到后台运行
  • fg[id]把任务调到前台运行
[root@localhost ~]# sleep 1000
^Z
[1]+  已停止               sleep 1000
[root@localhost ~]# sleep 200
^Z
[2]+  已停止               sleep 200
[root@localhost ~]# jobs
[1]-  已停止               sleep 1000
[2]+  已停止               sleep 200
[root@localhost ~]# bg 1 将任务1调到后台运行
[1]- sleep 1000 &
[root@localhost ~]# jobs
[1]-  运行中               sleep 1000 &
[2]+  已停止               sleep 200
[root@localhost ~]# fg 1 将任务1调到前台运行
sleep 1000
  • 命令后面加&直接丢到后台运行
[root@localhost ~]# sleep 1000 &
[1] 1303
[root@localhost ~]# jobs
[1]+  运行中               sleep 1000 &

猜你喜欢

转载自my.oschina.net/u/3850965/blog/1823259