博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
五周第三次课(4月20日)8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.5 输入输出重定向...
阅读量:6952 次
发布时间:2019-06-27

本文共 6603 字,大约阅读时间需要 22 分钟。

hot3.png

8.1 shell介绍

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

8.2 命令历史

  • history命令
  • bash_history
  • 最大1000条
  • 变量HISTSIZE
  • /etc/profile中修改
  • HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
  • 永久保存 chattr +a ~/.bash_history
  • !!
  • !n
  • !word

1 .history 命令

显示历史命令

[root@linux-151 ~]# history    1  mkdir /tmp/222    2  rmdir /tmp/111/ /tmp/222/	.	.	.   995  echo $?   996  ls /usr/local/apache2/   997  init 0   998  ls ./.bash_history   999  vim ./.bash_history   1000  history

3. .bash_history 文件

历史命令保存在用户家目录的.bash_history文件中

4. history –c

清空内存中的历史记录;不会清除.bash_history文件中的记录。

[root@linux-151 ~]# history -c[root@linux-151 ~]# history    2  history[root@linux-151 ~]# head -10 .bash_historymkdir /tmp/222rmdir /tmp/111/ /tmp/222/lslsls tmpls /tmpmkdir /tmp/111mkdir /tmp/222lsls /tmprmdir /tmp/111/ /tmp/222/
  • 只有当用户正常退出当前shell时,在当前shell中运行的命令才会保存至.bash_history文件中。

5. 定义HISTSIZE值,在配置文件/etc/profile中修改

[root@linux-151 ~]# vim /etc/profile搜索HISTSIZE 默认值是1000,可以修改成5000条,然后按esc,:wq 保存退出。然后运行命令:source /etc/profile[root@linux-151 ~]# source /etc/profile[root@linux-151 ~]# echo $HISTSIZE5000

6. 定义格式:HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

在配置文件/etc/profile中修改,在HISTSIZE=5000下面,添加一行HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

输入图片说明

[root@linux-151 ~]# vim /etc/profile[root@linux-151 ~]# source /etc/profile[root@linux-151 ~]# history    2  2018/04/20 22:49:44 history    3  2018/04/20 22:50:20 head -10 .bash_history    4  2018/04/20 22:55:05 vim /etc/profile    5  2018/04/20 22:57:52 source /etc/prfile    6  2018/04/20 22:58:04 source /etc/profile    7* 2018/04/20 22:58:16 echo $HIST    8  2018/04/20 23:01:02 vim /etc/profile    9  2018/04/20 23:03:49 history   10  2018/04/20 23:03:57 source /etc/profile   11  2018/04/20 23:03:59 history   12  2018/04/20 23:04:20 vim /etc/profile   13  2018/04/20 23:05:14 source /etc/profile   14  2018/04/20 23:05:17 history

6. 永久保存 chattr +a ~/.bash_history

  • 给.bash_history加一个a权限,不能删除,可以追加
[root@linux-151 ~]# chattr +a .bash_history

7. !!:连续两个!表示执行上一条命令。

[root@linux-151 ~]# pwd/root[root@linux-151 ~]# !!pwd/root

8. !n:这里的n是数字,表示执行命令历史中的第n条命令。

[root@linux-128 ~]# history    2  2018/04/20 22:49:44 history    3  2018/04/20 22:50:20 head -10 .bash_history    4  2018/04/20 22:55:05 vim /etc/profile    5  2018/04/20 22:57:52 source /etc/prfile    6  2018/04/20 22:58:04 source /etc/profile    7* 2018/04/20 22:58:16 echo $HIST    8  2018/04/20 23:01:02 vim /etc/profile    9  2018/04/20 23:03:49 history   10  2018/04/20 23:03:57 source /etc/profile   11  2018/04/20 23:03:59 history   12  2018/04/20 23:04:20 vim /etc/profile   13  2018/04/20 23:05:14 source /etc/profile   14  2018/04/20 23:05:17 history   15  2018/04/20 23:07:56 chattr +a .bash_history   16  2018/04/20 23:08:10 vim .bash_history   17  2018/04/20 23:09:25 pwd   18  2018/04/20 23:10:31 history[root@linux-128 ~]# !17pwd/root

9. !字符串: !pw表示执行命令历史中最近一次以pw开头的命令。

[root@linux-151 ~]# !pwpwd/root

8.3 命令补全和别名

补全

  • tab键,敲一下,敲两下
  • 参数补全,安装bash-completion
  • alias别名给命令重新起个名字
  • 各用户都有自己配置别名的文件 ~/.bashrc
  • ls /etc/profile.d/
  • 自定义的alias放到~/.bashrc
  1. 按tab键可以帮我们补全一个指令,一个路径或者一个文件名
  2. Centos6中tab键只能补全本身;Centos7中tab键支持命令参数补全,需要安装一个包bash-completion
[root@linux-151 ~]# yum install -y bash-completion重启服务器 reboot

别名

  1. Alias也是bash所特有的功能之一;直接执行alias命令会看到目前系统预设的所有别名。
[root@linux-151 ~]# aliasalias 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'
  1. 用户家目录下.bashrc文件中只配置了几个alias
# .bashrc# User specific aliases and functionsalias rm='rm -i'alias cp='cp -i'alias mv='mv -i'# Source global definitionsif [ -f /etc/bashrc ]; then        . /etc/bashrcfi
  1. 其他的很多别名在/etc/profile.d/目录下,有很多.sh文件中定义;
[root@linux-151 ~]# ls /etc/profile.d/256term.csh         colorgrep.csh  colorls.sh  less.csh  vim.sh256term.sh          colorgrep.sh   lang.csh    less.sh   which2.cshbash_completion.sh  colorls.csh    lang.sh     vim.csh   which2.shroot@linux-151 ~]# vim /etc/profile.d/colorls.sh

输入图片说明

8.4 通配符

  • ls *.txt
  • ls ?.txt
  • ls [0-9].txt
  • ls {1,2}.txt

*用来匹配零个或多个任意字符

[root@linux-151 ~]# ls *txt11.txt  1a.txt  23.txt  321.txt  ab.txt  a.txt  b.txt  工号.txt[root@linux-151 ~]# ls 1*11.txt  11.txt.bak  12  1a.txt  1.log123:[root@linux-151 ~]# ls *1*11.txt  11.txt.bak  12  1a.txt  1.log  321.txt  321.txt.bak123:[root@linux-151 ~]# ls *a*11.txt.bak  1a.txt  321.txt.bak  ab.txt  anaconda-ks.cfg  a.txt

?用来匹配一个字符

[root@linux-151 ~]# ls ?.txta.txt  b.txt

ls [0-3].txt [ ]里面表示范围,只要是在这个范围内都列出来

[root@linux-151 ~]# ls [0-3].txt1.txt  2.txt[root@linux-151 ~]# ls [a-z].txta.txt  b.txt[root@linux-151 ~]# ls [0-9a-zA-Z].txt1.txt  2.txt  a.txt  b.txt

ls {1,2}.txt { }花括号里面要用“,”隔开,1.txt或者2.txt;

[root@linux-151 ~]# ls {1,2}.txt1.txt  2.txt[root@linux-151 ~]# ls {1,2,3}.txtls: 无法访问3.txt: 没有那个文件或目录1.txt  2.txt

8.5 输入输出重定向

  • cat 1.txt >2.txt
  • cat 1.txt >> 2.txt
  • ls aaa.txt 2>err
  • ls aaa.txt 2>>err
  • wc -l < 1.txt
  • command >1.txt 2>&1

举个例子:

ls test.sh test1.sh >success.txt 2>&1

将前面执行结果的标准输出流写入success.txt文件,省略了1,全句为:

ls test.sh test1.sh 1>success.txt 2>&1

错误输出用2,如2>error.txt。用&1表示类似1,和1绑定到一起,输出到一个文件,用&表示绑定在一起。而“&-”表示关闭。

但注意,

command > file 2>file 与command > file 2>&1 是不同的:

 

command > file 2>file :是将命令所产生的标准输出信息stdout和错误的输出信息stderr送到file中,但这样会导致file会被打开两次,所以stdout和stderr会互相覆盖。实际上FD1和FD2同时使用file,引起资源竞争。

command >file 2>&1:将stdout直接送向file,而stderr是继承FD1管道后被送往file,所以file 只被打开一次,stdout和stderr共用FD1一个管道。实际相当于stderr合并到stdout后一起输出到file中。

从IO效率上,前一条命令的效率要比后面一条的命令效率要低,所以在编写shell脚本的时候,较多的时候我们会用command > file 2>&1 这样的写法.

>输出重定向

[root@linux-151 ~]# echo "112233">1.txt[root@linux-151 ~]# cat 1.txt112233

>>追加重定向

[root@linux-151 ~]# echo "aabbcc">>1.txt[root@linux-151 ~]# cat 1.txt112233Aabbcc

2> 错误重定向

[root@linux-151 ~]# ls aaa.txt 2>2.txt[root@linux-151 ~]# cat 2.txtls: 无法访问aaa.txt: 没有那个文件或目录

2>>错误追加重定向

[root@linux-151 ~]# ls aaa.txt 2>>2.txt[root@linux-151 ~]# cat 2.txtls: 无法访问aaa.txt: 没有那个文件或目录ls: 无法访问aaa.txt: 没有那个文件或目录

&> == >+2>正确和错误重定向

[root@linux-151 ~]# ls 1.txt aaa.txt &> 1.txt[root@linux-151 ~]# cat 1.txtls: 无法访问aaa.txt: 没有那个文件或目录1.txt

&>>

[root@linux-151 ~]# ls 1.txt aaa.txt &>> 1.txt[root@linux-151 ~]# cat 1.txtls: 无法访问aaa.txt: 没有那个文件或目录1.txtls: 无法访问aaa.txt: 没有那个文件或目录1.txt

可以将正确输出到一个文件中,错误输入到另一个文件中,也可以同时输出到一个文件中(用&>)。

[root@linux-151 ~]# ls 1.txt aaa.txt > 1.txt 2>2.txt[root@linux-151 ~]# cat 1.txt1.txt[root@linux-151 ~]# cat 2.txtls: 无法访问aaa.txt: 没有那个文件或目录

< 输入重定向

[root@linux-151 ~]# wc -l < /etc/passwd27左边只能是命令,不能是文件。

转载于:https://my.oschina.net/u/3804357/blog/1798782

你可能感兴趣的文章
css标签的使用
查看>>
apache+tomcat,搭建负载均衡服务器
查看>>
Andro - Multipurpose OpenCart 2.X 自适应主题模板 ABC-0651
查看>>
Linux设备模型(总线、设备、驱动程序和类)
查看>>
windows 环境下Eclipse开发MapReduce环境设置
查看>>
python--练习--for i in range(2,101)
查看>>
每天一个linux命令(25):chgrp命令
查看>>
【15】万魂杀服务器开发之原始NIO、Mina、Netty使用
查看>>
git rebase之前需要 commit 才行
查看>>
zabbix2.4.6升级zabbix3.0.8后无法发送报警邮件
查看>>
android -------- 打开本地浏览器或指定浏览器加载,打电话,打开第三方app
查看>>
fscanf、fprintf的返回值
查看>>
Notepad++ 快捷键
查看>>
当Win10软件窗口消失了怎么办
查看>>
我是怎么招聘程序员的
查看>>
shell逻辑判断、文件属性判断、if特殊用法、case判断
查看>>
Jenkins在Windows下的安装与配置
查看>>
数据结构与算法之递归和分治思想
查看>>
十天学会php之第二天
查看>>
Win7-64位 Cygwin编译Redis
查看>>