管道符和作业控制 shell变量 环境变量配置文件

 

  • 8.6 管道符和作业控制
  • 8.7/8.8 shell变量
  • 8.9 环境变量配置文件

 

  • 管道符和作业控制

管道符:表示把一个文件的输出内容传送到后面的命令

 

grep  用来过滤指定关键词的命令

 

“|” 为管道符

 

 

ls  | wc  -l     统计当前路径下文件的个数

 

[root@localhost xiaobo]# ls

1.xtt  2.txt  3.txt

[root@localhost xiaobo]# ls |wc -l

3

[root@localhost xiaobo]# 

 

 

find  ./  -type  f  |wc -l  统计当前多少个文件

 

 

 

 

统计1.txt文本多少行

[root@localhost xiaobo]# cat 1.txt |wc -l

1

 

过滤1.txt指定的“aaa”字符串

[root@localhost xiaobo]# echo "bsahgdhasdj aaaasjdhsaaa hsdjhsjaaa" >1.txt

[root@localhost xiaobo]# cat 1.txt |grep 'aaa'

bsahgdhasdj aaaasjdhsaaa hsdjhsjaaa

ctrl  + z 暂停一个任务

[root@localhost xiaobo]# vim 1.txt

[1]+  已停止               vim 1.txt

然后再使用 fg (forground )调回来

编辑了两个文件,都丢到后台 ctrl + z

 

jobs 查看后台任务 , 调回来  fg  1   ;  fg   2

 

把任务搞到后台去

bg  2  也就是bg 加任务号

[root@localhost xiaobo]# jobs

[1]-  已停止               vim 1.txt

[2]+  已停止               vim 2.txt

[root@localhost xiaobo]# bg 2

[2]+ vim 2.txt &

[2]+  已停止               vim 2.txt

 

 

vmstat 显示当前的系统状态

vmstat 1 就是不停的动态显示当前的系统状态

[root@localhost xiaobo]# 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

 1  0      0 704592   2076 207516    0    0     5     1   13   13  0  0

 

ctrl  +  z  暂停

[root@localhost xiaobo]# jobs

[1]-  已停止               vim 1.txt

[2]+  已停止               vmstat 1

 

bg 可以把vmstat 搞到后台去。你再前台可以干别的事情。

 

搞到前台fg  然后退出vim   q! ;   然后ctrl  + c 退出当前任务

 

 

sleep 1000  把命令暂停1000s

 

ctrl + c 停止;

 

 

[root@localhost xiaobo]# sleep 1000

^Z

[1]+  已停止               sleep 1000

[root@localhost xiaobo]# sleep 200

^Z

[2]+  已停止               sleep 200

[root@localhost xiaobo]# jobs

[1]-  已停止               sleep 1000

[2]+  已停止               sleep 200

[root@localhost xiaobo]# bg 2

[2]+ sleep 200 &

[root@localhost xiaobo]# jobs

[1]+  已停止               sleep 1000

[2]-  运行中               sleep 200 &

 

 

直接丢到后台  加一个“&”号

sleep 100 &

 

 

总结: bg 后台;fg 前台; ctrl + z暂停一个任务;ctrl + c 结束任务;  命令后加一个+“&”号直接丢到后台

 

 

 

  •   shell变量

 

 

PATH,HOME,PWD,LOGNAME

 

env命令 获取系统变量

 

 

set命令多了很多变量,并且包括用户自定义的变量

 

显示环境变量

[root@localhost xiaobo]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

 

 

自定义变量

a=1

echo $a

输出 1

 

用户自定义的变量,可以在set  体现出来

set  |grep   111

[root@localhost xiaobo]# set |grep 111

_=111

a=111

 

set |less

a=111 可以查到自定义的变量

 

变量名规则: 字母数字下划线,首位不能为数字

 

 

变量值有特殊符号时需要用单引号括起来

a='a b c' 双引号也行 a="a b c"

echo  $a

单引号可以脱义

[root@localhost xiaobo]# a="a$bc"

[root@localhost xiaobo]# echo $a

a

[root@localhost xiaobo]# a='a$bc'

[root@localhost xiaobo]# echo $a

a$bc

[root@localhost xiaobo]# a=1

[root@localhost xiaobo]# b=2

[root@localhost xiaobo]# echo $a$b

12

[root@localhost xiaobo]# a='a$bc'

[root@localhost xiaobo]# echo $a$b

a$bc2

[root@localhost xiaobo]# c="a$bc"

[root@localhost xiaobo]# echo $c

a

$bc 没有定义

所有输出a

 

[root@localhost xiaobo]# c="a$b"c

[root@localhost xiaobo]# echo $c

a2c

 

 

c=a"$b"c

echo $c

a2c

 

 

 

全局变量

 

w查看系统运行情况(包括登录了几个用户)

[root@localhost xiaobo]# w

 06:48:08 up  6:44,  2 users,  load average: 0.00, 0.01, 0.05

USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT

root     pts/0    10.211.55.2      00:03    0.00s  0.42s  0.00s w

root     pts/1    10.211.55.2      06:46    1:17   0.02s  0.02s -bash

 

 

显示自己在哪个终端下:

[root@localhost xiaobo]# echo $SSH_TTY

/dev/pts/0

 

安装pstree

[root@localhost xiaobo]# yum provides "/*/pstree"

已加载插件:fastestmirror

Loading mirror speeds from cached hostfile

 * epel: mirror.ehost.vn

psmisc-22.20-15.el7.x86_64 : Utilities for managing processes on your system

源    :base

匹配来源:

文件名    :/usr/bin/pstree

[root@localhost xiaobo]# yum install -y psmisc-22.20-15.el7.x86_64 

 

pstree 列出进程树

 

[root@localhost xiaobo]# pstree

systemd─┬─NetworkManager───2*[{NetworkManager}]

        ├─agetty

        ├─auditd───{auditd}

        ├─chronyd

        ├─crond

        ├─dbus-daemon───{dbus-daemon}

        ├─firewalld───{firewalld}

        ├─irqbalance

        ├─master─┬─pickup

        │        └─qmgr

        ├─polkitd───5*[{polkitd}]

        ├─rsyslogd───2*[{rsyslogd}]

        ├─sshd─┬─sshd───bash───bash───pstree

        │      └─sshd───bash

        ├─systemd-journal

        ├─systemd-logind

        ├─systemd-udevd

        └─tuned───4*[{tuned}]

 

在第一个bash下输入

输出一个全局变量

export   xiaobo=linux   

 

输入bash 进入到一个子shell

bash --> bash--->pstree

 

子shell  echo xiaobo

linux

 

exit 退出子shell

 

 

[root@localhost xiaobo]# export xiaobo=linux

[root@localhost xiaobo]# echo $xiaobo

linux

[root@localhost xiaobo]# bash

[root@localhost xiaobo]# echo $xiaobo

linux

 

 

全局变量是在当前的shell 向下 子shell 、孙shell 定义全局变量。

取消变量定义:

unset   xiaobo

 

 

 

全局变量: export   xiaobo=123;

echo $xiaobo

123

  • 环境变量配置文件

系统层次:

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

/etc/bashrc 用户不用登录,执行shell就生效

 

系统环境变量不要修改,只修改用户的环境变量

 

用户层次:

家目录下的

~/.bashrc

~/.bash_profile

 

~/.bash_history  (用户命令历史)

~/.bash_logout  (定义用户退出要做的事情)

PS1='' ([root@localhost ~]#)

 

 

PS1 是在/etc/bashrc 里面定义的

vim /etc/bashrc

/PS1

 

[root@localhost ~]# echo $PS1

[u@h W]$

 

 

把路径改成绝对路径 把大写的W改成小写w

[root@localhost ~]# echo $PS1

[u@h W]$

[root@localhost ~]# PS1='[u@h w]$'

[root@localhost ~]#cd  xiaobo/

[root@localhost ~/xiaobo]#

 

 

改变颜色

PS1='[33[01;32m]u@h[33[00m]:[33[01;36m]w[33[00m]$ '

 

root@localhost:~/xiaobo#

 

 

写shell脚本,用PS2

root@localhost:~# echo $PS2

>

 

 

root@localhost:~# for i in `seq 1 10` 

> do

> echo $i

> done

1

2

3

4

5

6

7

8

9

10

 

 

 

总结:

PS1 是  用户名@主机名 路径

PS2是 >

 

  

 

原文地址:https://www.cnblogs.com/zhaocundang/p/8271677.html