Shell基础-1

本文索引

  • shell基础
  • 命令历史history
    • 关于命令记录
    • 修改命令历史记录格式
    • 永久保存历史记录
    • 非正常退出终端
    • 命令历史快捷操作
  • 命令补全和别名
    • 命令补全
    • 参数补全
    • 别名的创建和使用
    • 取消别名
  • 通配符基础
  • 输入输出重定向

shell基础

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

很多Linux发行版(包括centos)的默认shell是bash(Bourne Again Shell);此外用户还可以自定义设置自己的shell(usermod -s SHELL USER),CentOS7默认支持的shell如下:

# 当前系统使用的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。

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  2017/09/28 20:29:49halt
  518  2017/09/28 20:29:58vim /etc/profile
  519  2017/09/28 20:31:04source /etc/profile
  520  2017/09/28 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 9月  28 21:02 1.txt
-rw-r--r--. 1 root root 0 9月  28 21:02 3.txt

[] 范围内任选其一

# 测试
[root@centos7 test]# ls -l 
总用量 0
-rw-r--r--. 1 root root 0 9月  28 21:02 1.txt
-rw-r--r--. 1 root root 0 9月  28 21:02 3.txt
-rw-r--r--. 1 root root 0 9月  28 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

转载于:https://my.oschina.net/LuCastiel/blog/1563384

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值