Linux shell基础知识(下)——特殊符号、cut、wc、

本文深入讲解了 Linux Shell 中的环境变量配置文件及其作用,包括 /etc/profile、/etc/bashrc 和用户级别的配置文件如 ~/.bashrc。此外,还详细介绍了 PS1 的颜色设置方法以及 shell 命令如 cut、sort、wc、uniq、tee、tr 和 split 的使用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linux shell基础知识(下)

七、变量
7.3 环境变量配置文件
  • 环境变量配置文件

    • /etc/profile 用户环境变量,交互,登录才执行
    • /etc/bashrc 用户不用登录,执行shell就生
    • ~/.bashrc
    • ~/.bash_profile
    • ~/.bash_history
    • ~/.bash_logout
    • PS1='[\033[01;32m]\u@\h[\033[00m]:[\033[01;36m]\w[\033[00m]$ ' //使其变色
  • /etc/profile 用户环境变量,交互、登陆才执行;

在/etc/profile文件中添加变量【对所有用户生效(永久的)】用VI在文件/etc/profile文件中增加变量,该变量将会对Linux下所有用户有效,并且是“永久的”。例如:编辑/etc/profile文件,添加CLASSPATH变量**export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib **

注:修改文件后要想马上生效还要运行# source /etc/profile不然只能在下次重进此用户时生效。

  • /etc/bashrc 用户不用登陆,执行shell就生效。

这个文件主要预设umask以及PS1;这个PS1就是我们在输入命令时前面的那串字符;

  • ~/.bashrc

概念:该文件包含专属于自己的shell的bash信息; 当登陆或每次新打开新的shell时,该文件会被读取; 例如,你可以将用户自定义的别名或者自定义变量写到这个文件中。

  • ~/.bash_profile

概念:该文件定义了用户的个人化路径于环境变量的文件名称; 每个用户都可使用该文件输入专属自己的shell信息,当用户登陆时,该文件仅执行一次。

  • ~/.bash_history

概念:该文件用户记录命令历史。

  • ~/.bash_logout

概念:当退出shell时,会执行该文件,可以将一些清理的工作放到这个文件中。

PS1基本设置

[root@ying01 ~]#cd NBA/5
[root@ying01 5]#echo $PS1                 //查看PS1系统赋予的变量
[\u@\h \W]\$
[root@ying01 5]#PS1='\u@\h \W\$'          //把方括号[]去掉
root@ying01 5#
root@ying01 5#PS1='<\u@\h \W>\$'          //添加尖括号<>
<root@ying01 5>#
<root@ying01 5>#PS1='<\u@\h \w>\$'        //把W改为w,改为绝对路径
<root@ying01 ~/NBA/5>#

PS1中颜色设置

<root@ying01 ~/NBA/5>#PS1='\033[01;32m\]\u@\h\033[00m\]:\033[01;36m\]\w\033[00m\]\$'
root@ying01:~/NBA/5#PS1='[\033[01;32m\]\u@\h\033[00m\]:\033[01;36m\]\w\033[00m\]]\$'
[root@ying01:~/NBA/5]#PS1='{\033[01;32m\]\u@\h\033[00m\]:\033[01;36m\]\w\033[00m\]}\$'
{root@ying01:~/NBA/5}#PS1='<\033[01;32m\]\u@\h\033[00m\]:\033[01;36m\]\w\033[00m\]>\$'
<root@ying01:~/NBA/5>#PS1='<\u@\h \w>\$'
<root@ying01 ~/NBA/5>#PS1='{\033[01;32m\]\u@\h\033[00m\]:\033[01;36m\]\w\033[00m\]}\$'
{root@ying01:~/NBA/5}#

对于颜色相关设置请参考: shell脚本中echo显示内容带颜色

PS2设置

{root@ying01:~}#echo $PS2
>
{root@ying01:~}#PS2='#'
{root@ying01:~}#echo $PS2
#
{root@ying01:~}#PS2='@'
{root@ying01:~}#echo $PS2
@
{root@ying01:~}#for i in `seq 1 6`
@do
@echo $i
@done
1
2
3
4
5
6

八、shell特殊符号

8.1 特殊符号简介
符号释义
*任意个任意字符,通配符
?任意一个字符
#注释字符
\脱义字符
管道符
  • 星号* 作为匹配文件名扩展的一个通配符,能自动匹配给定目录下的每一个文件;
{root@ying01:~}#ls *txt*
123.txt  1ceshi.txt  234.txt  x.txt.bz2  x.txt.gz  xx.txt  y.txt.xz
{root@ying01:~}#ls *txt
123.txt  1ceshi.txt  234.txt  xx.txt

  • **问号?**作为通配符,用于匹配文件名扩展特性中,用于匹配单个字符;
{root@ying01:~}#ls 1?.txt
12.txt  15.txt  18.txt  19.txt
  • **注释符号# **在一行中,#后面的内容并不会被执行;但是用单/双引号包围时,#作为#号字符本身,不具有注释作用。
{root@ying01:~}#b=1234 #5678
{root@ying01:~}#echo $b
1234
{root@ying01:~}#c='#'1234
{root@ying01:~}#echo $c
#1234
{root@ying01:~}#d=#1234
{root@ying01:~}#echo $d           //无法解释
#1234

  • **脱义字符 \ **这个字符会将后面特殊的符号还原为普通字符;

  • 管道符 | 它的作用是将管道前(左边)的命令产生的输出作为管道后(右边)的命令的输入。作为管道右边的命令,并不是所有的命令都可以;

{root@ying01:~}#cat /etc/passwd |wc -l
35
{root@ying01:~}# wc -l /etc/passwd
35 /etc/passwd

8.2 命令cut

概念:cut命令用来截取某一个字段 格式:cut -d [-cf] n cut -d截取分割符号为“:”, -f 1 表示截取第一段,1,2表示前两段,1-3表示头三段。

  • -d 后面跟分割字符,分割字符要用单引号括起来
  • -c 后面接的是第几个字符
  • -f 后面接的是第几个区块
{root@ying01:~}#cat -n /etc/passwd |head -2
     1	root:x:0:0:root:/root:/bin/bash
     2	bin:x:1:1:bin:/bin:/sbin/nologin
{root@ying01:~}#cat -n /etc/passwd |head -2 |cut -d ':' -f 1
     1	root
     2	bin
{root@ying01:~}#cat -n /etc/passwd |head -2 |cut -d ':' -f 1,2
     1	root:x
     2	bin:x
{root@ying01:~}#cat -n /etc/passwd |head -2 |cut -d ':' -f 1,3,5
     1	root:0:root
     2	bin:1:bin
{root@ying01:~}#cat -n /etc/passwd |head -2 |cut -d ':' -f 1-4
     1	root:x:0:0
     2	bin:x:1:1
  • -c用法
{root@ying01:~}# head -2 /etc/passwd |cut -c1,3,5
ro:
bnx
{root@ying01:~}# head -2 /etc/passwd |cut -c1-20
root:x:0:0:root:/roo
bin:x:1:1:bin:/bin:/
8.3 命令sort

sort命令用于排序 格式:sort [-t 分隔符] [-kn1,n2] [-nru] 这里-n1和n2指的是数字,其他选项如下:

  • -t 后面跟分割字符,作用跟cut -d选项一样,截取符号是什么;
  • -n 表示使用纯数字排序,字母及特殊符号表示为0;(需要和k一起用)
  • -r 表示反向排序;
  • -u 表示除去重复;
  • -kn1,n2 表示由n1区间排序到n2区间,可以只写-kn1,即对n1字段排序。
  • sort不加任何选项,则从首字符后依次按ASCII码值进行比较,最后将它们按升序输出;
{root@ying01:~}# head -n6 /etc/passwd |sort
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
  • -t 后面跟分隔符,-k后面跟单个数字表示对第几个区域字符串排序,-n选项则表示用存数字排序;
{root@ying01:~}# head -n6 /etc/passwd |sort -t: -k3 -n
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
  • -k选项后面跟数字n1和n2表示:对第n1和n2区域内的字符串排序,-r为反向排序
{root@ying01:~}# head -n6 /etc/passwd |sort -t: -k3,5 -r
sync:x:5:0:sync:/sbin:/bin/sync
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
8.4 wc 命令

wc命令用于统计文档的行数,字符数或词数;

  • wc不加选项 数 字数 字节数 文件名称
  • wc -c filename:显示一个文件的字节数
  • wc -m filename:显示一个文件的字符数
  • wc -l filename:显示一个文件的行数
  • wc -L filename:显示一个文件中的最长行的长度
  • wc -w filename:显示一个文件的字数

注意:由于采用UTF-8编码,所以一个汉字在这里被转换为3字节;

{root@ying01:~}# cat 520.txt 
我们 长城 中国人民
work me is aaa c e f 我们 中国长城

{root@ying01:~}# wc 520.txt            
 2 12 68 520.txt                       //行数、字数、字节数、文件名称
{root@ying01:~}# wc -c 520.txt 
68 520.txt                             //3*8+3=27,14+8+1=23,3*6=18,合计68字节
{root@ying01:~}# wc -m 520.txt 
40 520.txt                             //一个汉字一个字符,8+3+23+6=40
{root@ying01:~}# wc -l 520.txt 
2 520.txt                              //2行
{root@ying01:~}# wc -L 520.txt 
34 520.txt                             //根据Unicode码,一个汉字占用2字节,14+6*2+8=34
{root@ying01:~}# wc -w 520.txt 
12 520.txt                             //字数,看空格,中国长城也算一个字数,所以12

查看文件属性

{root@ying01:~}#file 520.txt 
520.txt: UTF-8 Unicode text

8.5 命令uniq

概念:uniq命令用来删除重复的行,改名了只有-c选项比较常用; 它表示统计重复的行数,并把行数写在前面。

[root@ying01 ~]# cat xxy.txt           
111
222
111
333
[root@ying01 ~]# uniq xxy.txt                //执行删除重复行,无效
111
222
111
333
[root@ying01 ~]# sort xxy.txt |uniq          //先用sort排序,执行删除重复行,有效
111
222
333
[root@ying01 ~]# cat xxy.txt                 //再次查看文档,说明uniq并没有真正的删除重复行,只是显示过程中
111
222
111
333
[root@ying01 ~]# sort xxy.txt |uniq -c       //-c选项,统计重复行
      2 111
      1 222
      1 333
8.6 命令tee

tee命令后面跟文件名,起作用类似于重定向>,但它比重定向多一个功能,即把文件写入后面跟的文件时,还会显示在屏幕上;

[root@ying01 ~]# echo "11111111111" |tee aa.txt        //把111111111用tee写入到aa.txt
11111111111                                            //把写入的内容显示屏幕
[root@ying01 ~]# cat aa.txt                            //文本里面也被写入
11111111111
[root@ying01 ~]# echo "22222222222" > aa.txt           //对比重定向>,可以看出>不显示屏幕
[root@ying01 ~]# cat aa.txt 
22222222222

8.7 命令tr

tr命令用于替换字符,常用来处理文档中出现的特殊符号;如DOS文档中出现的符号^M,该命令常用的选项如下:

  • -d 表示删除某个字符,后面跟要删除的字符;
  • -s 表示删除重复的字符。

tr命令常用于把小写字母变成大写字母,如tr '[a-z]' '[A-Z]'

[root@ying01 ~]# head -2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@ying01 ~]# head -2 /etc/passwd |tr '[a-z]' '[A-Z]'
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN

tr命令还可以替换一个字符

[root@ying01 ~]# head -2 /etc/passwd |tr 'r' 'R'
Root:x:0:0:Root:/Root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

-d 表示删除某个字符,后面跟要删除的字符;

[root@ying01 ~]# echo "hello 123 world 456" | tr -d '0-9'
hello  world 
[root@ying01 ~]# echo "hello 123 world 456" | tr -d 'a-z'
 123  456

-s 表示删除重复的字符;

[root@ying01 ~]# echo "hhhhhellooooo 123333 worldddddd 44444555556666" | tr -s 'h,o,3,d,4,5,6'
hello 123 world 456

注意:替换、删除去重复等操作都是针对一个字符来讲的;如果是一个字符串,就不能使用了;

8.8 命令split

split命令用于切割文档,常用的选项为-b和-l

  • -b 表示依据大小来分割文档,默认单位为byte(字节),可以设置单位:K、M等
[root@ying01 test]# du -sh *
1.3M	y.txt
[root@ying01 test]# split -b 300k  y.txt      //分割成300K
[root@ying01 test]# du -sh *
300K	xaa
300K	xab
300K	xac
300K	xad
68K	xae
1.3M	y.txt
[root@ying01 test]# rm -f x*
[root@ying01 test]# split -b 100000  y.txt    //分成成100000字节,此时需要用单位K,切割后文件太多
[root@ying01 test]# du -sb *
100000	xaa
100000	xab
100000	xac
100000	xad
100000	xae
100000	xaf
100000	xag
100000	xah
100000	xai
100000	xaj
100000	xak
100000	xal
96834	xam
1296834	y.txt
  • 也可以指定目标文件名
[root@ying01 test]# split -b 300k y.txt 123           //指定目标名称以123开头
[root@ying01 test]# du -sh *
300K	123aa
300K	123ab
300K	123ac
300K	123ad
68K	123ae
1.3M	y.txt
  • -l 表示依据行数来分割文档
[root@ying01 test]# rm -f 123*
[root@ying01 test]# du -sh *
1.3M	y.txt
[root@ying01 test]# split -l 5000  y.txt        //以5000行为分割单位
[root@ying01 test]# wc -l *
   5000 xaa
   5000 xab
   5000 xac
   5000 xad
   5000 xae
   5000 xaf
   2394 xag
  32394 y.txt
  64788 总用量

九、shell的特殊符号

特殊符号$

$可以用作变量前面的标识符,还可以和!结合使用。

[root@ying01 ~]# ls NBA/5
1.txt  2
[root@ying01 ~]# cd !$        //!$表示上条命令的最后一个变量
cd NBA/5
[root@ying01 5]# 

特殊符号 ;

在一行命令中运行两个或两个以上的命令,需要在命令之间加符号;

[root@ying01 ~]# ls 1ceshi.txt ;wc -l 234.txt 
1ceshi.txt
37 234.txt

特殊符号~

符号~代表用户的家目录,root用户的家目录是/root,普通用户的家目录是/home/username

[root@ying01 ~]# cd ~
[root@ying01 ~]# pwd
/root
[root@ying01 ~]# su nba
[nba@ying01 root]$ cd ~
[nba@ying01 ~]$ pwd
/home/nba

特殊符号 &

如果想把一条命令放到后台执行,则需要加上符号&,它通常用于命令运行时间较长的情况;比如睡眠,打包等;详见第九课 6.2作业符

[root@ying01 ~]# sleep 300 &               //与bg类似
[1] 1504
[root@ying01 ~]# jobs
[1]+  运行中               sleep 300 &

重定向符号>、>>、2>和2>>

和>>分别表示取代和追加的意思。当我们运行一个命令报错时,报错信息会输出到当前屏幕。如果想重定向到一个文本,则要用重定向符号2>或者2>>,它们分别表示错误重定向和错误追加重定向。

  • 2>和2>>它们分别表示错误重定向和错误追加重定向;
[root@ying01 ~]# ls nbanba
ls: 无法访问nbanba: 没有那个文件或目录
[root@ying01 ~]# ls nbanba 2> /tmp/error         
[root@ying01 ~]# cat !$
cat /tmp/error
ls: 无法访问nbanba: 没有那个文件或目录
[root@ying01 ~]# ls nbanba 2>> /tmp/error
[root@ying01 ~]# cat /tmp/error
ls: 无法访问nbanba: 没有那个文件或目录
ls: 无法访问nbanba: 没有那个文件或目录
  • 错误和正确都输入&>
[root@ying01 ~]# ls mmmm &> /tmp/error 
[root@ying01 ~]# cat /tmp/error
ls: 无法访问mmmm: 没有那个文件或目录
[root@ying01 ~]# ls 123.txt &> /tmp/error 
[root@ying01 ~]# cat /tmp/error
123.txt

中括号[]

中括号内为字符组合,代表字符组合中的任意一个。指定一个范围(1-3,a-z)。

[root@ying01 ~]# ls -l 1[1-9].txt
-rw-r--r--. 1 root root 0 6月   3 10:09 12.txt
-rw-r--r--. 1 root root 0 6月   3 10:09 15.txt
-rw-r--r--. 1 root root 0 6月   3 10:09 18.txt
-rw-r--r--. 1 root root 0 6月   3 10:09 19.txt

特殊符号||

使用||时,表示或者,意思是说 如果两条命令用||分割的话,第一条执行成功后,第二条就不会执行,假如第一条命令是错误的话,执行失败就会执行第二条

[root@ying01 ccc]# touch 1.txt 2.txt
[root@ying01 ccc]# ls
1.txt  2.txt
[root@ying01 ccc]# ls 1.txt || touch 3.txt
1.txt
[root@ying01 ccc]# ls
1.txt  2.txt
[root@ying01 ccc]# ls 4.txt || touch 3.txt
ls: 无法访问4.txt: 没有那个文件或目录
[root@ying01 ccc]# ls
1.txt  2.txt  3.txt

特殊符号 &&

使用&& 表示前面的命令执行成功以后,才会执行后面的命令,如果前面命令执行不成功,后面的命令就不会执行。用&&分割,用来判断的

[root@ying01 ccc]# ls
1.txt  2.txt  3.txt
[root@ying01 ccc]# ls 4.txt && touch 5.txt
ls: 无法访问4.txt: 没有那个文件或目录
[root@ying01 ccc]# ls
1.txt  2.txt  3.txt
[root@ying01 ccc]# ls 2.txt && touch 5.txt
2.txt
[root@ying01 ccc]# ls
1.txt  2.txt  3.txt  5.txt

总结下:

  • command1 ; command2

  • command1 && command2

  • command1 || command2

  • 使用 ”;” 时,不管command1是否执行成功都会执行command2;

  • 使用 “&&” 时,只有command1执行成功后,command2才会执行,否则command2不执行;

  • 使用 “||” 时,command1执行成功后command2 不执行,否则去执行command2,总之command1和command2总有一条命令会执行。

Linux shell基础知识(下)输入图片说明

输入图片说明

转载于:https://my.oschina.net/u/3851633/blog/1822494

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值