Linux Bash的常用功能

来自CloudWiki
跳转至: 导航搜索

什么是Bash

Linux Shell应用里,我们介绍了Bash

Linux 的 Shell 种类众多,常见的有:

   Bourne Shell(/usr/bin/sh或/bin/sh)
   Bourne Again Shell(/bin/bash)
   C Shell(/usr/bin/csh)
   K Shell(/usr/bin/ksh)
   Shell for Root(/sbin/sh)
   …… 

本教程关注的是 Bash,也就是 Bourne Again Shell,由于易用和免费,Bash 在日常工作中被广泛使用。同时,Bash 也是大多数Linux 系统默认的 Shell。

Bash的常用功能

Bash的命令历史

  • 命令历史
  • 保存用户曾经执行过的命令操作
  • 存放位置:~/.bash_history 文件

查看历史命令

[root@localhost root]# history
……
556  useradd  jerry
557  passwd  jerry
558  crontab  -e  -u  jerry
559  crontab  -l  -u  jerry


  • 调用历史命令
    • !n:执行历史记录中的第n条命令
    • !str:执行历史记录中以“str”开头的命令
  • 设置记录历史命令的条数
    • 修改 HISTSIZE 参数(默认为1000条)
[root@localhost root]# !562
crontab -l -u jerry
no crontab for jerry
[root@localhost ~]# vi /etc/profile
HISTSIZE=200

保存退出,输入命令 source /etc/profile 使其生效。

Bash的命令别名

命令别名: 为使用频率较高的复杂命令行设置简短的调用名称

存放位置:~/.bashrc

查看命令别名

格式:alias [别名]

设置命令别名

执行:alias 别名='实际执行的命令'

取消已设置的命令别名

格式:unalias 别名

  unalias  -a
[root@localhost ~]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias mv='mv -i'
alias rm='rm -i'

查找字符串

创建grep 命令

首先谈一下grep命令的常用格式为:grep [选项] ”模式“ [文件]

grep家族总共有三个:grep,egrep,fgrep。

常用选项:

  -E :开启扩展(Extend)的正则表达式。

  -i :忽略大小写(ignore case)。

  -v :反过来(invert),只打印没有匹配的,而匹配的反而不打印。

  -n :显示行号

使用grep命令查找 /etc/passwd 文件中的字符串.

使用grep命令查找带root的行

[root@iZwz9a4d6crd1w205b54pmZ test]# grep  root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

将/etc/passwd,将没有出现 root 的行取出来:

[root@iZwz9a4d6crd1w205b54pmZ test]# grep -v root /etc/passwd
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
...

将/etc/passwd,显示 出现root 的次数

[root@iZwz9a4d6crd1w205b54pmZ test]# grep -c root /etc/passwd
2

将/etc/passwd,有出现 root 的行取出来,同时显示这些行在/etc/passwd的行号

[root@iZwz9a4d6crd1w205b54pmZ test]# grep -n root /etc/passwd
2

什么是正则表达式

正则表达式(regular expression)就是用一个“字符串”来描述一个特征,然后去验证另一个“字符串”是否符合这个特征。比如 表达式“ab” 描述的特征是“一个 'a' 和 任意多个 'b' ,那么 'ab', 'abb', 'abbbbbbbbbb' 都符合这个特征。

表达式可以用来:

  1. 验证字符串是否符合指定特征,比如验证是否是合法的邮件地址。
  2. 用来查找字符串,从一个长的文本中查找符合指定特征的字符串,比查找固定字符串更加灵活方便。
  3. 用来替换,比普通的替换更强大。��

基本的正则表达式

Linux2-2.png

创建正则表达式


正则表达式在grep的应用

要查找的字符串以双引号括起来



创建文件 a.txt 内容如下:

i can't finish the test
Oh! the soup taste good.
the symbol '*' is represented as start.
Football game is not use feet only.
google is the best tools for search keyword.
However, this dress is about $ 3183 dollars.
You are the best is mean you are the no. 1
"Open Source" is a good mechanism to develop programs.
Oh! The soup taste good.


“^……”表示以……开头,“……$”表示以……结尾

[root@iZwz9a4d6crd1w205b54pmZ test]# grep -n '^the' a.txt
3:the symbol '*' is represented as start.
[root@iZwz9a4d6crd1w205b54pmZ test]# grep -n '\.$' a.txt
2:Oh! the soup taste good.
3:the symbol '*' is represented as start.
4:Football game is not use feet only.
5:google is the best tools for search keyword.
6:However, this dress is about $ 3183 dollars.
8:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.

例1:

grep -v '^#'


“^$”表示空行

例2:

grep -v '^$' /etc/rsyslog.conf


. :表示且只能代表任意一个字符

如果想要搜索到有 oo 的行:

[root@iZwz9a4d6crd1w205b54pmZ test]# grep -n '.oo' a.txt
2:Oh! the soup taste good.
4:Football game is not use feet only.
5:google is the best tools for search keyword.
8:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.

假设我需要找出 g??d 的字串,亦即共有四个字节, 起头是 g 而结束是 d ,我可以这样做

[root@iZwz9a4d6crd1w205b54pmZ test]# grep -n 'g..d' a.txt
2:Oh! the soup taste good.
8:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.

\ :转义字符,让有着特殊身份的字符,变回原来的字符。

[root@iZwz9a4d6crd1w205b54pmZ test]# grep -n '\.$' a.txt 2:Oh! the soup taste good. 3:the symbol '*' is represented as start. 4:Football game is not use feet only. 5:google is the best tools for search keyword. 6:However, this dress is about $ 3183 dollars. 8:"Open Source" is a good mechanism to develop programs. 9:Oh! The soup taste good.</nowiki>

* :重复0个或多个前面的一个字符,不代表所有

find / -name "*.txt"

.* :匹配所有的字符。^.* 任意多个字符开头。


[^]:字符类的反向选择:

字符类的反向选择 [^] :如果想要搜索到有 oo 的行,但不想要 oo 前面有 g,如下

[root@iZwz9a4d6crd1w205b54pmZ test]# grep -n '[^g]oo' a.txt
3:apple is my favorite food.
4:Football game is not use feet only.
5:google is the best tools for search keyword.

[abc] :匹配字符集合内任意一个字符[a-z]

如果我想要搜寻 test 或 taste 这两个单字时,可以发现

[root@iZwz9a4d6crd1w205b54pmZ test]# grep -n 't[ae]st' a.txt
1:i can't finish the test
2:Oh! the soup taste good.

当我们在一组集合字节中,如果该字节组是连续的,例如大写英文/小写英文/数字等等, 就可以使用[a-z],[A-Z],[0-9]等方式来书写

[root@iZwz9a4d6crd1w205b54pmZ test]# grep -n '[0-9]' a.txt
6:However, this dress is about $ 3183 dollars.
7:You are the best is mean you are the no. 1.

[^abc] :^在中括号表示非,表示不包含a或者b或者c

参考文档:

[1] https://baijiahao.baidu.com/s?id=1606776177677909517&wfr=spider&for=pc

管道和重定向

重定向允许将标准输出或错误消息从程序重定向到文件,以进行保存或稍后分析,或禁止其在终端显示。还可以通过文件而非键盘将输入读取至命令行程序

管道允许叫标准输出信息从程序连接至另一个程序的输入,每个程序作用于前一个程序的输出。

Bash的标准输入输出

标准输入:从该设备接收用户输入的数据

标准输出:通过该设备向用户输出数据

标准错误:通过该设备报告执行出错信息

Linux2-3.png

Bash的重定向操作

改变标准输入、标准输出、标准错误的方向

类型	操作符	用途
重定向标准输入	<	将命令中接收输入的途径由默认的键盘更改为指定的文件 
重定向标准输出	>	将命令的执行结果输出到指定的文件中,而不是直接显示在屏幕上 
                >>	将命令执行的结果追加输出到指定文件 
重定向标准错误	2>	清空指定文件的内容,并将标准错误信息保存到该文件中
                2>>	将标准错误信息追加输出到指定的文件中
重定向标准输出和
标准错误 	&>	将标准输出、标准错误的内容全部保存到指定的文件中,而不是直接显示在屏幕上

输入重定向实例

[root@iZwz9a4d6crd1w205b54pmZ test]# cat a.txt
i can't finish the test
Oh! the soup taste good.
the symbol '*' is represented as start.
Football game is not use feet only.
google is the best tools for search keyword.
However, this dress is about $ 3183 dollars.
You are the best is mean you are the no. 1
"Open Source" is a good mechanism to develop programs.
Oh! The soup taste good.
[root@iZwz9a4d6crd1w205b54pmZ test]# wc <a.txt
  9  65 338

输出重定向实例

将标准输出重定向到文件

[root@iZwz9a4d6crd1w205b54pmZ test]# ls -R > mydir.txt
 [root@iZwz9a4d6crd1w205b54pmZ test]# cat mydir.txt

将标准输出重定向追加到文件

[root@iZwz9a4d6crd1w205b54pmZ test]# ls / >>mydir.txt
[root@iZwz9a4d6crd1w205b54pmZ test]# cat mydir.txt

合并两个文件内容到新文件中

 cat a.txt b.txt >rec.txt
cat rec.txt

将错误输出重定向到文件

[root@iZwz9a4d6crd1w205b54pmZ test]# ls test2 2>>err.txt
[root@iZwz9a4d6crd1w205b54pmZ test]# cat err.txt
ls: cannot access test2: No such file or directory


将标准输出和错误输出重定向到文件

$ ls &> errfile
$ ls afile bfile &> errfile

Bash的管道操作

管道操作符号“|”

连接左右两个命令,将左侧的命令输出的结果,作为右侧命令的输入(处理对象)

格式:cmd1 | cmd2 [... | cmdn]

[root@localhost ~]# free -m | head -2 
total       used       free     shared    buffers     cached
Mem:           503        339        163          0         87        199
[root@controller ~]# free -m | grep "Mem"
Mem:           3936         799        2027           8        1109        2877

[root@localhost ~]# free -m | grep "Mem" | awk '{print $2,$4}'
503  163

[root@localhost ~]# find /mnt/downloads  -name "*.rpm" | grep gcc
/media/RHEL_6.0 x86_64 Disc 1/Packages/gcc-2.8.6-27.el6.x86_64.rpm

awk,以空格或制表位为分隔,输出指定第n列数据

作为linux中最为常用的三大文本(awk,sed,grep)处理工具之一,掌握好其用法是很有必要的。