第九课预习任务
第九課預習任務
1.shell介紹
2.命令歷史
3.命令補全和別名
4.通配符
5.管道符和作業控制
6 shell變量
7.環境變量配置文件
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.txt2.2我們可以在這個配置文件中修改這個參數/etc/history
HOSTNAME=`/usr/bin/hostname 2>/dev/null` HISTSIZE=1000 //在這里設置 if [ "$HISTCONTROL" = "ignorespace" ] ; thenexport HISTCONTROL=ignoreboth elseexport HISTCONTROL=ignoredups fiexport PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL2.3 我們可以用"history -c "清空歷史命令,并不會去修改配置文件,我們可以看到以前的命令都沒有了,只有剛剛執行的二條。2.4
[root@localhost ~]# history -c [root@localhost ~]# history1 histroy2 history2.4 這里有一個非常實用的變量,HISTTIMEFORMAT="%y:%m:%d ?%h:%m:%s",如果想用永久生效,我們需要在
/etc/profile 配置文件中去寫入這個變量
[root@localhost ~]# HISTTIMEFORMAT="%Y:%m:%d %H:%M:%S" [root@localhost ~]# history1 2018:08:10 06:54:38histroy2 2018:08:10 06:54:45history3 2018:08:10 07:00:18HISTORYTIMEFORMAT="%y:%m:%d ?%h:%m:%s"4 2018:08:10 07:00:29echo HISTORYTIMEFORMAT5 2018:08:10 07:00:40echo $HISTORYTIMEFORMAT6 2018:08:10 07:00:55history7 2018:08:10 07:02:18HISTORYTIMEFORMAT="%Y:%m:%d %H:%M:%S"8 2018:08:10 07:02:20history9 2018:08:10 07:05:26HISTTIMEFORMAT="%Y:%m:%d %H:%M:%S"10 2018:08:10 07:05:27history2.5如果我們不想自已的歷史命令一直保存下來,我們可以“chattr +a ~/.bash_history”
2.6"!!"表示執行上一條命令
[root@localhost ~]# ls anaconda-ks.cfg [root@localhost ~]# !! ls anaconda-ks.cfg2.7 “!n”這里的n是數字,表示執行歷史的第n條命令
[root@localhost ~]# !11 ls anaconda-ks.cfg2.8 “!字符串”表示執行命令最近一次的命令
[root@localhost /]# ls home 11.txt user user1 user2 [root@localhost /]# !ls ls home 11.txt user user1 user23.命令補全和別名
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 chvt3.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 functionsalias rm='rm -i' alias cp='cp -i' alias mv='mv -i'# Source global definitions if [ -f /etc/bashrc ]; then. /etc/bashrc fi4.通配符
4.1在bash,我們可以使用*來匹配零個或多個字符,用?匹配一個字符。
[root@localhost tmp]# ls *.txt //可以匹配一個或多個字符 11.txt 1.txt 223.txt 2.txt [root@localhost tmp]# ls ?.txt//只能匹配一個字符 1.txt 2.txt4.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.txt4.3 輸入\輸出重定向
4.3.1 輸入重定向用于改變命令的輸入,命令是<
[root@localhost tmp]# echo "11111" <1.txt 111114.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 33334.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 directory5.管道符和作業控制
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 httpd5.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.txt5.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 st2 0 0 181160 268 681904 0 0 10 30 68 78 1 1 98 0 0 bg [2]+ vi 2.txt & [3] Done vmstat6 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/env6.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 lai6.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 knightb6.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 knight6.8 取消變量 unset a
[root@localhost yum.repos.d]# unset a [root@localhost yum.repos.d]# echo $a7.環境變量配置文件
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" ] ; thenPATH=$PATH:$1elsePATH=$1:$PATHfiesac }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" ]; thenif [ -z "$PROMPT_COMMAND" ]; thencase $TERM inxterm*|vte*)if [ -e /etc/sysconfig/bash-prompt-xterm ]; thenPROMPT_COMMAND=/etc/sysconfig/bash-prompt-xtermelif [ "${VTE_VERSION:-0}" -ge 3405 ]; thenPROMPT_COMMAND="__vte_prompt_command"elsePROMPT_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 programsPATH=$PATH:$HOME/binexport PATH7.4.bashrc:該文件屬于自已的shell的bash信息,當登錄或每次打開新的shell時,該文件會被讀取。
[root@localhost ~]# cat .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 fi7.5.bash_history:該文件用于記錄命令歷史
7.6.bash_logout:當退出shell時,會執行該文件。可以將一些清理的工作放到這個文件夾中。
7.7 PS1:我們可以在這個配置文件下找到這個/etc/profile
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ " 其中\u 指用戶\h指主機名 \w指當前目錄,\$指定符(如果是普通用戶則顯示為¥)?
總結
 
                            
                        - 上一篇: 手机卫士day08
- 下一篇: 笔记本设置为无线路由器软件列表
