第九课预习任务

8.1 shell介绍
8.2 命令历史
8.3 命令补全和别名
8.4 通配符
8.5 输入输出重定向
8.6 管道符和作业控制
8.7/8.8 shell变量
8.9 环境变量配置文件
 

1.shell介绍

1.1shell脚本在日常linux系统管理中用得非常好,也是我们运维工程师一个必备技能。现在很多公司招聘这个也是这最基本的要求,这里只是介绍一下一些基础知识。

1.2shell是系统跟计算机硬件交互时全用的中间介质,它只是一个系统工具。它是一个命令解释器,并且支持特定的语法,每个用户都有自已特定的shell,centos默认的shell是bash.

2.命令历史

2.1我们执行过的命令都有记录下来,默认可以记录1000条历史命令。这些命令都会保存在用户家目录的.bash_history文件中。

//这里面记录了root的历史命令
[root@localhost ~]# ls -a
.  ..  anaconda-ks.cfg  .bash_history  .bash_logout  .bash_profile  .bashrc  .cshrc  .tcshrc
[root@localhost ~]# cat .bash_history
cd 
ls
cd ..
ls
cd /home
ls
ls -l
touch 11.txt
ls -l
chmod 750 11.txt
ls -l
useradd user1
chown user1 11.txtq
chown user1 11.txt
ls -l
useradd user2
passwd user2
passwd user1
su user2
chmod u+s 11.txt
cd ..
pwd
chmod u+s /home/11.txt

2.2我们可以在这个配置文件中修改这个参数/etc/history

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

2.3 我们可以用"history -c "清空历史命令,并不会去修改配置文件,我们可以看到以前的命令都没有了,只有刚刚执行的二条。2.4

[root@localhost ~]# history -c
[root@localhost ~]# history
    1  histroy
    2  history

2.4 这里有一个非常实用的变量,HISTTIMEFORMAT="%y:%m:%d  %h:%m:%s",如果想用永久生效,我们需要在

/etc/profile 配置文件中去写入这个变量

[root@localhost ~]# HISTTIMEFORMAT="%Y:%m:%d %H:%M:%S"
[root@localhost ~]# history
    1  2018:08:10 06:54:38histroy
    2  2018:08:10 06:54:45history
    3  2018:08:10 07:00:18HISTORYTIMEFORMAT="%y:%m:%d ?%h:%m:%s"
    4  2018:08:10 07:00:29echo HISTORYTIMEFORMAT
    5  2018:08:10 07:00:40echo $HISTORYTIMEFORMAT
    6  2018:08:10 07:00:55history
    7  2018:08:10 07:02:18HISTORYTIMEFORMAT="%Y:%m:%d %H:%M:%S"
    8  2018:08:10 07:02:20history
    9  2018:08:10 07:05:26HISTTIMEFORMAT="%Y:%m:%d %H:%M:%S"
   10  2018:08:10 07:05:27history

2.5如果我们不想自已的历史命令一直保存下来,我们可以“chattr +a ~/.bash_history”

2.6"!!"表示执行上一条命令

[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# !!
ls
anaconda-ks.cfg

2.7 “!n”这里的n是数字,表示执行历史的第n条命令

[root@localhost ~]# !11
ls
anaconda-ks.cfg

2.8 “!字符串”表示执行命令最近一次的命令

[root@localhost /]# ls home
11.txt  user  user1  user2
[root@localhost /]# !ls
ls home
11.txt  user  user1  user2

3.命令补全和别名

3.1 “tab”补全,这个命令非常实用。日常工作中使用度也非常高。敲一下和敲两下也有点不同,敲一下会帮我们补全一个指令、一个路径或者一个文件名,敲二下会把系统所有的命令或者文件名都列出来。

[root@localhost /]# ch
chacl       chardetect  chattr      chcpu       chgrp       chmem       chown       chronyc     chroot      chsh        
chage       chat        chcon       chfn        chkconfig   chmod       chpasswd    chronyd     chrt        chvt      

3.2 别名alias,它是bash功能之一。我们可以通过alisa把一个常用的并且很长的指令另取名为一个简单好记的指令。如果取消只要用unalias命令解除别名功能。

3.2.1查看系统中存在的别名

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

3.2.2我们也可以自定义命令的别名,格式:alias[命令的别名]=['具体的命令']我们这里把常用的‘cd’命令重名一下为‘c’

[root@localhost ~]# alias c='cd' //自定义别名
[root@localhost ~]# alias //系统里面就有了这个别名
alias c='cd'
root@localhost /]# c home //成功了
[root@localhost home]# 
[root@localhost ~]# cat .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

4.通配符

4.1在bash,我们可以使用*来匹配零个或多个字符,用?匹配一个字符。

[root@localhost tmp]# ls *.txt //可以匹配一个或多个字符
11.txt  1.txt  223.txt  2.txt
[root@localhost tmp]# ls ?.txt//只能匹配一个字符
1.txt  2.txt

4.2 还有“[]” “{}”这两个是集合的匹配

[root@localhost tmp]# ls [1-3].txt
1.txt  2.txt
[root@localhost tmp]# ls {1,3}.txt //匹配范围中的一个
ls: cannot access 3.txt: No such file or directory
1.txt

4.3 输入\输出重定向

4.3.1 输入重定向用于改变命令的输入,命令是<

[root@localhost tmp]# echo "11111" <1.txt
11111

4.3.2输出重定向用于改变命令的输出,命令是>,它经常用于将命令的结果输入到文件中。追加重定向,命令是>>

[root@localhost tmp]# echo "2222">1.txt
[root@localhost tmp]# cat 1.txt
2222
[root@localhost tmp]# echo "3333">>1.txt //不会删除原先存在数据
[root@localhost tmp]# cat 1.txt
2222
3333

4.3.3 错误重定向,命令为2>,追加错误重定向2>>

[root@localhost tmp]# ls 3.txt 2>1.txt //将前面的错误信息重定向到后面的文档中
[root@localhost tmp]# cat 1.txt
ls: cannot access 3.txt: No such file or directory
[root@localhost tmp]# cat 1.txt
ls: cannot access 3.txt: No such file or directory
[root@localhost tmp]# ls 33.txt 2>>1.txt
[root@localhost tmp]# cat 1.txt
ls: cannot access 3.txt: No such file or directory
ls: cannot access 33.txt: No such file or directory

5.管道符和作业控制

5.1 管道符“|”,它用于将前一个指令的输出作为后一个指令的输入

[root@localhost tmp]# ps aux | grep 'httpd'//查看进程中是否有httpd这个进程
root      95863  0.0  0.0 112704   956 pts/0    S+   07:52   0:00 grep --color=auto httpd

5.2 作业控制

5.2.1 当运行进程时,你可以使它暂停(ctrl+z),然后使用fg命令恢复它,或是利用bg命令使它到后台运行。还可以使它终止(ctrl+c)

[root@localhost tmp]# vi 1.txt

[1]+  Stopped                 vi 1.txt //ctrl+z我们可以看到这个vi命令已经终止
[root@localhost tmp]# fg //将命令调回来
vi 1.txt
ls: cannot access 3.txt: No such file or directory
ls: cannot access 33.txt: No such file or directory
                                                     
[root@localhost tmp]# bg //将该命令放到后台运行
[1]+ vi 1.txt &

5.2.2 我们可以使用‘jobs’查看被暂停或者在后台运行的任务

[root@localhost tmp]# jobs
[1]-  Stopped                 vi 1.txt
[2]+  Stopped                 vi 2.txt

5.2.3 使用&把任务放到后台运行

[root@localhost tmp]# vmstat &
[3] 95882
[root@localhost tmp]# procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 181160    268 681904    0    0    10    30   68   78  1  1 98  0  0
bg
[2]+ vi 2.txt &
[3]   Done                    vmstat

6 shell变量

6.1 shell预设的变量都是大写的。变量就是使用一个简单的字符串来替代某些具有特殊意义的设定以及数据。常用的有:PATH,HOME,PWD,LOGNAME.

6.2我们可以使用env查看系统预设的全部系统变量

[root@localhost tmp]# env
XDG_SESSION_ID=9
HOSTNAME=localhost.localdomain //表示主机的名称
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash //表示当前用户的shell类型
HISTSIZE=000 //表示历史记录数
SSH_CLIENT=192.168.1.100 50164 22
SELINUX_USE_CURRENT_RANGE=
OLDPWD=/
SSH_TTY=/dev/pts/0
USER=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
MAIL=/var/spool/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/tmp
LANG=en_US.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LOGNAME=root
SSH_CONNECTION=192.168.1.100 50164 192.168.1.150 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env

6.3还可以使用‘set’列出系统中的预设的环境变量

[root@localhost tmp]# set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="2" [2]="46" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.2.46(2)-release'
COLUMNS=136
DIRSTACK=()
EUID=0
GROUPS=()

6.4 前面都是系统变量,其实我们还可以自定义变量,定义变量的规则是

6.5 当变量内容带有特殊字符时,需要加上单引号

[root@localhost yum.repos.d]# a='knight lai' //这里我们加了一个特殊字符空格
[root@localhost yum.repos.d]# echo $a
knight lai

6.6变量的累加,需要加双引号

[root@localhost yum.repos.d]# c='a'b
[root@localhost yum.repos.d]# echo $c
ab
[root@localhost yum.repos.d]# c='$a'b
[root@localhost yum.repos.d]# echo $c
$ab
[root@localhost yum.repos.d]# c="$a"b //看到这里才可以正确累加
[root@localhost yum.repos.d]# echo $c
knightb

6.7 全局变量 export就是声明一下这个变量,让该shell的子shell也知道这个变量。

[root@localhost yum.repos.d]# export a=knight //声明全局变量
[root@localhost yum.repos.d]# echo $a
knight
[root@localhost yum.repos.d]# bash //打开他的子shell发现也可以生效
[root@localhost yum.repos.d]# echo $a
knight

6.8 取消变量 unset a

[root@localhost yum.repos.d]# unset a
[root@localhost yum.repos.d]# echo $a

7.环境变量配置文件

7.1 /etc/profile:用户环境变量,交互, 登录才执行。

这个文件预设了几个重要的变量如:PATH,USER,LOGNAME,MAIL,INPUTRC,HOSTNAM等 

[root@localhost ~]# cat /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
}

7.2/etc/bashrc:这个文件主要预设umask以及PS1.用户不用登录,执行shell就执行

[root@localhost ~]# cat /etc/bashrc
# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# 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.

# are we an interactive shell?
if [ "$PS1" ]; then
  if [ -z "$PROMPT_COMMAND" ]; then
    case $TERM in
    xterm*|vte*)
      if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
      elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then
          PROMPT_COMMAND="__vte_prompt_command"
      else
          PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
      fi
      ;;

7.3.bash_profile:这个文件定义了用户的个人化路径与环境变量的文件名称。

[root@localhost ~]# cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

7.4.bashrc:该文件属于自已的shell的bash信息,当登录或每次打开新的shell时,该文件会被读取。

[root@localhost ~]# cat .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

7.5.bash_history:该文件用于记录命令历史

7.6.bash_logout:当退出shell时,会执行该文件。可以将一些清理的工作放到这个文件夹中。

7.7 PS1:我们可以在这个配置文件下找到这个/etc/profile

 [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
其中\u 指用户\h指主机名 \w指当前目录,\$指定符(如果是普通用户则显示为¥)

猜你喜欢

转载自blog.csdn.net/a1779078902/article/details/81603828