8.1 shell介绍
shell是一种命令解释器,用于接收用户指令与操作系统交互;
他有特定的语法:例如逻辑判断、循环语句等。
每个系统用户都有自己的shell,CentOS默认的Shell是 -bash(bourne Agin Shell)
系统还有zsh、ksh等。
Connecting to 192.168.87.130:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Last login: Fri Apr 20 00:00:10 2018 from 192.168.87.1
[root@lgs-01 ~]# shell
-bash: shell: 未找到命令
8.2 命令历史
linux系统会记录用户输入过的历史命令。
查看命令的历史记录:history 命令,默认存储1000条命令历史
[root@lgs-01 ~]# history
#节省部分显示
976 rm -rf /var/cache/yum/x86_64/7/base/packages/zziplib-0.13.62-5.el7.x86_64.rpm
977 ls -l /var/cache/yum/x86_64/7/base/packages/
978 yum reinstall -y zziplib --downloadonly
979 ls -l /var/cache/yum/x86_64/7/base/packages/
980 cd /usr/local/src/
981 ls
982 wget http://cn2.php.net/distributions/php-7.1.6.tar.bz2
983 ls -l php-7.1.6.tar.bz2
984 tar jxvf php-7.1.6.tar.bz2
985 ls
986 cd php-7.1.6
987 ls
988 ./configure --prefix=/usr/local/php7 --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php7/etc --with-pdo-mysql=/usr/local/mysql/ -with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
989 echo $?
990 make
991 echo $?
992 make install
993 echo $?
994 /usr/local/apache2.4/bin/apachectl -M
995 ls -l /usr/local/apache2.4/modules/libphp7.so
996 init 0
997 shell
998 history
history的历史是记录在 /root/.bash_history里:
[root@lgs-01 ~]# ls -l /root/.bash_history
-rw-------. 1 root root 23650 4月 20 01:11 /root/.bash_history
清除命令历史:history -c
默认记录1000条命令历史,可以修改配置文件/etc/profile里的HISTSIZE参数:
[root@lgs-01 ~]# echo $HISTSIZE
1000
[root@lgs-01 ~]# 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
}
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=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
# /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 ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
unset i
unset -f pathmunge
加入变量HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S",可以显示命令执行的时间
修改配置文件后,source /etc/profile 重新加载才生效
[root@lgs-01 ~]# 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.
#节省部分内容
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@lgs-01 ~]# source /etc/profile
[root@lgs-01 ~]# history
995 2018/04/20 13:33:01ls -l /usr/local/apache2.4/modules/libphp7.so
996 2018/04/20 13:33:01init 0
997 2018/04/20 14:20:48shell
998 2018/04/20 14:23:11history
999 2018/04/20 14:25:22historyls -l /etc/.bash_history
1000 2018/04/20 14:25:27ls -l /etc/.bash_history
1001 2018/04/20 14:25:46ls -l /root/.bash_history
1002 2018/04/20 14:27:11cat /root/.bash_history
1003 2018/04/20 14:27:41vim /root/.bash_history
1004 2018/04/20 14:31:30echo $HISTSIZE
1005 2018/04/20 14:32:03cat /etc/profile
1006 2018/04/20 14:35:16vim /etc/profile
1007 2018/04/20 14:36:43source /etc/profile
1008 2018/04/20 14:36:46ls
1009 2018/04/20 14:36:55history
执行上一条命令:!!
[root@lgs-01 ~]# !!
ls
123.zip b mesa-libGL-devel-17.0.1-6.20170307.el7.x86_64.rpm
1.txt.bak c mesa-libGLU-9.0.0-4.el7.x86_64.rpm
执行命令历史里的第几条命令:!n ,n是数字
1010 2018/04/20 14:39:18 cat /etc/profile
1011 2018/04/20 14:40:04 source /etc/profile
1012 2018/04/20 14:40:45 ls
1013 2018/04/20 14:41:54 history
[root@lgs-01 ~]# !1011
source /etc/profile
执行最近以某字符内容开头的命令:!echo
1004 2018/04/20 14:31:30 echo $HISTSIZE
1005 2018/04/20 14:32:03 cat /etc/profile
1006 2018/04/20 14:35:16 vim /etc/profile
1007 2018/04/20 14:36:43 source /etc/profile
1008 2018/04/20 14:36:46 ls
1009 2018/04/20 14:36:55 history
1010 2018/04/20 14:39:18 cat /etc/profile
1011 2018/04/20 14:40:04 source /etc/profile
1012 2018/04/20 14:40:45 ls
1013 2018/04/20 14:41:54 history
[root@lgs-01 ~]# !1011
source /etc/profile
[root@lgs-01 ~]# !echo
echo $HISTSIZE
1000
8.3 命令补全和别名
linux系统提供了命令输入的补全功能,提高用户输入命令的效率。
需要安装:bash-completion
当输入命令、文件或者目录的开头部分字符串的时候:按tab补全:
按一下tab:当开头字符串只有一个命令、文件或者目录匹配的时候,按一下tab即可补全
# [root@lgs-01 ~]# logo 此处按一下tab,即可补全命令 logout
[root@lgs-01 ~]# logout
按两下tab:当开头字符串不止一个命令、文件或者目录匹配的时候,按两下tab可以列出所有该开头字符串的命令、文件或者目录,然后再继续根据提示输入后续字符即可。
[root@lgs-01 ~]# log
logger login loginctl logname logout logrotate logsave
当一条命令很长的时候,还可以给命令起别名,提高输入效率
用alias命令:例如ls命令,是有带颜色显示选项的ls命令取别名而来
[root@lgs-01 ~]# which ls
alias ls='ls --color=auto'
/usr/bin/l
[root@lgs-01 ~]# alias restartnet='systemctl restart network.service '
[root@lgs-01 ~]# res
reset resize2fs resizecons resizepart restartnet restorecon
[root@lgs-01 ~]# restartnet
[root@lgs-01 ~]# which restartnet
alias restartnet='systemctl restart network.service '
/usr/bin/systemctl
查看系统所有的别名:
[root@lgs-01 ~]# 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'
在用户家目录下的/.bashrc 配置文件里定义了部分系统的alias别名,剩下的在
/etc/profile.d/ 目录下的脚本文件
[root@lgs-01 ~]# cat /root/.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
[root@lgs-01 ~]# ls /etc/profile.d/
256term.csh colorgrep.csh colorls.csh gnome-ssh-askpass.csh lang.csh less.csh vim.csh which2.csh
256term.sh colorgrep.sh colorls.sh gnome-ssh-askpass.sh lang.sh less.sh vim.sh which2.sh
8.4 命令的通配符
任意个字符或0个:*
[root@lgs-01 ~]# ls *.bak
1.txt.bak 2_hard.txt.bak 2.txt.bak
[root@lgs-01 ~]# ls 333*.tar
333.tar
单个字符:
[root@lgs-01 ~]# ls 33?.tar
333.tar
[ ] 中括号范围内的单个字符
[root@lgs-01 ~]# ls *.txt
k.txt zb.txt
[root@lgs-01 ~]# ls [a-zA-Z].txt
k.txt
[ ] 中括号里选取某单个字符
[root@lgs-01 ~]# ls *.txt
k.txt w.txt zb.txt
[root@lgs-01 ~]# ls [lkw].txt
k.txt w.txt
{ } 大括号里的一个字符段,以逗号分隔
[root@lgs-01 ~]# ls *.txt
abcd.txt ape.txt k.txt los.txt w.txt zb.txt
[root@lgs-01 ~]# ls {los,k,sss,ape}.txt
ls: 无法访问sss.txt: 没有那个文件或目录
ape.txt k.txt los.txt
8.5 输入输出重定向
输出重定向,覆盖原文件的内容: >
[root@lgs-01 ~]# echo 1234 >w.txt
[root@lgs-01 ~]# cat w.txt
1234
[root@lgs-01 ~]# echo 0008 >w.txt
[root@lgs-01 ~]# cat w.txt
0008
追加重定向,只追加内容,不覆盖:>>
[root@lgs-01 ~]# cat w.txt
0008
[root@lgs-01 ~]# echo 99999 >>w.txt
[root@lgs-01 ~]# cat w.txt
0008
99999
错误信息输出重定向: 2>
[root@lgs-01 ~]# laaaa
-bash: laaaa: 未找到命令
[root@lgs-01 ~]# laaaa 2>k.txt
[root@lgs-01 ~]# cat k.txt
-bash: laaaa: 未找到命令
错误信息追加重定向:2>>
[root@lgs-01 ~]# laaaa 2>k.txt
[root@lgs-01 ~]# cat k.txt
-bash: laaaa: 未找到命令
[root@lgs-01 ~]# kkkkk 2>>k.txt
[root@lgs-01 ~]# cat k.txt
-bash: laaaa: 未找到命令
-bash: kkkkk: 未找到命令
正确与错误输出重定向: &>
[root@lgs-01 ~]# ls *.txt 2>k.txt abcccc.txt &>k.txt
[root@lgs-01 ~]# cat k.txt
ls: 无法访问abcccc.txt: 没有那个文件或目录
abcd.txt
ape.txt
k.txt
los.txt
w.txt
zb.txt
也支持正确与错误追加重定向: &>>
正确与错误输出分开重定向:
[root@lgs-01 ~]# cat w.txt
0008
99999
[root@lgs-01 ~]# cat k.txt
111
[root@lgs-01 ~]# ls *.txt ppp.txt >w.txt 2>k.txt
[root@lgs-01 ~]# cat w.txt
abcd.txt
ape.txt
k.txt
los.txt
w.txt
zb.txt
[root@lgs-01 ~]# cat k.txt
ls: 无法访问ppp.txt: 没有那个文件或目录
输入重定向:< 把右边的文件内容 输入给 左边的命令,左边只能是命令
[root@lgs-01 ~]# cat w.txt
abcd.txt
ape.txt
k.txt
los.txt
w.txt
zb.txt
[root@lgs-01 ~]# wc -l <w.txt
6