zoukankan      html  css  js  c++  java
  • Linux5.2 Shell基础上

    Shell介绍

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

    命令历史

       历史命令保存在用户家目录下的 .bash_history

    [root@chy002 ~]# cat -n /root/.bash_history
         1  init 0
         2  dhclient
        ... ...
       987  ps aux| grep httpd
       988  cat /etc/selinux/config
       989  init 0
    
    [root@chy002 ~]# echo $HISTSIZE     #命令最大保存数量
    1000
    
    #可以通过环境变量 $HISTSIZE 改变最大保存数,在配置文件/etc/profile中修改,source  /etc/profile 生效
    
    #history -c  可以清空内存当中的命令历史清空
    #不能清空命令历史文件里的记录
    [root@chy002 ~]# history -c     
    #刚输入的命令不会马上记录到命令历史文件中,只有退出终端后才会录入
    
    
    #修改history命令格式,显示时间。在配置文件中添加。#HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
    
    
    1. 快捷显示上一条命令 !!
    2. 运行history列表里某一条命令  !n
    3. 倒序以XXX开头的第一个命令 !XXX
    4. 永久保存命令文件,只能添加不能删除        chattr  +a ~/.bash_history
    

    命令补全及别名

      tab键,补全命令及文件路径。

      自动补全命令参数补全,需要安装 yum install -y bash-completion   ,然后再重启系统才可以生效

    [root@chy002 ~]# systemctl re
    reboot                 reload-or-restart      reset-failed
    reenable               reload-or-try-restart  restart
    reload                 rescue
    

      自定义的alias放到用户家目录下的  ~/.bashrc,还有一些 /etc/profile.d/中脚本定义,unalias取消自定义别名。

    [root@localhost ~]# cat /root/.bashrc
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
            . /etc/bashrc
    fi
    
    [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 restartnet='systemctl restart network.service'
    alias rm='rm -i'
    alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    

    通配符及重定向

    通配符
    1.  ls  *.txt*     统配若干任意字符
    2.  ls  ?.txt      统配一个任意字符
    3.  ls   [0-4].txt   ; ls  [0-9a-zA-Z].txt 统配0、1、2、3、4任意其一个名字的.txt全部列出来
    4.  ls    {0,2}.txt =  ls  [0,2].txt    不包含1.txt
    输入输出重定向
    1. cat 1.txt > 2.txt  前面输出重定向到后面,会把后面的重写
    2. cat  1.txt  >> 2.txt   前面输出追加到后面
    3. ls  aaa.txt.  2>  a.txt   前面命令错误输出重定向到后面
    4.  ls  aaa.txt  2>> a.txt   前面命令错误输出追加到后面
    5.  &>     正确错误输出全重定向到后面,也支持追加
    6. 命令  >1.txt  2>2.txt  一条命令的正确输出和错误分别保存
    7. wc -l  <  1.txt   【查看1.txt行数】右边的内容输入重定向到左边的命令中输入,从右到左只能左边是命令。
  • 相关阅读:
    【转载】这是炎热小镇慵懒的一天
    【原创】Google的文本内容对比代码
    【原创】你知道Oracle 10G能存多少数据吗
    【原创】一个亿级数据库优化过程
    【原创】关于not in的一些事情
    【原创】自动结束进程脚本
    Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
    Android 解决ListView 和 ScrollView 共存冲突的问题
    使用Symfony2的组件创建自己的PHP框架
    数据管理 ListView SQLite Dialog
  • 原文地址:https://www.cnblogs.com/chyuanliu/p/7841145.html
Copyright © 2011-2022 走看看