shell基础1

 

10月11日任务shell

8.1 shell介绍apache

8.2 命令历史vim

8.3 命令补全和别名centos

8.4 通配符bash

8.5 输入输出重定向less

 

shell基础

shell--命令解释器,提供用户和机器的交互,输入命令,输出结果。测试

不少Linux发行版(包括centos)的默认shell是bash(Bourne Again Shell);此外用户还能够自定义设置本身的shell(usermod -s SHELL USER),CentOS7默认支持的shell以下:centos7

# 当前系统使用的shell
[root@localhost ~]# echo $SHELL
/bin/bash

# 系统默认支持的shell
[root@localhost ~]# cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin

还能够经过yum安装zsh、ksh等其余的shell。spa

shell支持特定语法,如逻辑判断,循环等插件

历史记录 history

用户执行过的命令都会记录到其家目录下的 .bash_history文件中!

历史命令记录默认最大1000条,可是能够经过修改环境变量HISTSIZE(/etc/profile配置文件中修改)来调整历史记录的条数。

[root@localhost ~]# echo $HISTSIZE
1000

注意,并非修改完/etc/profile文件,HISTSIZE就会当即生效,要想当即生效,执行source /etc/profile命令。

关于命令记录

当前用户登陆后执行的命令,会暂时存储在内存中,只有在用户退出终端才会被写入.bash_history文件中!!

修改命令历史记录格式

自定义一个环境变量,使历史命令储存时保存时间。

暂时改变格式,只在当前终端下生效
[root@centos7 ~]# HISTTIMEFORMAT=“%Y/%m/%d %H:%M:%S”

永久保存格式
[root@centos7 ~]# vim /etc/profile#在HISTSIZE变量后追加一行
HISTTIMEFORMAT=“%Y/%m/%d %H:%M:%S”
:wq 保存退出

#马上生效修改
[root@centos7 ~]# source /etc/profile

[root@centos7 ~]# history
.....
517 ... 20:29:49halt
518 ... 20:29:58vim /etc/profile
519 ... 20:31:04source /etc/profile
520 ... 20:31:12history

HOSTTIMEFORMAT环境变量在用户命令行下设置,变量只是本地变量,只在当前终端有效!

永久保存历史记录(隐藏权限)

# 经过设置a隐藏权限,使文件没法删除以前多余的命令,只能追加
[root@centos7 ~]# chattr +a ~/.bash_history

非正常退出终端

使用终端执行命令时,若是直接关闭终端,会致使**.bash_history文件没法正常记录完整执行记录。所以在退出终端时,应该使用exitlogout**命令正常退出!

命令历史快捷操做

  • 上一条命令 :!!
  • 执行history内的第n条命令:!n
  • 执行history内的最新的以command开头的命令:!command

命令补全和别名

命令补全:tab键

  • 按一下:待选命令就一个,会自动补全
  • 按两下:显示待选命令列表
[root@localhost ~]# ch
chacl      chattr     chfn       chmod      chronyc    chrt
chage      chcon      chgrp      chown      chronyd    chsh
chat       chcpu      chkconfig  chpasswd   chroot     chvt

参数补全(默认不支持)

CentOS7支持参数补全(需安装插件),CentOS6不支持。

  • 安装软件包:yum install -y **bash-completion **

安装完毕需重启系统!!!

别名的建立和使用

  • 查看当前系统内的别名
[root@localhost ~]# alias
alias 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@localhost ~]# alias lt='ls -lt'
[root@localhost ~]# lt ./
总用量 68
-rw-r--r--. 1 root root   146 11月  4 09:12 file.py
-rw-r--r--. 1 root root   586 10月 27 21:16 class.py
drwxr-xr-x. 3 root root    58 10月 27 19:49 111
lrwxrwxrwx. 1 root root    11 10月 27 19:47 test -> /etc/passwd
-rw-r--r--. 1 root root   225 10月 26 22:58 try.py
-rw-r--r--. 1 root root    93 10月 23 18:38 iptables.log
-rw-------. 1 root root  1422 10月 18 02:47 anaconda-ks.cfg
  • 系统定义的部分和用户自定义的别名:~/bashrc文件
[root@localhost ~]# cat ~/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
...
  • 其余别名:/etc/profile.d/*.sh
[root@localhost ~]# ls -l /etc/profile.d/*.sh
-rw-r--r--. 1 root root  841 11月  6 2016 /etc/profile.d/256term.sh
-rw-r--r--. 1 root root  201 4月  29 2015 /etc/profile.d/colorgrep.sh
-rw-r--r--. 1 root root 1606 11月  5 2016 /etc/profile.d/colorls.sh
-rw-r--r--. 1 root root 2703 11月  6 2016 /etc/profile.d/lang.sh
-rw-r--r--. 1 root root  121 7月  31 2015 /etc/profile.d/less.sh
-rw-r--r--. 1 root root  269 8月   2 08:45 /etc/profile.d/vim.sh
-rw-r--r--. 1 root root  169 1月  28 2014 /etc/profile.d/which2.sh

取消别名

[root@localhost ~]# unalias lt
[root@localhost ~]# lt ./
-bash: lt: 未找到命令

通配符基础

{} 以逗号链接的选项之一

#同时建立多个文件:{}
[root@centos7 test]# touch {1,3}.txt
[root@centos7 test]# ls -l
总用量 0
-rw-r--r--. 1 root root 0 ... 21:02 1.txt
-rw-r--r--. 1 root root 0 ... 21:02 3.txt

[] 范围内任选其一

# 测试
[root@centos7 test]# ls -l
总用量 0
-rw-r--r--. 1 root root 0 ... 21:02 1.txt
-rw-r--r--. 1 root root 0 ... 21:02 3.txt
-rw-r--r--. 1 root root 0 ... 21:04 aa.txt
  • 匹配0或多个任意字符: *
[root@centos7 test]# ls *.txt
1.txt  3.txt  aa.txt
  • 匹配一个任意字符:?
[root@centos7 test]# ls ?.txt
1.txt  3.txt
  • 匹配范围:[],{}
[root@centos7 test]# ls [0-9].txt
1.txt  3.txt
[root@centos7 test]# ls {1,3}.txt
1.txt  3.txt

输入输出重定向

  • 输出重定向: >
[root@localhost ~]# echo "line 1" > 1.txt
[root@localhost ~]# cat 1.txt 
line 1
# > 覆盖以前的输入
[root@localhost ~]# echo "line 2" > 1.txt
[root@localhost ~]# cat 1.txt 
line 2
  • 重定向追加: >>
[root@localhost ~]# echo "line 3" >> 1.txt 
[root@localhost ~]# cat 1.txt 
line 2
line 3

错误输出重定向和错误输出重定向追加跟以前的相似,它会将命令执行时产生的错误输出重定向或追加到文件中

  • 错误输出重定向: 2>
  • 错误重定向追加:2>>
  • 输入重定向:wc -l < 1.txt
# 将1.txt的内容做为wc命令的输入
[root@localhost ~]# wc -l < 1.txt 
2

具体使用

在使用脚本自动安装源码包时,咱们并不想输出信息至终端,而是想将其存储在文件中,方便后续查看:

[root@localhost httpd-2.2.34]# ./configure --prefix=/usr/local/apache2 >install.log 2> error.log
相关文章
相关标签/搜索