8.6 管道符和作业控制
cat 1.txt |wc -l ; cat 1.txt |grep 'aaa'
ctrl z 暂停一个任务
jobs查看后台的任务 (只存在运行的终端中,无法查看其他终端,可以使用进程查看 )
[root@test ~]# ps aux|grep sleep
root 23327 0.0 0.0 107948 348 pts/0 S 11:34 0:00 sleep 1000
root 23345 0.0 0.0 107948 348 pts/0 S 11:36 0:00 sleep 1000000000000000000
root 23353 0.0 0.0 107948 348 ? S 11:37 0:00 sleep 60
root 23357 0.0 0.0 112720 980 pts/0 R+ 11:37 0:00 grep --color=auto sleep
bg[id]把任务调到后台
fg[id]把任务调到前台
命令后面加&直接丢到后台
管道符:文件输出内容传递给后面命令 grep过滤指定关键词
[root@test ~]# cat passwd |wc -l
22
统计文件:
[root@test ~]# ls |wc -l
14
[root@test ~]# find ./ -type f |wc -l
28
作业控制:
ctrl z 暂停一个任务
[root@test ~]# vim 32312.txt
[1]+ 已停止 vim 32312.txt
jobs查看后台的任务
[root@test ~]# jobs
[1]+ 已停止 vim 32312.txt
bg[id]把任务调到后台
[root@test ~]# bg
[1]+ vim 32312.txt &
[1]+ 已停止 vim 32312.txt
fg[id]把任务调到前台
显示系统状态
[root@test ~]# 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 3291676 2116 409192 0 0 1 0 20 16 0 0 100 0 0
0 0 0 3291560 2116 409224 0 0 0 0 84 63 0 0 100 0 0
0 0 0 3291560 2116 409224 0 0 0 0 82 64 0 0 100 0 0
0 0 0 3291560 2116 409224 0 0 0 0 86 64 0 0 100 0 0
0 0 0 3291560 2116 409224 0 0 0 0 76 56 0 0 100 0 0
[root@test ~]# sleep 1000
^Z
[1]+ 已停止 sleep 1000
[root@test ~]# bg (后台运行)
[1]+ sleep 1000 &
[root@test ~]# jobs
[1]+ 运行中 sleep 1000 &
将命令直接丢到后台运行 命令 &
[root@test ~]# sleep 1000000000000000000 &
[2] 23345
[root@test ~]# jobs
[1]- 运行中 sleep 1000 &
[2]+ 运行中 sleep 1000000000000000000 &
8.7/8.8 shell变量
PATH,HOME,PWD,LOGNAME (变量)
env命令 (获取到系统的变量)
set命令多了很多变量,并且包括用户自定义的变量
自定义变量a=1
变量名规则:字母、数字下划线,首位不能为数字
[root@test ~]# 1dasdas=1
bash: 1dasdas=1: 未找到命令...
变量值有特殊符号时需要用单引号括起来
[root@test ~]# a='a b c'
[root@test ~]# echo $a
a b c
[root@test ~]# a="a$bc"
[root@test ~]# echo $a
a
[root@test ~]# a='a$bc'
[root@test ~]# echo $a
a$bc
变量的累加
全局变量export b=2
unset变量
[root@test ~]# env(系统常用的环境变量)
[root@test ~]# set(查看系统内置变量,和用户自定义的变量)
自定义变量
[root@test ~]# a=21321321
[root@test ~]# echo $a
21321321
[root@test ~]# set |grep 21321321
a=21321321
[root@test ~]# set |less
变量累加
[root@test ~]# a=1
[root@test ~]# b=2
[root@test ~]# echo $a$b
12
[root@test ~]# a='a$bc'
[root@test ~]# echo $a$b
a$bc2
[root@test ~]# c="a$bc"
[root@test ~]# echo $c
a
[root@test ~]# c="a$b"c
[root@test ~]# echo $c
a2c
[root@test ~]# c='a$b'c
[root@test ~]# echo $c
a$bc
[root@test ~]# echo $c
a2c
[root@test ~]# c='a"$b"c'
[root@test ~]# echo $c
a"$b"c
全局变量
查看终端位置
[root@test ~]# w
12:17:22 up 23:41, 1 user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 115.236.28.52 11:09 2.00s 0.20s 0.00s w
[root@test ~]# echo $SSH_TTY
/dev/pts/0
进入子shell
[root@test ~]# bash
查看子shell位置
[root@test ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─VGAuthService
├─abrt-watch-log
├─abrtd
├─auditd─┬─audispd─┬─sedispatch
│ │ └─{audispd}
│ └─{auditd}
├─avahi-daemon───avahi-daemon
├─chronyd
├─crond
├─dbus-daemon
├─dnsmasq───dnsmasq
├─gssproxy───5*[{gssproxy}]
├─irqbalance
├─ksmtuned───sleep
├─libvirtd───16*[{libvirtd}]
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─packagekitd───2*[{packagekitd}]
├─polkitd───5*[{polkitd}]
├─rpcbind
├─rsyslogd───2*[{rsyslogd}]
├─run-initial-set───initial-setup-t───{initial-setup-t}
├─sshd───sshd───bash─┬─bash───pstree
│ └─sleep
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─tuned───4*[{tuned}]
└─vmtoolsd
退出子shell [root@test ~]# exit
全局变量生效 (只在子shell等下面生效,但是重新开个终端时不生效,并且只能往下生效,不能网上生效如子shell定义,孙shell生效,但是父shell不生效)
[root@test ~]# export ys=hkashdlasjd
[root@test ~]# echo $ys
hkashdlasjd
[root@test ~]# bash
[root@test ~]# echo $ys
hkashdlasjd
注:这里含义:export在写脚本时生成子脚本,脚本一牵涉到脚本二,脚本二可以使用脚本一里面变量
取消变量
[root@test ~]# unset ys
[root@test ~]# echo $ys
8.9 环境变量配置文件
/etc/profile 用户环境变量,交互,登录才执行 (系统维度)
/etc/bashrc 用户不用登录,执行shell就生效 (系统维度)
~/.bashrc (用户层次,用户家目录下)
~/.bash_profile (用户层次,用户家目录下)
profile和bashrc之间的区别profile登陆时自动加载,profile自动调用bashrc,bashrc是执行shell脚本时用户不用登陆就能执行里面的配置
~/.bash_history (存储命令历史)
~/.bash_logout (定义用户退出需要做的操作,每次用户退出就会自动执行)
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ ' (/etc/bashrc中定义)
[root@test ~]# vim ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc (这里的点表示[root@test ~]# source .bashrc 自动加载配置文件 )
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
注:~/.bash_profile会自动调用 ~/.bashrc
[root@test ~]# vim ~/.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 会自动调用 /etc/bashrc
[root@test ~]# vim /etc/bashrc
PS1表示:[root(用户名字)@test(主机名) ~(所在目录的最后层级)]#
[root@test ~]# echo $PS1
[\u@\h \W]\$
[root@test etc]# PS1='[\u@\h \w]\$'
[root@test /etc]#
[root@test /etc/ssh]#PS1='*\u@\h \w*\$'
*root@test /etc/ssh*#
*root@test /etc/ssh*#PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '
PS2模式如mysql进入到一个小终端中
root@test:/etc/ssh# for i in `seq 1 10`
> do
> echo $i
> done
1
2
3
4
5
6
7
8
9
10
root@test:/etc/ssh# for i in `seq 1 10`; do echo $i; done
1
2
3
4
5
6
7
8
9
10
root@test:/etc/ssh# PS2="%"
root@test:/etc/ssh# for i in `seq 1 10`
%do
%echo $i
%done
1
2
3
4
5
6
7
8
9
10
root@test:/etc/ssh# for i in `seq 1 10`; do echo $i; done