Linux Bash的常用功能
目录
什么是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'
正则表达式
什么是正则表达式
正则表达式(regular expression)就是用一个“字符串”来描述一个特征,然后去验证另一个“字符串”是否符合这个特征。比如 表达式“ab” 描述的特征是“一个 'a' 和 任意多个 'b' ,那么 'ab', 'abb', 'abbbbbbbbbb' 都符合这个特征。
表达式可以用来:
- 验证字符串是否符合指定特征,比如验证是否是合法的邮件地址。
- 用来查找字符串,从一个长的文本中查找符合指定特征的字符串,比查找固定字符串更加灵活方便。
- 用来替换,比普通的替换更强大。��
基本的正则表达式
创建正则表达式
创建grep 命令
使用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 的行取出来,同时只显示这些行在/etc/passwd的行号
[root@iZwz9a4d6crd1w205b54pmZ test]# grep -c root /etc/passwd 2
将/etc/passwd,有出现 root 的行取出来,同时显示这些行在/etc/passwd的行号
[root@iZwz9a4d6crd1w205b54pmZ test]# grep -c root /etc/passwd 2
正则表达式在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.
如果我想要搜寻 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.
假设我需要找出 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.
如果想要搜索到有 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.
字符类的反向选择 [^] :如果想要搜索到有 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.
行首和行尾
[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.
管道和重定向
重定向允许将标准输出或错误消息从程序重定向到文件,以进行保存或稍后分析,或禁止其在终端显示。还可以通过文件而非键盘将输入读取至命令行程序
管道允许叫标准输出信息从程序连接至另一个程序的输入,每个程序作用于前一个程序的输出。
Bash的标准输入输出
标准输入:从该设备接收用户输入的数据
标准输出:通过该设备向用户输出数据
标准错误:通过该设备报告执行出错信息
Bash的重定向操作
改变标准输入、标准输出、标准错误的方向
类型 操作符 用途 重定向标准输入 < 将命令中接收输入的途径由默认的键盘更改为指定的文件 重定向标准输出 > 将命令的执行结果输出到指定的文件中,而不是直接显示在屏幕上 >> 将命令执行的结果追加输出到指定文件 重定向标准错误 2> 清空指定文件的内容,并将标准错误信息保存到该文件中 2>> 将标准错误信息追加输出到指定的文件中 重定向标准输出和 标准错误 &> 将标准输出、标准错误的内容全部保存到指定的文件中,而不是直接显示在屏幕上
输出重定向实例
将标准输出重定向到文件
$ ls /etc/ > etcdir
将标准输出重定向追加到文件
$ ls /etc/sysconfig/ >> etcdir
将错误输出重定向到文件
$ nocmd 2> 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@localhost ~]# free -m | grep "Mem" | awk '{print $2,$4}' 503 163
awk,以空格或制表位为分隔,输出指定第n列数据
返回 Linux基础