zl程序教程

您现在的位置是:首页 >  其他

当前栏目

linux基础(day24)

2023-04-18 16:52:58 时间

8.6 管道符和作业控制

管道符、作业控制

  • ctrl z //暂停一个任务
  • jobs //查看后台的任务
  • bg [id] //把任务调到后台
  • fg [id] //把任务调到前台
  • 命令后面加&直接丢到后台

管道符的使用

  • 管道符 | ,表示把前面命令输出的结果,传输给后面的命令
  • cat 1.txt |wc -l ;cat 1.txt |grep 'aaa'
    • grep 命令,用来过滤指定关键词的命令,只要在一行中含有这个关键词,就会把这一行过滤出来
    • wc -l 命令,查看文件有多少个
[root@localhost ~]# ls
111  123  1.txt  234  2.txt  2.txt.bak  3.txt  anaconda-ks.cfg
[root@localhost ~]# ls | wc -l    
8
  • find ./ -type f //在当前目录下,列出所有的文件
    • find ./ -type f |wc -l //计算当前目录下有多少个文件
[root@localhost ~]# find ./ -type f
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./.viminfo
./1.txt
./2.txt
./3.txt
./2.txt.bak
[root@localhost ~]# find ./ -type f |wc -l    计算当前目录下,有多少个文件
12

作业控制

  • ctrl+z快捷键,暂停一个任务
    • 若是正在编辑一个文件的时候
      • 可以ctrl+z临时暂停下这个服务(丢到后台去了),回到命令行界面,去操作其他的任务
      • fg 命令可以把丢在后台的命令,调回前台
    • 可以控制多个任务,将他们暂停掉
  • jobs 命令,可以把暂停的任务列出来
    • 暂停多个任务后,并会显示停止的任务列出来
[root@localhost ~]# vim 1.txt

[1]+  已停止               vim 1.txt
[root@localhost ~]# fg
vim 1.txt

[1]+  已停止               vim 1.txt
[root@localhost ~]# jobs
[1]+  已停止               vim 1.txt
[root@localhost ~]# vim 2.txt

[2]+  已停止               vim 2.txt
[root@localhost ~]# jobs
[1]-  已停止               vim 1.txt
[2]+  已停止               vim 2.txt
[root@localhost ~]# 
  • fg [id] 命令,把任务调到前台并执行——>不加id号就是执行最后一次的任务(加id就是指定任务)
    • 可以选择执行的任务
[root@localhost ~]# fg 1
  • bg [id] 命令,把任务调到后台并执行
[root@localhost ~]# bg 1
[1]+ vim 1.txt &

运行一条命令,可以将它丢到后台(前台)去运行 在结束任务的时候,必须是在前台才能结束——>(否则在后台是无法结束任务的)

  • sleep 1000 命令,暂停一千秒,什么事都不做,一千秒之后把命令窗口恢复回来
[root@localhost ~]# sleep 1000
^Z
[1]+  已停止               sleep 1000
[root@localhost ~]# jobs
[1]+  已停止               sleep 1000
[root@localhost ~]# sleep 200
^Z
[2]+  已停止               sleep 200
[root@localhost ~]# jobs
[1]-  已停止               sleep 1000
[2]+  已停止               sleep 200
[root@localhost ~]# fg
sleep 200
^Z
[2]+  已停止               sleep 200
在调到前后台运行的时候,不指定id号,就是默认最后一条执行命令
  • & 符号,把任务丢到后台去执行
[root@localhost ~]# sleep 100 &
[3] 2239
[root@localhost ~]# jobs
[1]   运行中               sleep 100 &
[root@localhost ~]# 

在打开另一终端,jobs命令,是查看不到执行当前终端的任务

但是在另一个终端,可以查看到进程ps aux |grep sleep

```
[root@localhost ~]# ps aux |grep sleep
root      2235  0.0  0.0 107892   624 pts/0    T    23:20   0:00 sleep 1000
root      2236  0.0  0.0 107892   620 pts/0    T    23:20   0:00 sleep 200
root      2264  0.0  0.0 112656   984 pts/1    R+   23:31   0:00 grep --color=auto slee
[root@localhost ~]# 
```

8.7/8.8 shell变量

变量

  • PATH,HOME,PWD,LOGNAME
  • env命令,来获取系统的变量
  • set命令多了很多变量,并且包括用户自定义的变量
  • 自定义变量a=1
  • 变量名规则:字母、数字下划线,首位不能为数字
  • 变量值有特殊符号时需要用单引号括起来
  • 变量的累加
  • 全局变量export b=2
    • 格式 export 变量名=变量值
    • 全局变量仅仅在子shell里面生效
      • 运行bash 命令,直接进去 子shell
  • unset变量 //取消变量

查看环境变量的命令

  • env命令,查看系统常用的环境变量
    • 系统的变量都是大写的英文字母,变量的值可以数字,字符串,英文字母等
  • set命令,查看系统内置的环境变量和用户自定义的变量
    • 在centos6中,显示一些环境变量出来或者是显示所有的变量
  • 自定义变量
    • 自定义的变量会在 set 中体现出来
    • set |grep 111 查找变量
[root@localhost ~]# a=111
[root@localhost ~]# echo $a
111
[root@localhost ~]# set |grep 111
_=111
a=111
[root@localhost ~]# 
  • set和env命令查看系统变量

变量名规则

  • 变量名规则:
    • 系统的环境变量是系统内置的(一般不会去更改)
    • 自定义的变量的名字:
      • 字母、数字下划线,首位不能为数字
[root@localhost ~]# 
[root@localhost ~]# a1=3
[root@localhost ~]# echo $a1
3
[root@localhost ~]# a_1=2
[root@localhost ~]# echo $a_1
2
[root@localhost ~]# _a1=4
[root@localhost ~]# echo $_a1
4
[root@localhost ~]# 1aa=2   变量名首位不能为数字
-bash: 1aa=2: 未找到命令
[root@localhost ~]# 

变量值规则

  • 变量值有特殊符号时需要用单引号括起来
[root@localhost ~]# a='a b c'
[root@localhost ~]# echo $a
a b c
[root@localhost ~]# a="a b c"
[root@localhost ~]# echo $a
a b c
这里可以使用 单引号'' 或 双引号"",但使用 单引号 更加好用——>方便脱义
  • 在以后赋值变量,存在特殊符号的时候,使用单引号
    • 否则在使用双引号的时候,赋予的值里面的特殊符号会有可能会系统当做标记
[root@hf-01 ~]# a="a$bc"        //会发现使用双引号,赋值变量得不到想要的结果
[root@hf-01 ~]# echo $a
a
[root@hf-01 ~]# a='a$bc'        //使用单引号会发现正常赋值
[root@hf-01 ~]# echo $a
a$bc

变量的累加

[root@hf-01 ~]# a=1
[root@hf-01 ~]# b=2
[root@hf-01 ~]# echo $a$b        //变量$a=1,变量$b=2
12
[root@hf-01 ~]# a='a$bc'
[root@hf-01 ~]# echo $a$b        //变量$a=a$bc,变量$b=2
a$bc2
[root@hf-01 ~]# c="a$bc"
[root@hf-01 ~]# echo $c        //变量$bc为赋值,所以为空,最后输出的值为a
a
[root@hf-01 ~]# c="a$b"c
[root@hf-01 ~]# echo $c        //变量$b=2,,所以输出为a2c
a2c
以下例子中,$bc为整体,而我又没有给它赋值,所以为空

当变量或表达式较为复杂的时候,变量叠加的时候,可以使用双引号将它们标记起来

全局变量

  • 全局变量 export b=2
  • w命令,用于显示已经登陆系统的用户列表,并显示用户正在执行的指令

非全局变量

    1. 首先打开两个终端, 终端1 和 终端2
    1. 使用w命令,可以看到有三个用户登录了系统
[root@hf-01 ~]# w
 05:34:28 up  4:05,  3 users,  load average: 0.00, 0.01, 0.05
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1      01:29    4:04m  0.03s  0.03s -bash
root     pts/0     01:30    4.00s  0.07s  0.03s w
root     pts/1     05:34    5.00s  0.02s  0.02s -bash
    1. 查看在哪个TTY下(终端下),执行环境变量
    • 在终端1 下执行命令 echo $SSH_TTY
[root@hf-01 ~]# echo $SSH_TTY        //当前是在/dev/pts/0下
/dev/pts/0
- 在终端2下执行命令 echo $SSH_TTY
[root@hf-01 ~]# echo $SSH_TTY
/dev/pts/1
    1. 在终端1 下定义一个变量,并去查看
[root@hf-01 ~]# hanfeng=linux
[root@hf-01 ~]# echo $hanfeng
linux
    1. 在终端2 下去查看终端1 定义的变量,会发现查看不到
[root@hf-01 ~]# echo $hanfeng        //会发现变量为 空

[root@hf-01 ~]# 
    1. 在终端1 下,在进入一个子shell
    • shell它是一个进程,打开一个shell,就相当于进入到了另一个终端,虽然还是在pts/1上,可以执行pstree 命令查看
[root@hf-01 ~]# bash
[root@hf-01 ~]# 
    1. 在终端1下,执行命令 pstree
[root@hf-01 ~]# pstree
systemd─┬─NetworkManager───3*[{NetworkManager}]
        ├─auditd───{auditd}
        ├─avahi-daemon───avahi-daemon
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─iprdump
        ├─iprinit
        ├─iprupdate
        ├─login───bash
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─mysqld_safe───mysqld───20*[{mysqld}]
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd───sshd─┬─bash───bash───pstree
        │             └─bash
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        └─tuned───4*[{tuned}]
[root@hf-01 ~]# 
    1. 在终端1下,这时再来执行echo $hanfeng,会发现变量未生效
[root@hf-01 ~]# echo $hanfeng

[root@hf-01 ~]# 
这是因为这个变量仅仅在上一个shell中
    1. 在终端1下,退出当前shell,执行命令 exit ,并在此执行pstree命令
[root@hf-01 ~]# exit
exit
[root@hf-01 ~]# pstree
systemd─┬─NetworkManager───3*[{NetworkManager}]
        ├─auditd───{auditd}
        ├─avahi-daemon───avahi-daemon
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─iprdump
        ├─iprinit
        ├─iprupdate
        ├─login───bash
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─mysqld_safe───mysqld───20*[{mysqld}]
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd───sshd─┬─bash───pstree
        │             └─bash
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        └─tuned───4*[{tuned}]
[root@hf-01 ~]# 
这时会看到缺少了一个bash,因为刚exit退出了一个bash
    1. 在终端1下,再去执行echo $hanfeng 会得到自定义的值
[root@hf-01 ~]# echo $hanfeng
linux

这种定义一个变量叫做非全局,或者叫做本地的变量(仅仅在你终端下生效)

全局变量

  • export hanfeng=linux //设置全局变量hanfeng=linux
    • 格式:export 变量名=变量值
  1. 在终端1下,设置
[root@hf-01 ~]# export hanfeng=linux
[root@hf-01 ~]# echo $hanfeng
linux
[root@hf-01 ~]# bash
[root@hf-01 ~]# echo $hanfeng
linux
[root@hf-01 ~]# 
全局环境变量,在终端1下,在打开shell之后,只要执行export 命令 ,在这下面所有的子shell 都会变量值,但在终端2下,变量依旧是不会生效

全局变量是向下的,在这个shell的基础上生成子shell,子子shell,子子子shell,而不会向上生效

取消变量,unset命令

  • unset命令,取消变量
    • 格式,unset 加变量名称
[root@hf-01 ~]# hanfeng=linux
[root@hf-01 ~]# echo $hanfeng
linux
[root@hf-01 ~]# unset hanfeng
[root@hf-01 ~]# echo $hanfeng

[root@hf-01 ~]# 

8.9 环境变量配置文件

环境变量配置文件目录概要

  • /etc/profile 用户环境变量,交互,登录才执行
  • /etc/bashrc 用户不能登录,执行shell就生效
  • ~/.bashrc
  • ~/.bash_history
  • ~/.bash_logout
  • PS1='[33[01;32m]u@h[33[00m]:[33[01;36m]w[33[00m]$ ' //带颜色显示命令行左边

系统的环境变量配置文件

  • 两个纬度,一个是系统层次,一个是用户层次
    • 系统层次,就是/etc 下的文件
    • 用户层次,就是用户家目录下的文件。每个用户的家目录下都会有以 . 点开头的隐藏文件.bash_profile或 .bashrc
  • 两种类型,把 bashrc 作为一种类型,把profile作为一种类型
    • 区别:
      • profile是用户登录的时候,就会自动的加载profile,profile又会自动的调用bashrc
      • bashrc是执行shell脚本的时候,用户不用登录,就可以直接执行shell脚本,执行shell脚本就会调用bashrc里面的一些配置 -系统中的 /etc/profile文件和 /etc/bashrc文件,一般不要去编辑它们
    • 在遇到一些需要的时候,可以编辑用户家目录下的.bash_profile
    • source .bash_profile 或 . .bash_profile 加载配置文件中的配置
  • ~/.bash_logout 文件,用来定义用户退出的时候需要做的一些操作
  • PS1是在/etc/bashrc中定义的
    • 在登录一个系统之后,他会在命令左边,有一串字符串 [root@hf-01 ~]
      • 最左侧是root,就是登陆用户的名字
      • @ 是主机名,hostname
      • 然后是你所在的目录最后一层级
[root@hf-01 ~]# echo $PS1
[u@h W]$
  • 切换到/etc/sysconfig/network-scripts/,并将W改成小写w,会发现变成绝对路径了
[root@hf-01 ~]# cd /etc/sysconfig/network-scripts/
[root@hf-01 network-scripts]# PS1='[u@h w]$'
[root@hf-01 /etc/sysconfig/network-scripts]#
[root@hf-01 ~]# cd 123/
[root@hf-01 ~/123]# cd /tmp/
[root@hf-01 /tmp]# 
这是一个全局路径
  • 也可以去除方括号[]
[root@hf-01 /tmp]# PS1='u@h w$'
root@hf-01 /tmp# 
  • 也可以修改成其他符号
root@hf-01 /tmp# PS1='<u@h w> $'
<root@hf-01 /tmp> # 
普通用户是 $号,root用户是# 号
  • 带颜色显示
PS1='[33[01;32m]u@h[33[00m]:[33[01;36m]w[33[00m]$ '

扩展