课后笔记

预习内容

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

 8.1 shell介绍

shell是系统跟计算机硬件交互时使用的中间介质,它只是系统的一个工具。实际上,在shell和计算机硬件之间还有一层东西--系统内核。用户直接面对的不是计算机硬件而是shell,用户把指令告诉shell,然后shell再传输给系统内核,接着内核在去支配计算机硬件去执行各种操作。

现在我们接触到的linux发布版本(Red Hat/CentOS)默认安装的shell版本是bash(即Bourne Again Shell),他是sh的增强版本。Bourne Shell是最早流行起来的一个shell版本。其创始人是Steven Bourne,为了纪念他而讲其命名为Bourne Shell,简称sh。

 8.2 命令历史

我们执行过的命令linux都会记录,预设可以记录1000条历史记录。这些命令保存在用户家目录的.bash_history文件中,需要注意的是,只有用户正常退出shell时,在当前shell中运行的命令才会保存到.bash_history文件中去。

  1. 我们cat一下.bash_history文件,可以看到我们之前用过的命令。
[root@localhost ~]# ls /root/.bash_history
/root/.bash_history
[root@localhost ~]# cat !$
cat /root/.bash_history
yum install -y net-tools
ntpdate time.windows.com
yum install -y ntpdate
ntpdate time.windows.com
date
tine
time
date
echo $PATH
yum install -y lrzsz
yum install -y expect
yum install -y lvm2
yum install -y vim-enhanced
yum install -y bzip2
yum install -y zip
yum install -y unzip
rpm -q nginx
mount /dev/cdrom /mnt/
ls /mnt/
cd /mnt/Packages
ls
rpm -ivh zsh-5.0.2-7.el7.x86_64.rpm
yum list
 cd
cp -r /etc/yum.repos.d /etc/yum.repos.d.bak
cd /etc/yum.repos.d.bak
cd /etc/yum.repos.d
ls
rm -rf ./*
vi dvd.repo
yum clean all
yum list
ls
rm -f dvd.repo
cp ../yum.repos.d.bak/* .
ls
rm -f CentOS-Base.repo
curl -O http://mirrors.163.com/.help/CentOS7-base-163.repo
ls
yum list
cat CentOS7-base-163.repo
vim CentOS7-base-163.repo
yum list
yum clean all
yum list
yun=m install CentOS-Base.repo
yun install CentOS-Base.repo
yum install CentOS-Base.repo
curl -O http://mirrors.163.com/.help/CentOS7-Base-163.repo
yum list
ls
rm -rf CentOS7-base-163.repo
ls
yum list
vim CentOS7-base-163.repo
vim CentOS7-Base-163.repo
yum install zlib
yum install zsh
yum list
yum install -y epel-release
ls
yum list
cd
cd /usr/local/src/
ls
wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.2.32.tar.gz
yum install wget
wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.2.32.tar.gz
wget http://archive.apache.org/dist/httpd/httpd-2.2.9.tar.gz
ls
tar -zxvf httpd-2.2.9.tar.gz
ls
cd httpd-2.2.9
ls
./configure --prefix=/usr/local/apache2
yum install gcc
./configure --prefix=/usr/local/apache2
echo $?
make
echo $?
make install
echo $?
ls /usr/local/aapache2
ls /usr/local/apache2
cd ..
yum search nginx
r]search all
search all
yum search all nginx
rpm -q nginx
cd
mkdir /data/yumdata
mkdir -p /data/yumdata
mount /dev/cdrom /mnt/
cp /mnt/Packages/*rpm /data/yumdata
cp /mnt/Packages/*rpm /data/yumdata/
rm -rf /data/yumdata/*rpm
ls /data/yumdata
ls /data/yumdata、
ls /data/yumdata/
ls
cp /mnt/Packages/*rpm /data/yumdata/
ls /data/yumdata/
createrepo /data/yumdata/
yum install createrepo
createrepo /data/yumdata/
ls /data/yumdata/
yum install -y epel-release
yum install -y nginx
vi /etc/nginx/nginx.conf
nginx -t
vi /etc/nginx/nginx.conf
nginx -t
vi /etc/nginx/nginx.conf
nginx -t
vi /etc/nginx/nginx.conf
nginx -t
vi /etc/nginx/nginx.conf
nginx -t
vi /etc/nginx/nginx.conf
nginx -t
vi /etc/nginx/nginx.conf
nginx -t
ls /etc/nginx/
vi /etc/nginx/nginx.conf
nginx -t
vi /etc/nginx/nginx.conf
nginx -t
vi /etc/nginx/nginx.conf
nginx -t
vi /etc/nginx/nginx.conf
nginx -t
vi /etc/nginx/nginx.conf
nginx -t
vi /etc/nginx/nginx.conf
nginx -t
vi /etc/nginx/nginx.conf
nginx -t
vi /etc/nginx/nginx.conf
nginx -t
systemctl start nginx
dhclient
ip addr
vi /etc/sysconfig/network-scripts/ifcfg-eno16777736 
systemctl restart network.service
ping www.qq.com
init 0
systemctl stop firewlld
systemc disable firewalld
systemctl disable firewalld
systemctl start nginx
systemctl disable firewalld
systemctl stop firewlld
systemctl start nginx
systemc disable firewalld
yum install -y systemc
vi /etc/nginx/nginx.conf
systemctl  stop firewalld
systemctl disable firewalld
systemctl start nginx
vi /etc/nginx/nginx.conf
systemctl start nginx
nginx -t
vi /etc/nginx/nginx.conf
nginx -t
vi /etc/nginx/nginx.conf
getenforce
setenforce 0
getenforce
systemctl start nginx
getenforce
setenforce 1
getenforce
setenforce 0
systemctl start nginx
setenforce 1
getenforce
setenforce 1
getenforce
setenforce 0
getenforce
yum list |grep zsh
yum list |grep ksh
yum install -y bash-completion
reboot
init 0

 

2.我们可以history看一下我的历史命令存了189条。

history
    1  yum install -y net-tools
    2  ntpdate time.windows.com
    3  yum install -y ntpdate
    4  ntpdate time.windows.com
    5  date
    6  tine
    7  time
    8  date
    9  echo $PATH
   10  yum install -y lrzsz
   11  yum install -y expect
   12  yum install -y lvm2
   13  yum install -y vim-enhanced
   14  yum install -y bzip2
   15  yum install -y zip
   16  yum install -y unzip
   17  rpm -q nginx
   18  mount /dev/cdrom /mnt/
   19  ls /mnt/
   20  cd /mnt/Packages
   21  ls
   22  rpm -ivh zsh-5.0.2-7.el7.x86_64.rpm
   23  yum list
   24   cd
   25  cp -r /etc/yum.repos.d /etc/yum.repos.d.bak
   26  cd /etc/yum.repos.d.bak
   27  cd /etc/yum.repos.d
   28  ls
   29  rm -rf ./*
   30  vi dvd.repo
   31  yum clean all
   32  yum list
   33  ls
   34  rm -f dvd.repo
   35  cp ../yum.repos.d.bak/* .
   36  ls
   37  rm -f CentOS-Base.repo
   38  curl -O http://mirrors.163.com/.help/CentOS7-base-163.repo
   39  ls
   40  yum list
   41  cat CentOS7-base-163.repo
   42  vim CentOS7-base-163.repo
   43  yum list
   44  yum clean all
   45  yum list
   46  yun=m install CentOS-Base.repo
   47  yun install CentOS-Base.repo
   48  yum install CentOS-Base.repo
   49  curl -O http://mirrors.163.com/.help/CentOS7-Base-163.repo
   50  yum list
   51  ls
   52  rm -rf CentOS7-base-163.repo
   53  ls
   54  yum list
   55  vim CentOS7-base-163.repo
   56  vim CentOS7-Base-163.repo
   57  yum install zlib
   58  yum install zsh
   59  yum list
   60  yum install -y epel-release
   61  ls
   62  yum list
   63  cd
   64  cd /usr/local/src/
   65  ls
   66  wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.2.32.tar.gz
   67  yum install wget
   68  wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.2.32.tar.gz
   69  wget http://archive.apache.org/dist/httpd/httpd-2.2.9.tar.gz
   70  ls
   71  tar -zxvf httpd-2.2.9.tar.gz
   72  ls
   73  cd httpd-2.2.9
   74  ls
   75  ./configure --prefix=/usr/local/apache2
   76  yum install gcc
   77  ./configure --prefix=/usr/local/apache2
   78  echo $?
   79  make
   80  echo $?
   81  make install
   82  echo $?
   83  ls /usr/local/aapache2
   84  ls /usr/local/apache2
   85  cd ..
   86  yum search nginx
   87  r]search all
   88  search all
   89  yum search all nginx
   90  rpm -q nginx
   91  cd
   92  mkdir /data/yumdata
   93  mkdir -p /data/yumdata
   94  mount /dev/cdrom /mnt/
   95  cp /mnt/Packages/*rpm /data/yumdata
   96  cp /mnt/Packages/*rpm /data/yumdata/
   97  rm -rf /data/yumdata/*rpm
   98  ls /data/yumdata
   99  ls /data/yumdata、
  100  ls /data/yumdata/
  101  ls
  102  cp /mnt/Packages/*rpm /data/yumdata/
  103  ls /data/yumdata/
  104  createrepo /data/yumdata/
  105  yum install createrepo
  106  createrepo /data/yumdata/
  107  ls /data/yumdata/
  108  yum install -y epel-release
  109  yum install -y nginx
  110  vi /etc/nginx/nginx.conf
  111  nginx -t
  112  vi /etc/nginx/nginx.conf
  113  nginx -t
  114  vi /etc/nginx/nginx.conf
  115  nginx -t
  116  vi /etc/nginx/nginx.conf
  117  nginx -t
  118  vi /etc/nginx/nginx.conf
  119  nginx -t
  120  vi /etc/nginx/nginx.conf
  121  nginx -t
  122  vi /etc/nginx/nginx.conf
  123  nginx -t
  124  ls /etc/nginx/
  125  vi /etc/nginx/nginx.conf
  126  nginx -t
  127  vi /etc/nginx/nginx.conf
  128  nginx -t
  129  vi /etc/nginx/nginx.conf
  130  nginx -t
  131  vi /etc/nginx/nginx.conf
  132  nginx -t
  133  vi /etc/nginx/nginx.conf
  134  nginx -t
  135  vi /etc/nginx/nginx.conf
  136  nginx -t
  137  vi /etc/nginx/nginx.conf
  138  nginx -t
  139  vi /etc/nginx/nginx.conf
  140  nginx -t
  141  systemctl start nginx
  142  dhclient
  143  ip addr
  144  vi /etc/sysconfig/network-scripts/ifcfg-eno16777736 
  145  systemctl restart network.service
  146  ping www.qq.com
  147  init 0
  148  systemctl stop firewlld
  149  systemc disable firewalld
  150  systemctl disable firewalld
  151  systemctl start nginx
  152  systemctl disable firewalld
  153  systemctl stop firewlld
  154  systemctl start nginx
  155  systemc disable firewalld
  156  yum install -y systemc
  157  vi /etc/nginx/nginx.conf
  158  systemctl  stop firewalld
  159  systemctl disable firewalld
  160  systemctl start nginx
  161  vi /etc/nginx/nginx.conf
  162  systemctl start nginx
  163  nginx -t
  164  vi /etc/nginx/nginx.conf
  165  nginx -t
  166  vi /etc/nginx/nginx.conf
  167  getenforce
  168  setenforce 0
  169  getenforce
  170  systemctl start nginx
  171  getenforce
  172  setenforce 1
  173  getenforce
  174  setenforce 0
  175  systemctl start nginx
  176  setenforce 1
  177  getenforce
  178  setenforce 1
  179  getenforce
  180  setenforce 0
  181  getenforce
  182  yum list |grep zsh
  183  yum list |grep ksh
  184  yum install -y bash-completion
  185  reboot
  186  init 0
  187  ls /root/.bash_history
  188  cat /root/.bash_history
  189  history

 

 3.我们之前说过history记录的1000条命令是谁决定的呢?是环境变量echo $HISTSIZE决定的。

[root@localhost ~]# echo $HISTSIZE
1000

 

4.history -c 选项是清空当前内存中的命令历史记录。他不能清空环境变量的配置文件。

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


[root@localhost ~]# cat .bash_history 
yum install -y net-tools
ntpdate time.windows.com
yum install -y ntpdate
ntpdate time.windows.com
date
tine
time
date
echo $PATH
yum install -y lrzsz
yum install -y expect
yum install -y lvm2

 

5.我们也可以修改命令历史最大的储存记录,进入到 /etc/profile当中去修改。

[root@localhost ~]# vi /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=`id -u`
        UID=`id -ru`
    fi
    USER="`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                                 我们可以看到HISTSIZE后面决定的是记录1000条命令,可以更改5000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups

修改完之后想要成功需要重启一下/etc/profile 然后我们在使用命令echo $HISTSIZE 看一下。

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

[root@localhost ~]# echo $HISTSIZE
5000

 

 6.我们也可以把history命令显示的内容带时间日期的格式,首先赋予一个HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S",然后我们echo $HISTTIMEFORMAT,然后我们在看一下效果

[root@localhost ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@localhost ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@localhost ~]# history
    1  2018/05/31 19:02:13history
    2  2018/05/31 19:02:47cat .bash_history 
    3  2018/05/31 19:05:38vi /etc/profile
    4  2018/05/31 19:07:57history
    5  2018/05/31 19:08:29source /etc/profile
    6  2018/05/31 19:09:47echo $HISTSIZE
    7  2018/05/31 19:14:13HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
    8  2018/05/31 19:14:23echo $HISTTIMEFORMAT
    9  2018/05/31 19:14:30history

 

7.还有一个用法就是我们让我们的命令历史记录永久保存,可以给加a权限。只能追加不能删除。

[root@localhost ~]# chattr +a ~/.bash_history 

 

8.!是与命令历史有关的一个特殊字符,该字符常用的应用有以下3个。

!!:连续二个!表示执行上一条命名。

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

!n:这里的n是数字,表示执行命令历史的第n条指令。

  219  ls
  220  lm
  221  la
  222  ls -la
  223  ls
  224  nginx -t
  225  history 
[root@localhost ~]# !219
ls
anaconda-ks.cfg
[root@localhost ~]# 

!字符串:(字符串大于等于1):例如!pw表示执行命令历史中最近一次以pw开头的命令:

[root@localhost ~]# !yum
yum install ntpdate
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * epel: mirrors.tongji.edu.cn
软件包 ntpdate-4.2.6p5-28.el7.centos.x86_64 已安装并且是最新版本
无须任何处理

!echo 表示倒着找离echo最近的命令

8.3 命令补全和别名

1. 按tab键可以帮我们补全一个命令,一个路径或者一个文件名。连续按二次tab键,系统则会把所有的命令或者文件名都列出来。

2.在之前我们提过alias,它是bash特有的功能之一,我们可以通过alias把一个常用的并且很长的指令另取名为一个简单易记得指令。如果不想用了,还可以使用unalisa解除别名功能,直接执行alias,会看到系统下预设的别名。

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

另外我们还可以自定义命令的别名。示例:

[root@localhost ~]# alias hongwei='pwd'
[root@localhost ~]# hongwei
/root
[root@localhost ~]# unalias hongwei
[root@localhost ~]# hongwei
-bash: hongwei: 未找到命令

我们还可以进入到alias配置文件,可以自行定义更改别名

[root@localhost ~]# 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
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
".bashrc" 12L, 176C

取消别名用  unalias

 8.4 通配符

*符号表示通配。

我们使用*.txt来查找ls下所有的txt的文件,还可以使用*txt,*txt*都是可以的

[root@localhost ~]# ls *.txt
2.txt  3.txt  4.txt

我们也可以使用s*,就是找出所有s开头的文件或者目录等等

[root@localhost etc]# ls s*
securetty      shadow   statetab   sudo-ldap.conf  system-release-cpe
services       shadow-  sudo.conf  sysctl.conf
sestatus.conf  shells   sudoers    system-release

sasl2:
smtpd.conf

security:
access.conf   console.handlers  group.conf   namespace.conf  opasswd         sepermit.conf
chroot.conf   console.perms     limits.conf  namespace.d     pam_env.conf    time.conf
console.apps  console.perms.d   limits.d     namespace.init  pwquality.conf

selinux:
config  semanage.conf  targeted

skel:

ssh:
moduli      sshd_config         ssh_host_ecdsa_key.pub  ssh_host_rsa_key.pub
ssh_config  ssh_host_ecdsa_key  ssh_host_rsa_key

ssl:
certs

statetab.d:

sudoers.d:

sysconfig:
authconfig  ebtables-config   iptables-config  modules          rdisc          sshd
cbq         firewalld         irqbalance       netconsole       readonly-root  wpa_supplicant
console     grub              kdump            network          rsyslog
cpupower    init              kernel           network-scripts  run-parts
crond       ip6tables-config  man-db           ntpdate          selinux

sysctl.d:
99-sysctl.conf

systemd:
bootchart.conf  journald.conf  logind.conf  system  system.conf  user  user.conf

我们也可以使用?.txt,可以是数字或者字母。使用二个??也可以查找二个字母或者数字开头的文件

[root@localhost ~]# touch w.txt
[root@localhost ~]# touch 55.txt
[root@localhost ~]# touch sb.txt
[root@localhost ~]# ls
2.txt  3.txt  4.txt  55.txt  anaconda-ks.cfg  sb.txt  w.txt
[root@localhost ~]# ls ?.txt
2.txt  3.txt  4.txt  w.txt
[root@localhost ~]# ls ??.txt
55.txt  sb.txt

也可以使用[]来定义列出范围。括号里可以是数字0-9也可以是字母a-z都可以。也可以指定12.aw等。

[root@localhost ~]# ls [0-6].txt
2.txt  3.txt  4.txt
[root@localhost ~]# ls [a-z].txt
w.txt
[root@localhost ~]# 

[root@localhost ~]# ls [13].txt
3.txt
[root@localhost ~]# ls [wa].txt
w.txt

[root@localhost ~]# ls [0-9a-z].txt
2.txt  3.txt  4.txt  w.txt

我们还可以使用{}花括号,但是花括号里用,隔开

[root@localhost ~]# ls {1,2,3,a,w}.txt
ls: 无法访问1.txt: 没有那个文件或目录
ls: 无法访问a.txt: 没有那个文件或目录
2.txt  3.txt  w.txt

 

 8.5 输入输出重定向

 

>表示输出重定向

[root@localhost ~]# echo 1111 > 2.txt
[root@localhost ~]# cat 2.txt
1111

<表示输入重定向    输入前面必须是一个命令

[root@localhost ~]# aa < 2.txt
-bash: aa: 未找到命令
[root@localhost ~]# cat < 2.txt
1111

还有错误的命令2>,就是把错误的信息也输入到文件里面去,示例:

[root@localhost ~]# ls qw.txt 2> 2.txt
[root@localhost ~]# cat 2.txt
ls: 无法访问qw.txt: 没有那个文件或目录

追加重定向>> 

[root@localhost ~]# laaa 2>> 2.txt
[root@localhost ~]# cat 2.txt
ls: 无法访问qw.txt: 没有那个文件或目录
-bash: laaa: 未找到命令

>+2> 就等于&>  这一条命令相当于把正确和错误的命令都输入到3.txt中去

[root@localhost ~]# ls [2-3].txt 456.txt &> 3.txt
[root@localhost ~]# cat 3.txt
ls: 无法访问456.txt: 没有那个文件或目录
2.txt
3.txt

我们也可以使用下面命令把正确和错误的输出给区分开

[root@localhost ~]# ls [2-3].txt 456.txt > 3.txt 2>4.txt
[root@localhost ~]# cat 3.txt
2.txt
3.txt
[root@localhost ~]# cat 4.txt
ls: 无法访问456.txt: 没有那个文件或目录
[root@localhost ~]# 

 

 8.6 管道符和作业控制

管道符

之前我们用过很多| 符号,这是管道符号,它用于将前一个指令的输出作为后一个指令的输入,例如:

[root@localhost ~]# cat /etc/passwd |wc -l
21
[root@localhost ~]# cat /etc/passwd |grep bin
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
nginx:x:998:997:Nginx web server:/var/lib/nginx:/sbin/nologin

 

作业控制

  • 当运行进程时,可以使用快捷键 ctrl + z 使它暂停,然后使用fg命令恢复,或者使用bg命令使它到后台运行,示例:
[root@localhost ~]# vim 2.txt

[1]+  已停止               vim 2.txt

我们比如想vim 2.txt,然后按键ctrl +z 会停止当前的进程,如上例子。

  • 此时我们按下fg恢复到进程中
[root@localhost ~]# fg
vim 2.txt

ls: 无法访问qw.txt: 没有那个文件或目录
-bash: laaa: 未找到命令
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
                                                                             1,1          全部
  • 使用命令bg把进程到后台去
[root@localhost ~]# bg
[1]+ vim 2.txt &
  • 我们也可以使用jobs 查看当前有多少后台的任务在运行
[root@localhost ~]# vim 2.txt

[1]+  已停止               vim 2.txt
[root@localhost ~]# bg
[1]+ vim 2.txt &
[root@localhost ~]# vi 4.txt

[1]-  已停止               vim 2.txt

[2]+  已停止               vi 4.txt
[root@localhost ~]# fg
vi 4.txt

[2]+  已停止               vi 4.txt
[root@localhost ~]# bg
[2]+ vi 4.txt &
[root@localhost ~]# jobs
[1]-  已停止               vim 2.txt
[2]+  已停止               vi 4.txt
  • 上面例子中,我们出现了二个后台的任务,想要把某一个任务弄到后天去,需要使用fg bg 后面加上id号,示例
[root@localhost ~]# fg 1
vim 2.txt

ls: 无法访问qw.txt: 没有那个文件或目录
-bash: laaa: 未找到命令
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
~                                                                                              
                                                                             1,1          全部
[root@localhost ~]# bg 1
[1]+ vim 2.txt &
[root@localhost ~]# bg 2
[2]- vi 4.txt &

[1]+  已停止               vim 2.txt
[root@localhost ~]# jobs
[1]-  已停止               vim 2.txt
[2]+  已停止               vi 4.txt
  • 例子:
[root@localhost ~]# vmstat 1
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 686212    692 178436    0    0    14     1   17   20  0  0 100  0  0
 0  0      0 686064    692 178468    0    0     0     0   21   21  0  1 99  0  0
 0  0      0 686064    692 178468    0    0     0     0   24   19  0  0 100  0  0
 0  0      0 686064    692 178468    0    0     0     0   18   17  0  0 100  0  0
 0  0      0 686064    692 178468    0    0     0     0   14   12  0  0 100  0  0
 0  0      0 686064    692 178468    0    0     0     0   18   17  0  0 100  0  0
 0  0      0 686064    692 178468    0    0     0     0   16   12  0  1 99  0  0
 0  0      0 686064    692 178468    0    0     0     0   14   15  0  0 100  0  0
 0  0      0 686064    692 178468    0    0     0     0   14   14  0  0 100  0  0
^C

我们使用vmstat 可以观察一个系统状态的命令。

  • 还有一个命令就是sleep 后面加数字代表秒数 就是暂停某一个进程多少秒 sleep 200 就是暂停200秒的意思。 

 8.7/8.8 shell变量

我们之前接触过环境变量PATH,它是shell预设的一个变量。通常,shell预设的变量都是大写的。变量就是使用一个较简单的字符串来替代某些具有特殊意义的设定以及数据。就拿PATH来讲,这个PATH就代替了所有常用命令的绝对路径的设定,有了PATH这个变量,我们运行某个命令时,就不再需要输入全局路径,直接输入命令名即可。我们可以使用echo命令显示变量的值,如下:

[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# echo $HOME
/root
[root@localhost ~]# echo $PWD
/root
[root@localhost ~]# echo $LOGNAME
root

除了PATH HOME PWD LOGNAME 系统的变量还有很多。

1.命令env

使用env命令,可列出系统预设的全部系统变量,如下:

[root@localhost ~]# env
XDG_SESSION_ID=2
HOSTNAME=localhost.localdomain
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=5000
SSH_CLIENT=192.168.73.1 52440 22
SELINUX_USE_CURRENT_RANGE=
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=/root
LANG=zh_CN.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LOGNAME=root
SSH_CONNECTION=192.168.73.1 52440 192.168.73.128 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env

登录不同的用户,这些环境变量的值也不同,当前显示的是root账户的环境变量,下面介绍一下常用的简单变量:

  • HOETNAME:表示主机的名称
  • SHELL:表示当前用户的shell类型
  • HISTSIZE:表示历史记录数
  • MAIL:表示当前用户的邮件存放数量
  • PATH:该变量决定了shell将到哪些目录中寻找命令或程序
  • PWD:表示当前目录
  • LANG:这是与语言相关的环境变量,多语言环境可以修改此环境变量
  • HOME:表示当前用户的家目录
  • LOGNAME:表示当前用户的登录名

env命令显示的变量只是环境变量,系统预设的变量还有很多,我们可以使用set命令查看

2.命令set

set命令和env命令类似,也可以输出环境变量,如下:

[root@localhost ~]# set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_COMPLETION_COMPAT_DIR=/etc/bash_completion.d
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="2" [2]="45" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.2.45(1)-release'
COLUMNS=95
DIRSTACK=()
EUID=0
GROUPS=()
HISTCONTROL=ignoredups
HISTFILE=/root/.bash_history
HISTFILESIZE=5000
HISTSIZE=5000
HOME=/root
HOSTNAME=localhost.localdomain
HOSTTYPE=x86_64
ID=0
IFS=$' \t\n'
LANG=zh_CN.UTF-8
LESSOPEN='||/usr/bin/lesspipe.sh %s'
LINES=18
LOGNAME=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:'
MACHTYPE=x86_64-redhat-linux-gnu
MAIL=/var/spool/mail/root
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=1794
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/root
SELINUX_LEVEL_REQUESTED=
SELINUX_ROLE_REQUESTED=

set命令不仅可以显示系统预设的变量,也可以显示用户自定义的变量,我们举例自定义一个变量。

[root@localhost ~]# myname=liuhongwei
[root@localhost ~]# echo $myname
liuhongwei

虽然我们可以自定义变量,但是该变量只能在当前shell中生效。

[root@localhost ~]# echo $myname
liuhongwei
[root@localhost ~]# bash
[root@localhost ~]# echo $myname

上面例子中,我们使用bash命令可以再打开一个shell,此时先前设置的myname变量已经不存在了,退出当前shell回到原来的shell,myname变量还在,如果想要此设置的环境变量一直生效,应该怎么做,分二种情况

  • 允许系统内所有用户登录后都能使用该便变量,具体的操作方法是:在/etc/profile文件的最后一行加入export myname=liuhongwei,然后运行source /etc/profile就可以生效了,此时再运行bash命令或者切换到其他用户,就可以看到效果了。
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
export myname=liuhongwei
-- 插入 --  
[root@localhost ~]# vim /etc/profile
[root@localhost ~]# source !$
source /etc/profile
[root@localhost ~]# source /etc/profile
[root@localhost ~]# bash 
[root@localhost ~]# echo $myname
liuhongwei
  • 仅允许当前用户使用该变量。具体的操作方法。在用户主目录下的.bsahrc文件的最后一行加入export myname=liuhongwei,然后运行source .bashrc就可以生效了。这时在切换myname变量则不会生效了。这里source命令的作用是将目前设定的配额刷新,既不在注销也能生效。

但是在linux下设置自定义变量,有一些规则。

  • 设定变量的格式为a=b,其中a为变量名,b为变量的内容,等号二边不能有空格
  • 变量名只能由字母、数字以及下划线组成,而且不能以数字开头
  • 当变量内容带有特殊符号时,需要加上单引号,
[root@localhost ~]# myname='liu hongwei'
[root@localhost ~]# echo $myname
liu hongwei

有一种情况需要注意,就是变量内容中本身带有单引号,这时需要加双引号了。举例

[root@localhost ~]# myname="liu's"
[root@localhost ~]# echo $myname
liu's

如果变量内容中需要用到其他命令,运行结果则可以使用反引号,如下:

[root@localhost ~]# myname=`pwd`
[root@localhost ~]# echo $myname
/root

变量内容可以累加其他变量的内容,但需要加双引号。示例:

[root@localhost ~]# myname="$LOGNAME"liuhongwei
[root@localhost ~]# echo $myname
rootliuhongwei

如果不小心把双引号错加为单引号,则得不到你想要的结果

[root@localhost ~]# myname='$LOGNAME'liuhongwei
[root@localhost ~]# echo $myname
$LOGNAMEliuhongwei

通过上面的几个例子可以看出,单引号与双引号的区别。使用双引号时,不会取消双引号中特殊字符本身的作用,而使用单引号时,里面的特殊字符将全部失去其本身的作用。

在上面的例子中,多次使用了bash命令,如果在当前shell中运行bash命令,则会进入一个新的shell,这个shell就是原来shell的子shell,我们可以用pstree来查看一下,没有pstree命令使用 yum install psmisc 命令安装

[root@localhost ~]# yum install psmisc
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * epel: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 psmisc.x86_64.0.22.20-15.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

===============================================================================================
 Package              架构                 版本                       源                  大小
===============================================================================================
正在安装:
 psmisc               x86_64               22.20-15.el7               base               141 k

事务概要
===============================================================================================
安装  1 软件包

总下载量:141 k
安装大小:475 k
Is this ok [y/d/N]: y
Downloading packages:
psmisc-22.20-15.el7.x86_64.rpm                                          | 141 kB  00:00:06     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : psmisc-22.20-15.el7.x86_64                                                 1/1 
  验证中      : psmisc-22.20-15.el7.x86_64                                                 1/1 

已安装:
  psmisc.x86_64 0:22.20-15.el7                                                                 

完毕!
[root@localhost ~]# pstree |grep bash
        |-login---bash
        |-sshd---sshd---bash---bash-+-grep
[root@localhost ~]# bash
[root@localhost ~]# pstree |grep bash
        |-login---bash
        |-sshd---sshd---bash---bash---bash-+-grep

可以看到我们bash一下就多了一个bash

 8.9 环境变量配置文件扩展

看一下系统环境变量和配置文件:

  • /etc/profile:这个文件预设了几个重要的变量,例如PATH USER LOGNAME MAIL INPUTRC HOSTNAME HISTSIZE umask 
  • /etc/bashrc:这个文件主要预设umask以及ps1,这个ps1就是我们在输入命令时前面的那串字符。

以上是二个系统级别的配置文件外,每个用户的主目录下还有几个隐藏文件

  • .bash_profile:该文件定义了用户的个人化路径与环境变量的文件名称。每个用户都可以使用该文件输入专属于自己的shell信息,当用户登录时,该文件仅仅执行一次
  • .bashrc:该文件包含专属于自己的shell的bash信息,当登录或每次打开新的shell时,该文件会被读取。
  • .bash_history:该文件用于记录命令历史
  • .bash_logout:当退出shell时,会执行该文件。

 

转载于:https://my.oschina.net/u/3851487/blog/1822119

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值