博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
history、重定向、alias
阅读量:7220 次
发布时间:2019-06-29

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

hot3.png

第八章 shell基础

8.1 shell介绍

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

8.2 命令历史(history)

history命令

语法: history [-c]

-c:=clear 清除内存中的命令,不能删除配置文件中的历史命令

[root@3 ~]# history    1  ls    2  ls /tmp/    3  ls /boot/    4  ls /    5  dhclient……[root@3 ~]# ls /root/.bash_history/root/.bash_history     history的家目录

显示使用过的命令历史,默认保存1000条使用过的命令(注:此令需要是在正常关机操作情况下的处1000条命)!

history环境变量

  • 变量HISTSIZE
[root@3 ~]# echo $HISTSIZE1000

该变量决定命令历史保存的命令的数目。

  • 定义变量HISTSIZE
编辑其配置文件[root@3 ~]# vim /etc/profile ……HOSTNAME=`/usr/bin/hostname 2>/dev/null`HISTSIZE=1000……[root@3 ~]# echo $HISTSIZE1000[root@3 ~]# source /etc/profile[root@3 ~]# echo $HISTSIZE2000

搜索关键字"HIST"找到‘HISTSIZE=1000’,在此更改其数字,保存退出,然后执行命令‘source /etc/profile’刷新该配置文件才会生效。

  • 更改history显示格式
[root@3 ~]# echo $HISTTIMEFORMAT[root@3 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "[root@3 ~]# echo $HISTTIMEFORMAT%Y/%m/%d %H:%M:%S[root@3 ~]# history    1  2017/06/28 18:50:11 history    2  2017/06/28 18:51:32 echo $HISTTIMEFORMAT    3  2017/06/28 18:51:43 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "    4  2017/06/28 18:51:45 echo $HISTTIMEFORMAT    5  2017/06/28 18:52:32 history

直接为‘HISTTIMEFORMAT’赋值即可,不过此时该格式只适用于当前终端。如果要其使用于所有用户,则需要将其写入history配置文件并刷新后生效。

[root@3 ~]# vim /etc/profile ……HOSTNAME=`/usr/bin/hostname 2>/dev/null`HISTSIZE=1000HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "……保存退出![root@3 ~]# source /etc/profile
  • 命令历史永久保存
    即,使命令历史记录只能写入不能被删除!
[root@3 ~]# chattr +a ~/.bash_history

使用文件特殊权限,为‘.bash_history’文件配置‘a’权限(只可追加,不可删除),限于正常关机操作。

‘!!’命令

[root@3 ~]# w……[root@3 ~]# !!w……

‘!’的用法:‘!n’(n代表数字),表示运行命令历史中的第n条命令;‘!word’,表示运行上一次以该word开头的命令。

eg:

[root@3 ~]# history    1  2017/06/28 18:50:11 history    2  2017/06/28 18:51:32 echo $HISTTIMEFORMAT    3  2017/06/28 18:51:43 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "    4  2017/06/28 18:51:45 echo $HISTTIMEFORMAT    5  2017/06/28 18:52:32 history[root@3 ~]# !4echo $HISTTIMEFORMAT%Y/%m/%d %H:%M:%S[root@3 ~]# !HISTHISTSIZE=1000

8.3 命令补全和别名

命令补全Tab

按一次tab可以补全一个命令或参数(需要安装包bash-completion,并重启系统);按两次tab可以显示以某字母开头的所有命令或文件名。

alias命令

语法: alias [命令别名]=[具体命令] 设置别名

取消别名:unalias [命令别名]

直接输入alias会显示系统所有的别名:

[root@3 ~]# 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'[root@3 ~]#

系统别名存放在配置文件‘~/.bashrc’和‘ls /etc/profile.d/’下:

[root@3 ~]# cat !$cat .bashrc# .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[root@3 ~]# ls /etc/profile.d/256term.csh         colorgrep.sh  lang.sh                qt-graphicssystem.sh  which2.sh256term.sh          colorls.csh   less.csh               vim.cshbash_completion.sh  colorls.sh    less.sh                vim.shcolorgrep.csh       lang.csh      qt-graphicssystem.csh  which2.csh

8.4 通配符

  • 通配符‘*’代表零个或多个任意字符
  • 通配符‘?’代表一个任意字符
  • 中括号‘[]’,“ls [0-9].txt”表示0-9区间内的任意.txt文件
  • 花括号‘{}’,“ls {1,2,3}.txt”表示括号内任意.txt文件

输入输出重定向

“>,>>,<,2>,2>>”

‘>’:输出重定向
‘>>’:追加重定向
‘2>’:错误重定向
‘<’:输入重定向
使用‘>’命令时会将文件内原有内容删除。

[root@3 tmp]# echo 3xuelinux > 1.txt[root@3 tmp]# cat 1.txt3xuelinux[root@3 tmp]# echo 3xu > 1.txt[root@3 tmp]# cat 1.txt3xu#####################################[root@3 tmp]# echo 3xu >> 1.txt[root@3 tmp]# cat 1.txt3xu3xu#####################################[root@3 tmp]# lsaaa-bash: lsaaa: 未找到命令[root@3 tmp]# lsaaa 2> 2.txt[root@3 tmp]# cat 2.txt-bash: lsaaa: 未找到命令输入重定向:必须定向到(
  • 应用
[root@3 tmp]# ls {1,2}.txt aaaa.txt > 1.txt 2> 3.txt[root@3 tmp]# cat 1.txt1.txt2.txt[root@3 tmp]# cat 3.txtls: 无法访问aaaa.txt: 没有那个文件或目录

说明: 使用ls命令查看 {1,2}.txt aaaa.txt,1.txt和2.txt文件存在,可以使用ls查看,aaaa.txt不存在,使用ls查看会报错,‘> 1.txt 2> 3.txt’意思是将正确信息保存到1.txt,将错误信息保存到3.txt。

转载于:https://my.oschina.net/u/3706800/blog/1631278

你可能感兴趣的文章
Linux下解决中文乱码问题
查看>>
nginx一个简单的反向代理设置
查看>>
Errors occurred during the build
查看>>
一个关于js的内存问题
查看>>
Python字符串,整型,浮点数相互转化
查看>>
Spring中RedirectAttributes的用法
查看>>
Hive:处理时间的函数
查看>>
photoshop学习
查看>>
P2600 [ZJOI2008]瞭望塔
查看>>
poj 2282 The Counting Problem & 3286 How many 0's?
查看>>
****Linux MySQL命令运用个人总结
查看>>
Android IPC机制(二)用Messenger进行进程间通信
查看>>
选择排序Java版
查看>>
vue 遍历数组的巧妙之处
查看>>
【中文】Joomla1.7扩展介绍之uddeIM (私信系统)
查看>>
Java知多少(67)面向字符的输入流
查看>>
1105. Spiral Matrix (25)
查看>>
第三天 笔记
查看>>
烂泥:使用nginx利用虚拟主机搭建WordPress博客
查看>>
Codeforces Round #450 (Div. 2)
查看>>