shell基础

Linux学习

一、shell介绍
二、命令历史
三、命令补全和别名
四、通配符
五、输入输出重定向

一、shell介绍

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

二、命令历史

  • history命令
  • .bash_history
  • 最大1000条命令
  • 变量HISTSIZE
  • /etc/profile中修改
  • HISTTIMEFORMAT="%Y%m%d %H:%M:%S"
  • 永久保存chattr +a ~/.bash_history
  • !!
  • !n
  • !word
  • history -c只清空内存中的命令,不会删除存命令的配置文件
  • 当前输入的命令是不会被直接存放在配置文件中,只有退出当前命令终端后,才会被写入配置文件;
  • 环境变量HISTSIZE配置存放在/etc/profile里
  • source /etc/profile可以直接是命令生效(HISTSIZE=5000)
  • 历史记录有时间显示了。
  • 如果需要永久生效,只要在环境变量/etc/profile里面加入 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S" 即可
  • 永久保存历史命令不被删除,chattr +a ~/.bash_history //直接关闭命令窗口则不被记录为历史命令
  • !!执行上一条命令
  • !n(n是指数字)
  • !echo,从后面往前找,第一个echo开头的命令
操作
[root@localhost ~]# echo $HISTSIZE
1000
[root@localhost ~]# 

[root@localhost ~]# vim /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
    # ksh workaround
    EUID=`/usr/bin/id -u`
    UID=`/usr/bin/id -ru`
fi
USER="`/usr/bin/id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi

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
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
if [ -r "$i" ]; then
    if [ "${-#*i}" != "$-" ]; then 
        . "$i"
    else
        . "$i" >/dev/null
    fi
fi
done

unset i
unset -f pathmunge
[root@localhost ~]# 

三、命令补全和别名

  • tab键,敲一下,敲两下
  • 参数补全,安装bash-completion(yum install -y bash-completion)
  • alias别名给命令重新起名字(取消别名:unalias)
  • 各用户都有自己配置别名的文件~/.bashrc
  • ls /etc/profile.d/
  • 自定义的alias放到~/.bashrc
  • 安装命令补全:yum install -y bash-completion就可用tab补全后面的命令参数
操作
[root@localhost ~]# alias restartnet=-'systemctl restart network.service'
[root@localhost ~]# alias off='init 0'
[root@localhost ~]# 

[root@localhost ~]# !v
vim .bashrc 

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias off='init 0'
alias restartnet='systemctl restart network.service'
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
[root@localhost ~]# source .bashrc         //即时生效

[root@localhost ~]# ls /etc/profile.d/
256term.csh  bash_completion.sh  colorgrep.sh  colorls.sh  lang.csh  less.csh  sh.local  vim.sh      which2.sh
256term.sh   colorgrep.csh       colorls.csh   csh.local   lang.sh   less.sh   vim.csh   which2.csh
[root@localhost ~]# 

四、通配符

  • ls *.txt
  • ls ?.txt
  • ls [0-9].txt
  • ls [1,2].txt
  • cat 1.txt >2.txt
  • cat 1.txt >>2.txt
  • ls aaa.txt 2>err
  • ls aaa.txt 2>>err
  • wc -l < 1.txt
  • command > 1.txt 2>&1
操作
[root@localhost ~]# ls *.txt
11.txt  12.txt  1a.txt  1.txt  22.txt  2a.txt  2.txt  a.txt  b.txt
[root@localhost ~]# 

[root@localhost ~]# ls *.txt
11.txt  12.txt  1a.txt  1.txt  22.txt  2a.txt  2.txt  a.txt  b.txt
[root@localhost ~]# ls ?.txt
1.txt  2.txt  a.txt  b.txt
[root@localhost ~]# ls ??.txt
11.txt  12.txt  1a.txt  22.txt  2a.txt
[root@localhost ~]# ls [0-9].txt
1.txt  2.txt
[root@localhost ~]# ls [0-9,a-z].txt
1.txt  2.txt  a.txt  b.txt
[root@localhost ~]# cat 1.txt > 2.txt 
[root@localhost ~]# cat 1.txt >> 2.txt 
[root@localhost ~]# ls aaa.txt 2>err
[root@localhost ~]# ls aaa.txt 2>>err
[root@localhost ~]# wc -l < 1.txt 
171
[root@localhost ~]# command > 1.txt 2>&1
[root@localhost ~]# wc -l 1.txt 
0 1.txt
[root@localhost ~]# 

五、输入输出重定向

  • cat 1.txt >2.txt //输出
  • cat 1.txt >>2.txt //追加重定向
  • cat 1.txt 2>2.txt //错误重定向
  • cat 1.txt 2>>2.txt //错误追加重定向
  • 错误和正确都输出>+2> == &>
操作
root@localhost ~]# !w
wc -l 1.txt 
19 1.txt
[root@localhost ~]# cat 1.txt >2.txt   //重定向
[root@localhost ~]# wc -l 2.txt 
19 2.txt
[root@localhost ~]# 
[root@localhost ~]# cat 1.txt >> 2.txt 
[root@localhost ~]# cat 1.txt >> 2.txt 
[root@localhost ~]# cat 1.txt >> 2.txt 
[root@localhost ~]# cat 1.txt >> 2.txt   //追加重定向
[root@localhost ~]# wc -l 2.txt 
95 2.txt
[root@localhost ~]#

[root@localhost ~]# ls *.txt 123.sh >b.txt 2>a.txt
[root@localhost ~]# cat b.txt 
11.txt
123.sh
12.txt
1a.txt
1.txt
22.txt
2a.txt
2.txt
a.txt
b.txt
[root@localhost ~]# cat a.txt 
[root@localhost ~]# 

[root@localhost ~]# wc -l < 1.txt 
19
[root@localhost ~]# wc -l 1.txt 
19 1.txt
[root@localhost ~]# less -n2 < 1.txt 

你可能感兴趣的:(shell基础)