2019独角兽企业重金招聘Python工程师标准>>>

1、shell基础介绍:

shell是一个命令解释器,用于用户与机器的交互:

也支持特定的语法(逻辑判断,循环):

每个用户都有自己特定的shell:Centos7的shell是bash(bourne   agin  shell):

shell还有zsh    ksh这两种:

2、命令历史:  history 

命令历史存放路径:用户的家目录:用history命令可以查看命令历史:

[root@localhost ~]# ls -ld /root/.bash_history      #命令历史所在的家目录
-rw------- 1 root root 13038 Jun 30 22:36 /root/.bash_history
[root@localhost ~]# history                         #可直接查看系统中命令历史994  yum list |grep zsh995  cat .bash_history996  ls -ld /root/.bash_history997  history998  cat .bash_history999  history1000  exit1001  clear1002  history1003  history |tail -10

2.1:history可以存放1000条命令的数目:是有环境变量HISTSIZE决定的:

[root@localhost ~]# echo $HISTSIZE
1000

注:有时超过1000条后再写入时则暂时存在于内存中,当正常退出终端后才写入到文件:

2.2:history  -c :只清空当前内存中的命令,不会清除配置文件中的历史命令:

[root@localhost ~]# history             #第一次看出命令历史记录1001  clear1002  history1003  history |tail -101004  echo $HISTSIZE1005  history |tail -101006  history |tail -6
[root@localhost ~]# history -c         #清除当前内存的命令历史
[root@localhost ~]# history            #再次查看则没有历史记录,当重新开启终端后,则再次显示:8  history

2.3:history命令历史的显示数目可定义:如下:

[root@localhost ~]# vim /etc/profile                    #编辑此配置文件:
[root@localhost ~]# cat /etc/profile |grep -C3 HISTSIZE
fi
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=5000                                    #修改此命令条目为5000:
if [ "$HISTCONTROL" = "ignorespace" ] ; thenexport HISTCONTROL=ignoreboth
elseexport HISTCONTROL=ignoredups
fi
[root@localhost ~]# echo   HISTSIZE=5000
[root@localhost ~]# source /etc/profile         #保存后需要刷新才能生效:
[root@localhost ~]# echo $HISTSIZE              再次查看命令历史:
5000

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

2.4:更改hsitory的显示格式HISTTIMEFORMAT

[root@localhost ~]# HISTTIMEFORMAT="%Y-%m-%d %H-%M-%S "    #直接给变量赋值即可:
[root@localhost ~]# history |tail -4                     #按年月日来显示:1023  2018-07-04 11-39-25 history1024  2018-07-04 11-39-33 HISTTIMEFORMAT="%Y-%m-%d %H-%M-%S "1025  2018-07-04 11-39-35 history1026  2018-07-04 11-39-40 history |tail -4
[root@localhost ~]# su - yuanhh                      #切换到另一个普通用户:
Last login: Wed Jul  4 11:28:53 CST 2018 on pts/1
[yuanhh@localhost ~]$ history                        #则不显示:1  history \2  history3  echo $HISTSIZE4  EXIT5  exit6  history

如上:修改命令历史的格式直接给其变量赋值即可,不过该格式只适用于当前终端,如果要其使用与所有用户,则需要写入到history的配置文件才会生效:

[root@localhost ~]# vim /etc/profile             #修改此文件
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTTIMEFORMAT="%Y-%m-%d %H-%M-%S"               #新增添加这一行了:
HISTSIZE=5000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
[root@localhost ~]# ^C
[root@localhost ~]# source /etc/profile
[root@localhost ~]# su - yuanhh                 #切换到普通用户查看:
Last login: Wed Jul  4 11:37:41 CST 2018 on pts/0
[yuanhh@localhost ~]$ history                   #命令历史显示格式发生变化:1  2018-07-04 11-46-54history \2  2018-07-04 11-46-54history3  2018-07-04 11-46-54echo $HISTSIZE4  2018-07-04 11-46-54EXIT

2.5:命令历史永久保存命令历史只能写入不能删除

[root@localhost ~]# chattr +a /root/.bash_history
[root@localhost ~]# lsattr /root/.bash_history
-----a-------e-- /root/.bash_history

给文件增加+a权限,只能追加,不能删除:也仅仅root用户可以操作:

3、!!命令:

!!:两个叹号表示当前命令历史中的最后一条命令:等同与上翻按键:

!n:n等于数字,表示命令历史中的第n条命令:

!word:表示命令历史中以该word命令开头的命令:

[root@localhost ~]# pwd
/root
[root@localhost ~]# !!
pwd
/root
 1033  2018-07-04 11-59-01ls1034  2018-07-04 11-59-06pwd1035  2018-07-04 11-59-56history
[root@localhost ~]# !1033               #表示第1033条命令:
ls
[root@localhost ~]# !p                  #表示以p开头的命令:
pwd
/root

4、命令补全和别名:  tab     alias  unalias

tab:补全命令:需要安装包:bash-completion   并重新启动系统即可:

按一次tab:补全一个命令或者参数:

按两次tab:补全某字母开头的所有命令及文件名:

4.1:命令别名:alias    unalias

alias              命令别名="具体的命令"                   #设置别名:

unalias         命令别名                                          #取消别名:

[root@localhost ~]# alias p="pwd"             #设置别名”pwd“:
[root@localhost ~]# p
/root
[root@localhost ~]# unalias p                 #取消别名:
[root@localhost ~]# p
-bash: p: command not found

4.2:输入"alias"可查看系统中所有的别名:

[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'

4.2:系统别名的存放在配置文件~/.bashrc/etc/profile.d/下:

[root@localhost ~]# cat ~/.bashrc              #当前用户下.bashrc文件下:
# .bashrc# User specific aliases and functionsalias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'# Source global definitions
if [ -f /etc/bashrc ]; then. /etc/bashrc
fi
[root@localhost ~]# ls /etc/profile.d/
256term.csh         colorls.csh         less.sh
256term.sh          colorls.sh          vim.csh
bash_completion.sh  lang.csh            vim.sh
colorgrep.csh       lang.sh             which2.csh
colorgrep.sh        less.csh            which2.sh

当前用户目录下的.bashrc只对当前用户生效:

/etc/profile.d/目录下的针对于系统中所有用户生效:

5、通配符:  *     ?    [  ]     {    }

*表示匹配零个或多个字符

?表示匹配一个字符

[  ]表示匹配一个范围,中括号区间内的任意一个文件

{  }表示匹配花括号内的任意一个文件

[root@localhost ~]# ls
1.txt  2.txt  3.txt  ba.txt.c  cat.txt  dir1  pk.txt
[root@localhost ~]# ls *.txt             #"*"匹配一个或者多个文件:
1.txt  2.txt  3.txt  cat.txt  pk.txt
[root@localhost ~]# ls *txt*
1.txt  2.txt  3.txt  ba.txt.c  cat.txt  pk.txt
[root@localhost ~]# ls
1.txt  2.txt  3.txt  ba.txt.c  cat.txt  dir1  pk.txt
[root@localhost ~]# ls ?.txt               #”?“只匹配一个字符:
1.txt  2.txt  3.txt
[root@localhost ~]# ls
1.txt  3.txt  5.txt  ba.txt.c  dir1
2.txt  4.txt  6.txt  cat.txt   pk.txt
[root@localhost ~]# ls [1-5].txt          #表示匹配1--5之间的任意.txt文件:
1.txt  2.txt  3.txt  4.txt  5.txt
[root@localhost ~]# ls
1.txt  3.txt  5.txt  ba.txt.c  dir1
2.txt  4.txt  6.txt  cat.txt   pk.txt
[root@localhost ~]# ls {1,2,3}.txt          #匹配花括号内的txt文件:
1.txt  2.txt  3.txt

6、输入输出重定向:   >    >>   2>   2>>   &>     <

>:输出重定向

>>:追加重定向

2>:错误重定向

2>>:错误追加重定向

&>:正确错误重定向

<:输入重定向

5.1:使用">"这个命令时会将源文件内容删除:

[root@localhost ~]# echo "111111" > 1.txt          #写入文件:
[root@localhost ~]# cat 1.txt
111111
[root@localhost ~]# echo "222222" > 2.txt          #再次重定向写入文件:
[root@localhost ~]# cat 2.txt                      #会删除源文件:
222222

使用">>"相当于追加,不删除源文件内容:

[root@localhost ~]# cat 2.txt
222222
[root@localhost ~]# echo "333333" >> 2.txt      #再次追加内容:
[root@localhost ~]# cat 2.txt                   #源文件内容不删除:
222222
333333

错误重定向"2>"错误追加重定向"2>>":

[root@localhost ~]# lsaaa
-bash: lsaaa: command not found
[root@localhost ~]# lsaaa 2> 11.txt           #错误重定向:
[root@localhost ~]# cat 11.txt
-bash: lsaaa: command not found
[root@localhost ~]# ls222 2>> 11.txt          #错误追加重定向:
[root@localhost ~]# cat 11.txt
-bash: lsaaa: command not found
-bash: ls222: command not found
[root@localhost ~]# ls 2> 11.txt             #此时输入一个正确的命令:
11.txt  2.txt  4.txt  6.txt     cat.txt  pk.txt
1.txt   3.txt  5.txt  ba.txt.c  dir1
[root@localhost ~]# cat 11.txt              #查看时发现无内容,说明命令正确时是不会被重定向的:

也可以同时使用重定向错误重定向

分别写入到两个文件1.txt2.txt

[root@localhost ~]# ls -ld 1.txt  aaa.txt >1.txt 2>2.txt
[root@localhost ~]# cat 1.txt            #正确的重定向在这里:
-rw-r--r-- 1 root root 0 Jul  5 16:18 1.txt
[root@localhost ~]# cat 2.txt            #错误的重定向在这里:
ls: cannot access aaa.txt: No such file or directory

如上:正确的文件写入到1.txt里,错误的则写入到2.txt文件下:

也可以写入到同一个1.txt文件里

[root@localhost ~]# ls -ld 1.txt  aaa.txt   &> 1.txt
[root@localhost ~]# cat 1.txt       #正确和错误都输出到同一个文件里:
ls: cannot access aaa.txt: No such file or directory
-rw-r--r-- 1 root root 0 Jul  5 16:23 1.txt#等同于下面的命令:
[root@localhost ~]# ls -ld 1.txt  aaa.txt   > 1.txt 2>&1    #&1:表示前面的1.txt文件:
[root@localhost ~]# cat 1.txt
ls: cannot access aaa.txt: No such file or directory
-rw-r--r-- 1 root root 0 Jul  5 16:24 1.txt

#命令中"&1"表示前面的1.txt文件:

输入重定向:"<"输入重定向的左边必须是一个命令:用的较少:

[root@localhost ~]# wc -l < 1.txt
2

转载于:https://my.oschina.net/yuanhaohao/blog/1840866

day23:shell基础介绍 alias及重定向相关推荐

  1. Shell 基础介绍 [1]

    本文目录 1.什么是Shell? 2.脚本语言类型 3.其他常用的脚本语句种类 4.Shell脚本的建立和执行 5.Shell 变量类型 6.普通变量 7.Shell 特殊重要变量 8.Shell进程 ...

  2. centos shell基础 alias 变量单引号 双引号 history 错误重定向 21 jobs 环境变量 .bash_history source配置文件 nohup ...

    centos shell基础知识 alias  变量单引号 双引号   history 错误重定向 2>&1  jobs  环境变量 .bash_history  source配置文件 ...

  3. Shell脚本基础介绍

    shell基础简介: 编写脚本通常使用某种基于解释器的编程语言.而shell脚本不过就是一些文件,我们能将一系列需要执行的命令写入其中,然后通过shell来执行这些脚本. 进入Linux系统(Ubun ...

  4. linux+管道+分段,Linux中shell基础、重定向、管道符、环境变量

    原标题:Linux中shell基础.重定向.管道符.环境变量 1.什么是shell Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口(命令解释器).它接收用户输入的命令并把它送入内核 ...

  5. 【图文教程】Shell基础知识

    Shell基础知识 1. shell介绍 2. history命令 history主要参数示例: 3. 命令补全和别名 4. 通配符 5. 输入输出重定向 6. 管道符和作业控制 管道符: 作业控制: ...

  6. 史上最牛最强的linux学习笔记 10.shell基础

    史上最牛最强的linux学习笔记 10.shell基础 写在最前面: 本文是基于某站的视频学习所得,第一个链接如下: https://www.bilibili.com/video/BV1mW411i7 ...

  7. shell脚本编程学习笔记1(xdl)——shell基础与Bash基本功能()

    shell脚本编程学习笔记1--shell基础与Bash基本功能 1,简介: 1,Shell就是一个命令行解释器,用以连接输入设别和内核. 2,Shell是解 释执行的脚本语言,在Shell中可以直接 ...

  8. Shell基础(一):Shell基础应用、简单Shell脚本的设计、使用Shell变量、变量的扩展应用...

    一.Shell基础应用 目标: 本案例要求熟悉Linux Shell环境的特点,主要练习以下操作: 1> 切换用户的Shell环境        2> 练习命令历史.命令别名        ...

  9. Linux shell篇---之一--shell基础

    一.shell基础 1.shell的基本概念 shell就是系统跟计算机硬件交互时使用的中间介质,它只是系统的一个工具. 用户界面shell(还有其他用户界面如kde等图形界面)-->内核--& ...

最新文章

  1. Microsoft Azure Site Recovery (1) 安装VMM服务器代理
  2. Android中使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信
  3. (大纲)三小时学会openCV
  4. python爬虫怎么下载图片到手机_Python爬虫获取图片并下载保存至本地
  5. python自己做个定时器_技术图文:如何利用 Python 做一个简单的定时器类?
  6. python中max()、min()获得最大值与最小值_(Python基础教程之十)Python max()和min()–在列表或数组中查找最大值和最小值...
  7. C语言基础 - 输出1-100万之间的素数
  8. 黑马程序员之《String Buffer,包装类》
  9. ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS
  10. ubuntu 常用命令锦集
  11. Webstorm中html无Vue语法提示
  12. C# 之 LINQ(一)
  13. android 键盘 横屏 边框,Android横屏时软键盘全屏问题
  14. 力扣的使用简介及代码编写说明
  15. 路由器恢复出厂设置后dns服务器未响应,路由器恢复出厂设置后连不上网怎么办?...
  16. nowcoder 点击消除 (字符串 + 栈)
  17. 【小沐学Android】Android手机上基于Termux实现Web服务器(Python、node.js、C/C++)
  18. c++语言程序设计教程与实验实验报告,C++程序设计课程设计实验报告—网络五子棋...
  19. pe读linux硬盘分区工具_MBROSTool:U启制作工具,多分区多启动,多合一系统(win+linux),只需一拖一格...
  20. 3721彻底清除方法

热门文章

  1. IntelliJ IDEA 2021.3.2 发布:告别不断建议安装xx插件的提示!
  2. Spring Boot 集成 JUnit5,更优雅单元测试!
  3. Java中的深浅拷贝问题你清楚吗?
  4. 使用 ThreadLocal 一次解决老大难问题!
  5. IntelliJ IDEA 2019.3这回真的要飞起来了,新特性抢先看!
  6. 微服务架构之「 API网关 」
  7. 数据分析利器Jupyter Notebook!
  8. 山东省百万奖金赛事来了!
  9. 一些真诚的学习经验和生活感悟
  10. 华人科学家胡安明被判无罪!曾因「中国行动计划」被FBI紧盯两年,遭软禁18个月...