8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.5 输入输出重定向 # Linux shell 基础 # 8.1 shell 介绍 - 什么是shell 1. shell 是一个命令解释器,提供用户和机器之间的交互 2. 支持特定语法,比如逻辑判断、循环 3. 每个用户都可以有自己特定的shell 4. CentOS7 默认shell 为bash (Bourne Agin Shell) 5. 还有zsh、ksh等 ``` [[email protected] ~]# [[email protected] ~]# yum list |grep zsh autojump-zsh.noarch 22.3.0-3.el7 epel zsh.x86_64 5.0.2-25.el7_3.1 updates zsh-html.x86_64 5.0.2-25.el7_3.1 updates zsh-lovers.noarch 0.9.0-1.el7 epel [[email protected] ~]# yum list |grep ksh ksh.x86_64 20120801-26.el7 base mksh.x86_64 46-5.el7 base python-XStatic-Rickshaw.noarch 1.5.0.0-4.el7 epel python-moksha-common.noarch 1.2.3-2.el7 epel python-moksha-hub.noarch 1.4.8-1.el7 epel python-moksha-wsgi.noarch 1.2.2-2.el7 epel [[email protected] ~]# ``` # 8.2 命令历史 ![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170814/225032244.png?imageslim) - 很多系统里面的命令都是存在用户的家目录下, /root/.bash_history ``` [[email protected] ~]# ls /root/.bash_history /root/.bash_history [[email protected] ~]# cat /root/.bash_history echo $? yum list |grep -i apr yum list |grep -i pcre yum install -y pcre.x86_64 yum install -y pcre-devel.x86_64 ./configure --prefix=/usr/local/apache2 echo $? make echo $? make install echo $? ls /usr/local/apache2 init 0 [[email protected] ~]# [[email protected] ~]# echo $HISTSIZE 1000 [[email protected] ~]# ``` - 这是我们之前存的命令,这个文件里最大可以存1000条 - histroy -c 仅仅是把内存当中的命令给清空了,并不会去删除配置文件 ``` [[email protected] ~]# history -c [[email protected] ~]# history 8 history [[email protected] ~]# cat .bash_history [[email protected] ~]# ls -l .bash_history -rw-------. 1 root root 15810 8月 12 23:03 .bash_history [[email protected] ~]# ``` - 变量HISTSIZE 可以定义的,在/etc/profile ``` [[email protected] ~]# vi /etc/profile HOSTNAME=`/usr/bin/hostname 2>/dev/null` HISTSIZE=1000 if [ "$HISTCONTROL" = "ignorespace" ] ; then export HISTCONTROL=ignoreboth else export HISTCONTROL=ignoredups fi export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL # By default, we want umask to get set. This sets it for login shell # Current threshold for system reserved uid/gids is 200 # You could check uidgid reservation validity in ``` - 可以把HISTSIZE=1000 改为5000 保存 ``` HOSTNAME=`/usr/bin/hostname 2>/dev/null` HISTSIZE=5000 if [ "$HISTCONTROL" = "ignorespace" ] ; then export HISTCONTROL=ignoreboth else export HISTCONTROL=ignoredups fi export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL # By default, we want umask to get set. This sets it for login shell # Current threshold for system reserved uid/gids is 200 # You could check uidgid reservation validity in -- INSERT -- ``` - 虽然改了,但是并没有生效 ``` [[email protected] ~]# vi /etc/profile [[email protected] ~]# echo $HISTSIZE 1000 [[email protected] ~]# ``` - 使用命令source /etc/profile 或者重新进入终端,才会生效 ``` [[email protected] ~]# source /etc/profile [[email protected] ~]# echo $HISTSIZE 5000 [[email protected] ~]# ``` - 会记录日期时间,这个效果是由环境变量改变的,但是只是在当前终端下生效, ``` [[email protected] ~]# history 8 history 9 cat .bash_history 10 ls -l .bash_history 11 vi /etc/profile 12 echo $HISTSIZE 13 source /etc/profile 14 echo $HISTSIZE 15 history [[email protected] ~]# 改变命令历史的格式 [[email protected] ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " [[email protected] ~]# echo $HISTTIMEFORMAT %Y/%m/%d %H:%M:%S [[email protected] ~]# history 8 2017/08/14 23:07:03 history 9 2017/08/14 23:09:09 cat .bash_history 10 2017/08/14 23:10:08 ls -l .bash_history 11 2017/08/14 23:28:55 vi /etc/profile 12 2017/08/14 23:33:10 echo $HISTSIZE 13 2017/08/14 23:34:10 source /etc/profile 14 2017/08/14 23:34:13 echo $HISTSIZE 15 2017/08/14 23:35:40 history 16 2017/08/14 23:39:00 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 17 2017/08/14 23:39:15 echo $HISTTIMEFORMAT 18 2017/08/14 23:39:28 history [[email protected] ~]# ``` - 改变命令历史的格式,这个只针对当前终端下生效,如果想要所有的都生效,需要编辑vi/etc/profile 文件 把HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 加入到文件里面 vi/etc/profile ``` HOSTNAME=`/usr/bin/hostname 2>/dev/null` HISTSIZE=5000 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 加入这一行 if [ "$HISTCONTROL" = "ignorespace" ] ; then export HISTCONTROL=ignoreboth else export HISTCONTROL=ignoredups [[email protected] ~]# vi /etc/profile [[email protected] ~]# source !$ source /etc/profile [[email protected] ~]# ``` - 这时候再打开另一个终端就可以看到生效了 ![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170814/234805048.png?imageslim) ``` [[email protected] ~]# echo $HISTTIMEFORMAT %Y/%m/%d %H:%M:%S [[email protected] ~]# 987 2017/08/14 23:46:55 echo $? 988 2017/08/14 23:46:55 yum list |grep -i apr 989 2017/08/14 23:46:55 yum list |grep -i pcre 990 2017/08/14 23:46:55 yum install -y pcre.x86_64 991 2017/08/14 23:46:55 yum install -y pcre-devel.x86_64 992 2017/08/14 23:46:55 ./configure --prefix=/usr/local/apache2 993 2017/08/14 23:46:55 echo $? 994 2017/08/14 23:46:55 make 995 2017/08/14 23:46:55 echo $? 996 2017/08/14 23:46:55 make install 997 2017/08/14 23:46:55 echo $? 998 2017/08/14 23:46:55 ls /usr/local/apache2 999 2017/08/14 23:46:55 init 0 1000 2017/08/14 23:46:57 clear 1001 2017/08/14 23:47:07 echo $HISTTIMEFORMAT 1002 2017/08/14 23:48:13 history [[email protected] ~]# ``` - 改变命令历史的格式就成功了 - 永久保存命令历史 chattr +a ~/.bash_history ``` [[email protected] ~]# chattr +a ~/.bash_history ``` - 这样运行过的命令都会记录下来, - 但是如果不正常退出,有时候敲了一些命令,但是你没有logout ,exit 退出,而是直接关闭终端,那样就会记录不全,命令就保存的不全。 - 命令 !! 2个!其实就是你运行的 上一条命令(最后一条命令) ``` [[email protected] ~]# ls 111 1_heard.txt 1.txt~ 234 3.txt anaconda-ks.cfg.1 123 1_sorft.txt 1.txt.bak 2.txt.bak 4.txt biji.txt [[email protected] ~]# !! ls 111 1_heard.txt 1.txt~ 234 3.txt anaconda-ks.cfg.1 123 1_sorft.txt 1.txt.bak 2.txt.bak 4.txt biji.txt [[email protected] ~]# ``` - !n 表示运行history里面的第n条命令 ``` [[email protected] ~]# history 8 2017/08/14 23:07:03 history 9 2017/08/14 23:09:09 cat .bash_history 10 2017/08/14 23:10:08 ls -l .bash_history 11 2017/08/14 23:28:55 vi /etc/profile 12 2017/08/14 23:33:10 echo $HISTSIZE 13 2017/08/14 23:34:10 source /etc/profile 14 2017/08/14 23:34:13 echo $HISTSIZE 15 2017/08/14 23:35:40 history 16 2017/08/14 23:39:00 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 17 2017/08/14 23:39:15 echo $HISTTIMEFORMAT 18 2017/08/14 23:39:28 history 19 2017/08/14 23:43:15 vi /etc/profile 20 2017/08/14 23:45:50 source /etc/profile 21 2017/08/14 23:51:50 chattr +a ~/.bash_history 22 2017/08/14 23:54:18 history 23 2017/08/14 23:55:50 ls 24 2017/08/14 23:56:13 history [[email protected] ~]# !10 ls -l .bash_history -rw-------. 1 root root 15881 8月 14 23:51 .bash_history [[email protected] ~]# ``` - !word 它会在命令历史里面倒着往上找第一个有word的命令 - 比如运行命令!echo 就是运行命令历史里 倒着数第一个有ehco 相关的命令 ``` [[email protected] ~]# history 8 2017/08/14 23:07:03 history 9 2017/08/14 23:09:09 cat .bash_history 10 2017/08/14 23:10:08 ls -l .bash_history 11 2017/08/14 23:28:55 vi /etc/profile 12 2017/08/14 23:33:10 echo $HISTSIZE 13 2017/08/14 23:34:10 source /etc/profile 14 2017/08/14 23:34:13 echo $HISTSIZE 15 2017/08/14 23:35:40 history 16 2017/08/14 23:39:00 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 17 2017/08/14 23:39:15 echo $HISTTIMEFORMAT 18 2017/08/14 23:39:28 history 19 2017/08/14 23:43:15 vi /etc/profile 20 2017/08/14 23:45:50 source /etc/profile 21 2017/08/14 23:51:50 chattr +a ~/.bash_history 22 2017/08/14 23:54:18 history 23 2017/08/14 23:55:50 ls 24 2017/08/14 23:56:13 history [[email protected] ~]# !10 ls -l .bash_history -rw-------. 1 root root 15881 8月 14 23:51 .bash_history [[email protected] ~]# !echo echo $HISTTIMEFORMAT %Y/%m/%d %H:%M:%S [[email protected] ~]# ``` - 命令历史里 关于ehco的命令是 echo $HISTTIMEFORMAT%Y/%m/%d %H:%M:%S,所以 !echo 就是运行这个命令echo $HISTTIMEFORMAT%Y/%m/%d %H:%M:%S # 8.3 命令补全和别名 ![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170815/220451944.png?imageslim) - 按tab一下补全命令,tab按俩下 列出以命令开头的命令 ``` [[email protected] ~]# ls ls lsblk lsinitrd lslocks lsmod lspci lsattr lscpu lsipc lslogins lsns lsscsi [[email protected] ~]# mk mkdict mkfifo mkfs.ext2 mkfs.xfs mknod mkdir mkfs mkfs.ext3 mkhomedir_helper mkpasswd mkdumprd mkfs.btrfs mkfs.ext4 mkinitrd mkswap mke2fs mkfs.cramfs mkfs.minix mklost+found mktemp [[email protected] ~]# mktemp ``` - 运行mk tab俩下就会显示一堆以mk开头的命令,按mkt tab一下就会自动补全mktemp - centos7 里面支持参数补全 1. 需要安装一个包 ``` [[email protected] ~]# systemctl restart network^C [[email protected] ~]# yum install -y bash-completion 已安装: bash-completion.noarch 1:2.1-6.el7 完毕! [[email protected] ~]# ``` 2. 安装完需要重启下系统才可以reboot 或者 init 6 3. 重启系统之后,先看下那个包是否安装,然后输入部分命令尝试按下tab 看看是否会补全 ``` [[email protected] ~]# rpm -qa bash-completion bash-completion-2.1-6.el7.noarch [[email protected] ~]# systemctl res rescue reset-failed restart [[email protected] ~]# systemctl res rescue reset-failed restart [[email protected] ~]# systemctl res rescue reset-failed restart [[email protected] ~]# systemctl restart network network-online.target network.service [[email protected] ~]# systemctl restart network.service [[email protected] ~]# ``` - alias 别名 给名重新起个名字 用alias 把systemctl restart network.service 改为 restartnet ``` [[email protected] ~]# systemctl restart network.service [[email protected] ~]# alias restartnet=‘systemctl restart network.service‘ [[email protected] ~]# restartnet 把系统里所有的alias 都列出来 [[email protected] ~]# 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‘ [[email protected] ~]# ``` - 取消自定义别名 ``` [[email protected] profile.d]# unalias restartnet [[email protected] profile.d]# restartnet -bash: restartnet: 未找到命令 [[email protected] profile.d]# ``` - 这些alias存在哪里呢 .bashrc , /etc/profile.d ``` [[email protected] ~]# vi .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 ~ ~ ``` 2. 这个/etc/profile.d文件下面的 colorls.sh ,colorgrep.sh 也有 ``` [[email protected] ~]# cd /etc/profile.d [[email protected] profile.d]# ls 256term.csh colorgrep.csh colorls.sh less.csh vim.sh 256term.sh colorgrep.sh lang.csh less.sh which2.csh bash_completion.sh colorls.csh lang.sh vim.csh which2.sh [[email protected] profile.d]# vi colorls.sh ~ ".bashrc" 12L, 176C alias ll=‘ls -l‘ 2>/dev/null alias l.=‘ls -d .*‘ 2>/dev/null INCLUDE= COLORS= for colors in "$HOME/.dir_colors.$TERM" "$HOME/.dircolors.$TERM" "$HOME/.dir_colors" "$HOME/.dircolors"; do [ -e "$colors" ] && COLORS="$colors" && INCLUDE="`/usr/bin/cat "$COLORS" | /usr/bin/grep ‘^INCLUDE‘ | /usr/bin/cut -d ‘ ‘ -f2-`" && break done /alias ``` 3. colorgrep.sh 文件也有 ``` # color-grep initialization /usr/libexec/grepconf.sh -c || return alias grep=‘grep --color=auto‘ 2>/dev/null alias egrep=‘egrep --color=auto‘ 2>/dev/null alias fgrep=‘fgrep --color=auto‘ 2>/dev/null ~ "colorgrep.sh" 7L, 201C ``` # 8.4 通配符 ![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170815/223720549.png?imageslim) - *.txt *表示任何字符 ``` [[email protected] ~]# ls 111 1_heard.txt 1.txt~ 234 3.txt anaconda-ks.cfg.1 123 1_sorft.txt 1.txt.bak 2.txt.bak 4.txt biji.txt [[email protected] ~]# ls *.txt 1_heard.txt 1_sorft.txt 3.txt 4.txt biji.txt [[email protected] ~]# ls *txt 1_heard.txt 1_sorft.txt 3.txt 4.txt biji.txt [[email protected] ~]# ls *txt* 1_heard.txt 1_sorft.txt 1.txt~ 1.txt.bak 2.txt.bak 3.txt 4.txt biji.txt [[email protected] ~]# ls 1* 1_heard.txt 1_sorft.txt 1.txt~ 1.txt.bak 111: 12.tx~ 12.txt 12_txt.swp 222 4913 aming3 123: aminglinux.log yum.log [[email protected] ~]# ``` - ls ?.txt ?表示一个字符 任意的字符 ``` [[email protected] ~]# touch 2.txt [[email protected] ~]# touch 1.txt [[email protected] ~]# ls ?.txt 1.txt 2.txt 3.txt 4.txt [[email protected] ~]# touch a.txt [[email protected] ~]# touch bb.txt [[email protected] ~]# ls ?.txt 1.txt 2.txt 3.txt 4.txt a.txt [[email protected] ~]# ``` - ls [0-9].txt []13.txt 或者的意思 ``` [[email protected] ~]# ls [0-3].txt 1.txt 2.txt 3.txt [[email protected] ~]# ls [123].txt 1.txt 2.txt 3.txt [[email protected] ~]# ls [23].txt 2.txt 3.txt [[email protected] ~]# ls [13].txt 1.txt 3.txt [[email protected] ~]# ls [0-9].txt 1.txt 2.txt 3.txt 4.txt [[email protected] ~]# ls [0-9a-zA-Z].txt 1.txt 2.txt 3.txt 4.txt a.txt [[email protected] ~]# ``` - ls {1,2}.txt ``` [[email protected] ~]# ls {1,2}.txt 1.txt 2.txt [[email protected] ~]# ls {1,2,3,a}.txt 1.txt 2.txt 3.txt a.txt [[email protected] ~]# ``` ## 8.5 输入输出重定向 - [ ] > 正确的输出重定向 cat 1.txt >2.txt - [ ] >> 正确的追加 cat 1.txt >>2.txt - 2> 错误的输出重定向 ls aaa.txt 2>err - 2>> 错误的追加 ls aaa.txt 2>>err ``` [[email protected] ~]# lsaaa -bash: lsaaa: 未找到命令 [[email protected] ~]# lsaaa 2> a.txt [[email protected] ~]# cat a.txt -bash: lsaaa: 未找到命令 [[email protected] ~]# lsaaa 2>>a.txt [[email protected] ~]# cat a.txt -bash: lsaaa: 未找到命令 -bash: lsaaa: 未找到命令 [[email protected] ~]# > >> 2> 2>> >+2> == &>^C [[email protected] ~]# ``` - &> 可以把正确和错误的放一起 ``` [[email protected] ~]# ls [12].txt aaa.txt &> a.txt [[email protected] ~]# cat a.txt ls: 无法访问aaa.txt: 没有那个文件或目录 1.txt 2.txt [[email protected] ~]# ``` - &> 同样也支持追加 ``` [[email protected] ~]# ls [12].txt aaa.txt &>> a.txt [[email protected] ~]# cat a.txt ls: 无法访问aaa.txt: 没有那个文件或目录 1.txt 2.txt ls: 无法访问aaa.txt: 没有那个文件或目录 1.txt 2.txt [[email protected] ~]# ``` - &> 既可以放正确也可以发个错误的输出信息 保存到指定的文件里 ``` [[email protected] ~]# ls [12].txt aaa.txt > 1.txt 2>a.txt [[email protected] ~]# cat 1.txt 1.txt 2.txt [[email protected] ~]# cat a.txt ls: 无法访问aaa.txt: 没有那个文件或目录 [[email protected] ~]# ``` - 输入重定向 把右边的一个文件,文件的内容 给它左边 输入到一个命令里面去 ``` [[email protected] ~]# wc -l < 1.txt 2 [[email protected] ~]# 2.txt < 1.txt -bash: 2.txt: 未找到命令 [[email protected] ~]# ``` - [ ] 左边必须是一个命令,不可以是文件 输入重定向用的比较少,做一个了解
时间: 2024-12-24 17:06:12