,变量,环境配置,特殊符号,cut, sort, tee, split,&&, ||

变量

  • 常用的变量:PATH(环境变量)、HOME、PWD、LOGNAME
  • env命令 - 查看环境变量,系统使用的环境变量;
  • set命令 - 除了系统的环境变量,还有用户自定义的变量;(自定义用户只能用set查出)
[root@tanyvlinux ~]# a=111
[root@tanyvlinux ~]# echo $a
111
[root@tanyvlinux ~]# set |grep 111
_=111
a=111
  • 变量名规则:可以用字母、数字和下划线组成,首位不能为数字;
[root@tanyvlinux ~]# a1=2
[root@tanyvlinux ~]# echo $a1
2
[root@tanyvlinux ~]# _1=45
[root@tanyvlinux ~]# echo $_1
45
[root@tanyvlinux ~]# 1a=33
bash: 1a=33: 未找到命令...
  • 变量的值
    含特殊符号要用单引号括起来,脱义;
    双引号不脱引,要引用另一个变量用双引号;
[root@tanyvlinux ~]# a='a b $c'
[root@tanyvlinux ~]# echo $a
a b $c
[root@tanyvlinux ~]# c=3
[root@tanyvlinux ~]# a="a b $c"
[root@tanyvlinux ~]# echo $a
a b 3
  • 变量的累加 - 变量的值是不带空格的
[root@tanyvlinux ~]# echo $a$c		# 接上;
a b 33
[root@tanyvlinux ~]# d=$ab$c		#$ab是一个变量,没有值;
[root@tanyvlinux ~]# echo $d
3
[root@tanyvlinux ~]# d="$a"b$c		#用双引号把变量和字母分开;
[root@tanyvlinux ~]# echo $d
a b 3b3
  • 变量只能在当前shell使用,要在子shell使用需要定义全局变量
[root@tanyvlinux ~]# bash			#接上,新建子shell;
[root@tanyvlinux ~]# echo $d			#变量不起作用;

[root@tanyvlinux ~]# exit					#退出子shell;
exit
[root@tanyvlinux ~]# export d=$c'$a'b		#定义全局变量;	
[root@tanyvlinux ~]# echo $d
3$ab
[root@tanyvlinux ~]# bash						#新建子shell;
[root@tanyvlinux ~]# echo $d					#变量起作用,没有定义全局变量$c,$c起作用;
3$ab
  • 其他独立的shell不能使用一个shell定义的变量和全局变量
TanydeMacBook-Air:~ tanytan$ ssh root@192.168.31.128
Last login: Tue Sep 10 17:15:37 2019 from 192.168.31.101
[root@tanyvlinux ~]# w
 23:45:01 up 1 day, 13:33,  3 users,  load average: 0.00, 0.02, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      六20    6:29m  0.27s  0.27s -bash
root     pts/0    192.168.31.101   23:44    5.00s  0.03s  0.00s w
root     pts/1    192.168.31.101   17:15    4:13   0.46s  0.04s bash 
[root@tanyvlinux ~]# echo $SSH_TTY			#终端变量;
/dev/pts/0
[root@tanyvlinux ~]# pstree								#程序树;
systemd─┬─ModemManager───2*[{ModemManager}]
        ├─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─abrt-dbus───2*[{abrt-dbus}]
        ├─2*[abrt-watch-log]
        ├─abrtd
        ├─alsactl
        ├─atd
        ├─auditd─┬─audispd─┬─sedispatch
        │        │         └─{audispd}
        │        └─{auditd}
        ├─avahi-daemon───avahi-daemon
        ├─bluetoothd
        ├─chronyd
        ├─crond
        ├─cupsd
        ├─dbus-daemon───{dbus-daemon}
        ├─dhclient
        ├─dnsmasq───dnsmasq
        ├─firewalld───{firewalld}
        ├─gssproxy───5*[{gssproxy}]
        ├─httpd───5*[httpd]
        ├─ksmtuned───sleep
        ├─libvirtd───16*[{libvirtd}]
        ├─login───bash
        ├─lsmd
        ├─lvmetad
        ├─mysqld_safe───mysqld───30*[{mysqld}]
        ├─packagekitd───2*[{packagekitd}]
        ├─php-fpm───4*[php-fpm]
        ├─polkitd───6*[{polkitd}]
        ├─rngd
        ├─rpcbind
        ├─rsyslogd───2*[{rsyslogd}]
        ├─smartd
        ├─sshd─┬─sshd───bash───bash			#前一个终端;
        │      └─sshd───bash───pstree					#这个终端;
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        ├─vmtoolsd───{vmtoolsd}
        └─vsftpd
[root@tanyvlinux ~]# echo $d		#不能用其他shell定义的变量;

  • 子shell定义的变量,不能向上被父shell使用
[root@tanyvlinux ~]# export e=999
[root@tanyvlinux ~]# echo $e
999
[root@tanyvlinux ~]# exit
exit
[root@tanyvlinux ~]# echo $e

  • 取消变量unset
[root@tanyvlinux ~]# echo $d
3$ab
[root@tanyvlinux ~]# unset d
[root@tanyvlinux ~]# echo $d

环境配置文件

  • /etc/profile文件,文件里备注System wide environment and startup programs,直译是系统环境和启动程序,每个用户登陆时都会加载,影响每一个用户,一般不会修改,修改后需要source /etc/profile(或. /etc/profile)让操作有效;

  • /etc/bashrc文件,文件里备注login setup Functions and aliases,直译是启动功能和别名,运行shell的时候都会加载,每一个用户使用shell都影响到,可用于修改别名,修改后重新登陆shell或运行shell生效;

  • ~/.bash_profile, 对应于/etc/profile文件,修改只对当前用户生效,修改后需要source ~/.bash_profile(或. ~/.bash_profile)让操作有效;

  • ~/.bashrc,对应于/etc/bashrc文件,但只对当前用户使用shell产生影响,可用于创建用户个性化别名,修改后重新登陆shell或运行shell生效;

  • 用户登陆时使用这些文件的顺序是/etc/profile - ~/.bash_profile - ~/.bashrc - /etc/bashrc

  • ~/.bash_logout系统退出时进行的操作,可添加清除使用记录的命令进去;

  • 变量PS1

[root@tanyvlinux ~]# echo $PS1
[\u@\h \W]\$
[root@tanyvlinux ~]# PS1="<\u@\h \w>\$"		#修改变量;
<root@tanyvlinux ~>$cd /usr/local/src
<root@tanyvlinux /usr/local/src>$					#显示效果变化;
  • 修改PS1颜色
<root@tanyvlinux /usr/local/src>$PS1=PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[36;40m\]\w\[\e[0m\]]\\$ "
PS1=[root@tanyvlinux /usr/local/src]# 
PS1=[root@tanyvlinux /usr/local/src]# 
  • PS2用在另外一种环境中的命令提示
PS1=[root@tanyvlinux /usr/local/src]# PS2='<'			#修改PS2;
PS1=[root@tanyvlinux /usr/local/src]# echo $PS2
<
PS1=[root@tanyvlinux /usr/local/src]# for i in `seq 1 10`		#shell脚本;
<do^C																#出现"<"符号;

特殊符号

在这里插入图片描述
使用例子:

[root@tanyvlinux ~]# ls
11.txt   2.txt            chapter11  dir3                NetBeansProjects                         wget-1.14-18.el7_6.1.x86_64.rpm  公共  文档
1.cap    33.txt           chapter4   gallery             new.txt                                  wordpress                        模板  下载
1.txt    anaconda-ks.cfg  Desktop    hs_err_pid9271.log  url2png                                  wordpress-3.9-zh_CN.zip          视频  音乐
222.txt  bbs.conf         dir2       mysqld              vim-enhanced-7.4.160-6.el7_6.x86_64.rpm  zrlog.war                        图片
[root@tanyvlinux ~]# ls *.txt					#*的使用;
11.txt  1.txt  222.txt  2.txt  33.txt  new.txt
[root@tanyvlinux ~]# ls ?.txt				#?的使用;
1.txt  2.txt
[root@tanyvlinux ~]# # ll ?.txt				##的使用;
[root@tanyvlinux ~]# a=1
[root@tanyvlinux ~]# b=2
[root@tanyvlinux ~]# c=$a$b
[root@tanyvlinux ~]# echo $c
12
[root@tanyvlinux ~]# c=\$a\$b		#\的使用;
[root@tanyvlinux ~]# echo $c
$a$b
[root@tanyvlinux ~]# tail -2 /etc/passwd | cut -d ":" -f 1,3,4		#|和cut的使用;
zabbix:986:979
ut1:1011:10

几个与管道有关的命令

在这里插入图片描述

  • cut截取第n个字符
[root@tanyvlinux ~]# tail -2 /etc/passwd |cut -c 1
z
u
[root@tanyvlinux ~]# tail -2 /etc/passwd 
zabbix:x:986:979:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
ut1:x:1011:10::/home/ut2:/bin/bash
  • sort排序
    默认从第一个字符向后,依次按照ASCII码值进行比较,然后依次输出。排序例子:
    在这里插入图片描述
  • sort -n按数字大小排序,字母和符号开头的行保持默认排序(全部前置)
[root@tanyvlinux ~]# sort 11.txt -n
<<>{
<>
]\
a3
a:3
b2
b:2
c1
c:1
kdjfjlldkjfkdkjslsdfkjslkfjk123
3
33
222
  • sort -r 反向排序,可以叠加其他选项

  • sort -t 按某个段的内容排序;

  • wc -l统计行数

  • wc -m统计字符数,包括换行符和tab;

[root@tanyvlinux ~]# wc -m 11.txt		#共9个字符;
9 11.txt
[root@tanyvlinux ~]# cat -A 11.txt		#含换行符和tab;
123$
^Iabc$
  • wc -w 统计单词数,单词以空格和换行分格
[root@tanyvlinux ~]# wc -w 11.txt
5 11.txt
[root@tanyvlinux ~]# cat 11.txt
123 ooo iii,lls   iioo.lslls
	abc
  • uniq, 去除连续重复的行;(跟sort一起使用);-c统计重复个数;
[root@tanyvlinux ~]# uniq 11.txt
123
456
123
456
[root@tanyvlinux ~]# sort 11.txt |uniq
123
456
[root@tanyvlinux ~]# sort 11.txt |uniq -c
      3 123
      3 456
[root@tanyvlinux ~]# cat 11.txt
123
456
123
123
456
456
  • tee命令,重定向和显示内容
    在这里插入图片描述

tee -a 追加重定向,在文件的后面添加内容

  • tr 一一对应的修改字符

  • split 切割文件
    使用例子如下:

[root@tanyvlinux dira]# ls -lh			#文件大小761K;
总用量 764K
-rw-r--r--. 1 root root 761K 9月  11 21:29 a.txt
[root@tanyvlinux dira]# split -b 1K a.txt	#以1K的大小切割文件,连原文件共762个文件,自动命名;
[root@tanyvlinux dira]# ls | wc -l
762
 [root@tanyvlinux dira]# split -b 1K a.txt as.		#指定前缀命名;
[root@tanyvlinux dira]# ls										#截选内容;
as.aa  as.bn  as.da  as.en  as.ga  as.hn  as.ja  as.kn  as.ma  as.nn  as.pa  as.qn  as.sa  as.tn  as.va  as.wn  as.ya    as.zaan  as.zaca  as.zadn
as.ab  as.bo  as.db  as.eo  as.gb  as.ho  as.jb  as.ko  as.mb  as.no  as.pb  as.qo  as.sb  as.to  as.vb  as.wo  as.yb    as.zaao  as.zacb  as.zado
[root@tanyvlinux dira]# wc -l a.txt				#文件行数;
22641 a.txt
[root@tanyvlinux dira]# split -l 1000 a.txt	#以每1000行分隔文件;
[root@tanyvlinux dira]# ls
a.txt  xaa  xab  xac  xad  xae  xaf  xag  xah  xai  xaj  xak  xal  xam  xan  xao  xap  xaq  xar  xas  xat  xau  xav  xaw
[root@tanyvlinux dira]# find -name "x*" -exec wc -l {} \;		#查看每个文件的行数,共22641行;
1000 ./xaa
1000 ./xab
1000 ./xac
1000 ./xad
1000 ./xae
1000 ./xaf
1000 ./xag
1000 ./xah
1000 ./xai
1000 ./xaj
1000 ./xak
1000 ./xal
1000 ./xam
1000 ./xan
1000 ./xao
1000 ./xap
1000 ./xaq
1000 ./xar
1000 ./xas
1000 ./xat
1000 ./xau
1000 ./xav
641 ./xaw
  • 特殊符号2
    在这里插入图片描述
[root@tanyvlinux ~]#  [ -d dira ] && mkdir dirb	#第一条命令成功,执行第二条命令;第一条命令不成功,第二条命令不执行;相当于两条命令同命相连;
[root@tanyvlinux ~]#  [ -d dirb ]		#判断目录dirb是否存在;
[root@tanyvlinux ~]# echo $?			#0表示上一条命令结果为1, 目录存在;
0
[root@tanyvlinux ~]# ls -d dirb
dirb
[root@tanyvlinux ~]# ls -d dira
dira
[root@tanyvlinux ~]#  [ -d dirc ]		
[root@tanyvlinux ~]# echo $?			#1表示上一条命令结果为0,目录不存在;
1
[root@tanyvlinux ~]# ls -d dirc
ls: 无法访问dirc: 没有那个文件或目录
[root@tanyvlinux ~]#  [ -d dirc ] || mkdir dirc		#||表示或者,第一条命令结果为1,第二条命令就不执行;第一条命令结果为0,执行第二条命令,二选一;
[root@tanyvlinux ~]# ls -d dirc
dirc
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值