栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux系统管理

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux系统管理

  • 环境
    • 虚拟机:VMware-10.0.7 build-2844087
    • Linux系统:CentOS 6.8
    • 远程工具:Xshell 6 (Build 0197)

Linux系统管理_Shell编程_03_Shell基础_Bash基本功能 一、历史命令 1.1、 历史命令的查看
  • 历史命令查看:
    • 格式:[root@localhost ~]# history [选项] [历史命令保存文件]
    • 选项:
      • -c:清空历史命令(慎用)
      • -w:把缓存中的历史命令写入历史命令保存文件,如果不手动指定历史命令保存文件,则放入默认历史命令保存文件 ~/ .bash_history
    • 用法:
      • history -w :将历史命令立刻写入 历史命令文件 ~/ .bash_history,默认命令是保存在缓存中,执行此句可以直接将历史命令写入文件
      • history -c:清空历史命令
1.2、历史命令的调用
  • 使用原先历史命令方法
    • 上下箭头调用
    • !n:重复执行第n条历史命令
    • !!:重复执行上一条历史命令
    • !字符串:重复执行最后一条以该字串开头的命令
    • !$:重复上一条命令的最后一个参数
[root@localhost ~]# !143                        <--!n 用法
cd /mnt
[root@localhost mnt]# !!                        <--!! 用法
cd /mnt
[root@localhost mnt]# !cd                       <--!字符串 用法
cd /mnt
[root@localhost mnt]# ls -l                     
总用量 8
dr-xr-xr-x  7 root root 4096 5月  23 2016 cdrom
drwxr-xr-x. 2 root root 4096 4月  23 16:18 usb
[root@localhost mnt]# ls !$                     <--!$ 用法
ls -l
总用量 8
dr-xr-xr-x  7 root root 4096 5月  23 2016 cdrom
drwxr-xr-x. 2 root root 4096 4月  23 16:18 usb
[root@localhost mnt]# 
二、命令与文件补全
  • 支持Tab键补全命令或者文件
三、命名别名 3.1、命令格式
  • 命令格式
    • 查询命令别名:[root@localhost ~]# alias
    • 设定命令别名:[root@localhost ~]# alias 别名='原命令'
[root@localhost ~]# alias                                <--查看当前系统命令别名
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l -a --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 ~]# alias egrep='grep -E --color=auto'  <--设定别名
[root@localhost ~]# alias
alias cp='cp -i'
alias egrep='grep -E --color=auto'                      <--已存在,设置成功
alias l.='ls -d .* --color=auto'
alias ll='ls -l -a --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'
3.2、命令执行顺序
  • 命令执行顺序
    • 第一顺位执行绝对路径或者相对路径的命令
    • 第二顺位执行别名
    • 第三顺位执行bash的内部命令
    • 第四顺位执行 $PATH 环境变量定义的目录查找顺序找到的第一个命令
四、Bash常用快捷键
快捷键作用

Ctrl + A

把光标移动到命令行开头

Ctrl + E

把光标移动到命令行结尾

Ctrl + C

强制中止当前命令

Ctrl + L

清屏,相当于 clear 命令

Ctrl + U

删除或者剪切光标之前的命令

Ctrl + K

删除或者剪切光标之后的命令

Ctrl + Y

粘贴 Ctrl + U 或者 Ctrl + K 剪切的内容

Ctrl + R

历史命令搜索:按下 Ctrl + R 之后,输入搜索内容,直接搜索相关命令

Ctrl + D

退出当前终端

Ctrl + Z

暂停,并放入后台(暂时搁置,后期要重点解释)

Ctrl + S

暂停屏幕输出

Ctrl + Q

恢复屏幕输出

        说明:标黑属于常用,日常尽量多使用,多记忆。

五、输入输出重定向 5.1、Bash的标准输入输出

设备设备文件名文件描述符类型
键盘

/dev/stdin

0

标准输入

显示器

/dev/stdout

1

标准输出

显示器

/dev/stderr

2

标准错误输出

5.2、输出重定向
  • 输出重定向如何理解
    • 命令执行之后,执行的结果应该输出到显示器上
    • 重定向之后,输出方向改变,不再输出到显示器上,而是输出到文件当中

重定向类型

符号作用

标准输出重定向

命令 > 文件

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

命令 >> 文件

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

标准错误输出重定向

错误命令 2> 文件

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

错误命令 2>> 文件

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

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

命令 > 文件 2>&1

以覆盖的方式,把命令的正确输出和错误输出都保存在同一个文件中

命令 >> 文件 2>&1

以追加的方式,把命令的正确输出和错误输出都保存在同一个文件中

命令 &> 文件

以覆盖的方式,把命令的正确输出和错误输出都保存在同一个文件中

命令 &>> 文件

以追加的方式,把命令的正确输出和错误输出都保存在同一个文件中

命令 >> 文件1 2>> 文件2

把正确的输出追加到文件1当中,把错误输出追加到文件2中

  • 举例 1:标准输出重定向 
 //<--标准正确输出,覆盖式 -->
[root@localhost ~]# echo -e  "e[1;31m This is a test file about right standard output redirection! e[0m" 
 This is a test file about right standard output redirection!  <--有结果正确输出                  
[root@localhost ~]# echo -e  "e[1;31m This is a test file about right standard output redirection! e[0m" > stdout    
                                                               <--覆盖式写入stdout文件                                       
[root@localhost ~]# cat stdout                                 <--查看stdout文件
 This is a test file about right standard output redirection! 
//<--标准正确输出,追加式-->
[root@localhost ~]# echo -e  "e[1;31m This is a test file about right standard output redirection! e[0m" >> stdout             
                                                               <--追加式写入stdout                     
[root@localhost ~]# cat stdout                                 <--查看stdout文件
 This is a test file about right standard output redirection! 
 This is a test file about right standard output redirection!  <--已追加
  • 举例2:标准错误输出重定向
//<--标准错误输出,覆盖式-->
[root@localhost ~]# print "e[1;31m This is a test file about right standard output redirection! e[0m" 2> stderr
[root@localhost ~]# cat stderr
-bash: print: command not found
//<--标准错误输出,追加式-->
[root@localhost ~]# print "e[1;31m This is a test file about right standard output redirection! e[0m" 2>> stderr
[root@localhost ~]# cat stderr
-bash: print: command not found
-bash: print: command not found

  • 举例3: 正确输出和错误输出同时保存
  • 举例3-1: 命令 > 文件 2>&1 ,覆盖式
[root@localhost ~]# touch std                        <--创建测试文件std
[root@localhost ~]# ls -l > std 2>&1                 <--命令正确执行,写入std
[root@localhost ~]# cat std                          <--查看std文件
总用量 60
-rw-------. 1 root root  1247 4月  23 09:21 anaconda-ks.cfg
-rw-r--r--. 1 root root 28250 4月  23 09:21 install.log
-rw-r--r--. 1 root root  7572 4月  23 09:18 install.log.syslog
drwxr-xr-x  2 root root  4096 5月  30 00:41 sh
-rw-r--r--  1 root root     0 5月  31 19:25 std
-rw-r--r--  1 root root    64 5月  31 19:07 stderr
-rw-r--r--  1 root root   148 5月  31 19:01 stdout
-rw-r--r--  1 root root    28 5月  31 06:33 wc        <--已写入
[root@localhost ~]# llll > std 2>&1                   <--错误命令执行后,也写入std 
[root@localhost ~]# cat std                           <--查看std
-bash: llll: command not found                        <--已写入std,源内容已被覆盖
  •  举例3-2: 命令 >> 文件 2>&1 , 追加式
[root@localhost ~]# cat std                                <--查看std文件原始内容
-bash: llll: command not found
[root@localhost ~]# ls -l                                  <--正确命令输出
总用量 64
-rw-------. 1 root root  1247 4月  23 09:21 anaconda-ks.cfg
-rw-r--r--. 1 root root 28250 4月  23 09:21 install.log
-rw-r--r--. 1 root root  7572 4月  23 09:18 install.log.syslog
drwxr-xr-x  2 root root  4096 5月  30 00:41 sh
-rw-r--r--  1 root root    31 5月  31 19:26 std
-rw-r--r--  1 root root    64 5月  31 19:07 stderr
-rw-r--r--  1 root root   148 5月  31 19:01 stdout
-rw-r--r--  1 root root    28 5月  31 06:33 wc
[root@localhost ~]# ls -l >> std 2>&1                     <--追加到std文件
[root@localhost ~]# cat std                               <--查看std文件
-bash: llll: command not found
总用量 64
-rw-------. 1 root root  1247 4月  23 09:21 anaconda-ks.cfg
-rw-r--r--. 1 root root 28250 4月  23 09:21 install.log
-rw-r--r--. 1 root root  7572 4月  23 09:18 install.log.syslog
drwxr-xr-x  2 root root  4096 5月  30 00:41 sh
-rw-r--r--  1 root root    31 5月  31 19:26 std
-rw-r--r--  1 root root    64 5月  31 19:07 stderr
-rw-r--r--  1 root root   148 5月  31 19:01 stdout
-rw-r--r--  1 root root    28 5月  31 06:33 wc            <--已追加
  •  举例3-3: 命令 &> 文件, 覆盖式
[root@localhost ~]# cat std                                <--查看std原始文件内容
-bash: llll: command not found
总用量 64
-rw-------. 1 root root  1247 4月  23 09:21 anaconda-ks.cfg
-rw-r--r--. 1 root root 28250 4月  23 09:21 install.log
-rw-r--r--. 1 root root  7572 4月  23 09:18 install.log.syslog
drwxr-xr-x  2 root root  4096 5月  30 00:41 sh
-rw-r--r--  1 root root    31 5月  31 19:26 std
-rw-r--r--  1 root root    64 5月  31 19:07 stderr
-rw-r--r--  1 root root   148 5月  31 19:01 stdout
-rw-r--r--  1 root root    28 5月  31 06:33 wc
[root@localhost ~]# ls -l &> std                           <--正确命令覆盖式写入std
[root@localhost ~]# cat std                                <--查看覆盖后的结果
总用量 60
-rw-------. 1 root root  1247 4月  23 09:21 anaconda-ks.cfg
-rw-r--r--. 1 root root 28250 4月  23 09:21 install.log
-rw-r--r--. 1 root root  7572 4月  23 09:18 install.log.syslog
drwxr-xr-x  2 root root  4096 5月  30 00:41 sh
-rw-r--r--  1 root root     0 5月  31 21:11 std
-rw-r--r--  1 root root    64 5月  31 19:07 stderr
-rw-r--r--  1 root root   148 5月  31 19:01 stdout
-rw-r--r--  1 root root    28 5月  31 06:33 wc
[root@localhost ~]# lllll &> std                           <--错误命令覆盖式写入std
[root@localhost ~]# cat std                                <--再次查看std文件内容
-bash: lllll: command not found                            <--std内容被覆盖
  • 举例3-4: 命令 &> > 文件, 追加式
[root@localhost ~]# cat std                                <--查看当前std内容
-bash: lllll: command not found
[root@localhost ~]# ls -l &>> std                          <--追加正确输出到std
[root@localhost ~]# cat std                                <--再次查看std内容
-bash: lllll: command not found                            <--原错误输出存在
总用量 64
-rw-------. 1 root root  1247 4月  23 09:21 anaconda-ks.cfg
-rw-r--r--. 1 root root 28250 4月  23 09:21 install.log
-rw-r--r--. 1 root root  7572 4月  23 09:18 install.log.syslog
drwxr-xr-x  2 root root  4096 5月  30 00:41 sh
-rw-r--r--  1 root root    32 5月  31 21:12 std
-rw-r--r--  1 root root    64 5月  31 19:07 stderr
-rw-r--r--  1 root root   148 5月  31 19:01 stdout
-rw-r--r--  1 root root    28 5月  31 06:33 wc             <--新内容也存在
  • 举例3-5: 命令 >> 文件1 2>> 文件2, 追加式
[root@localhost ~]# touch err ok                     <--创建2个文件
[root@localhost ~]# ls
anaconda-ks.cfg  err  install.log  install.log.syslog  ok  sh  wc
[root@localhost ~]# ls -l >> ok 2>> err              <--命令正确执行结果输出到ok
[root@localhost ~]# cat ok                           <--查看ok文件,已有记录
总用量 52
-rw-------. 1 root root  1247 4月  23 09:21 anaconda-ks.cfg
-rw-r--r--  1 root root     0 6月   1 06:55 err
-rw-r--r--. 1 root root 28250 4月  23 09:21 install.log
-rw-r--r--. 1 root root  7572 4月  23 09:18 install.log.syslog
-rw-r--r--  1 root root     0 6月   1 06:55 ok
drwxr-xr-x  2 root root  4096 5月  30 00:41 sh
-rw-r--r--  1 root root    28 5月  31 06:33 wc
[root@localhost ~]# lssssss -l >> ok 2>> err         <--命令错误执行结果输出到err
[root@localhost ~]# cat err
-bash: lssssss: command not found
5.3、输入重定向(了解,很少用)
  • 输入重定向
    • 格式:[root@localhost ~]# wc [选项] [文件名]
    • 选项:
      • -c:统计字节数
      • -w:统计单词数
      • -l:统计行数
    • 应用场景
      • 使用 cpio命令 提取httpd包时使用
      • 表示方式:
        • <:文件反向写入
        • <<:通过界定符反向录入数据
六、多命令顺序执行
多命令数序执行符

格式

作用

;(分号)

命令1 ; 命令2

多个命令顺序执行,没有逻辑关系

&&

命令1 && 命令2

当命令1正确执行($?=0),命令2才会执行; 

当命令1执行不正确($?≠0),命令2不会执行。

||

命令1 || 命令2

当命令1执行不正确($?≠0),命令2才会执行;

当命令1执行正确($?=0),命令2不会执行。

  • 举例 1:;(分号) 
[root@localhost ~]# ls -l ; cat err            <--没有关联关系,命令都正确执行
总用量 60
-rw-------. 1 root root  1247 4月  23 09:21 anaconda-ks.cfg
-rw-r--r--  1 root root    34 6月   1 06:57 err
-rw-r--r--. 1 root root 28250 4月  23 09:21 install.log
-rw-r--r--. 1 root root  7572 4月  23 09:18 install.log.syslog
-rw-r--r--  1 root root   388 6月   1 06:56 ok
drwxr-xr-x  2 root root  4096 5月  30 00:41 sh
-rw-r--r--  1 root root    28 5月  31 06:33 wc
-bash: lssssss: command not found
[root@localhost ~]# lssss -l ; cat err        <--没有关联关系,前面命令错误不会影响后面命令输出
-bash: lssss: command not found
-bash: lssssss: command not found
  • 举例 2:&& (短路原理)
[root@localhost ~]# ls -l && echo "This is a rigth stanard output! "  <--前一个命令正确,&& 后面的命令才会执行
总用量 60                           
-rw-------. 1 root root  1247 4月  23 09:21 anaconda-ks.cfg
-rw-r--r--  1 root root    34 6月   1 06:57 err
-rw-r--r--. 1 root root 28250 4月  23 09:21 install.log
-rw-r--r--. 1 root root  7572 4月  23 09:18 install.log.syslog
-rw-r--r--  1 root root   388 6月   1 06:56 ok
drwxr-xr-x  2 root root  4096 5月  30 00:41 sh
-rw-r--r--  1 root root    28 5月  31 06:33 wc
This is a rigth stanard output!
[root@localhost ~]# lsssss -l && echo "This is a wrong stanard output! "  <--前一个命令执行出错,&& 后面的命令不会执行
-bash: lsssss: command not found
  • 举例 3:|| (断路原理)
[root@localhost ~]# ls -l || echo yes   <--||前面命令正确执行,||后面的命令不会执行
总用量 60
-rw-------. 1 root root  1247 4月  23 09:21 anaconda-ks.cfg
-rw-r--r--  1 root root    34 6月   1 06:57 err
-rw-r--r--. 1 root root 28250 4月  23 09:21 install.log
-rw-r--r--. 1 root root  7572 4月  23 09:18 install.log.syslog
-rw-r--r--  1 root root   388 6月   1 06:56 ok
drwxr-xr-x  2 root root  4096 5月  30 00:41 sh
-rw-r--r--  1 root root    28 5月  31 06:33 wc
[root@localhost ~]# lssss -l || echo no <--||前面命令未正确执行,||后面的命令会执行
-bash: lssss: command not found
no
七、小结
  • 知识点总结
    • history 命令
    • Tab键补全命令或者文件名
    • 命令别名及命令执行顺序
    • Bash快捷键:Ctrl+A,Ctrl+E
    • 输入输出重定向
    • 多命令执行顺序:;,&&,||

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/964252.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号