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

2024-03-16 17:48

本文主要是介绍,变量,环境配置,特殊符号,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 ~]# w23: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.lsllsabc
  • 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 -c3 1233 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

这篇关于,变量,环境配置,特殊符号,cut, sort, tee, split,, ||的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/816248

相关文章

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Debian系和Redhat系防火墙配置方式

《Debian系和Redhat系防火墙配置方式》文章对比了Debian系UFW和Redhat系Firewalld防火墙的安装、启用禁用、端口管理、规则查看及注意事项,强调SSH端口需开放、规则持久化,... 目录Debian系UFW防火墙1. 安装2. 启用与禁用3. 基本命令4. 注意事项5. 示例配置R

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q

Redis MCP 安装与配置指南

《RedisMCP安装与配置指南》本文将详细介绍如何安装和配置RedisMCP,包括快速启动、源码安装、Docker安装、以及相关的配置参数和环境变量设置,感兴趣的朋友一起看看吧... 目录一、Redis MCP 简介二、安www.chinasem.cn装 Redis MCP 服务2.1 快速启动(推荐)2.

Go语言编译环境设置教程

《Go语言编译环境设置教程》Go语言支持高并发(goroutine)、自动垃圾回收,编译为跨平台二进制文件,云原生兼容且社区活跃,开发便捷,内置测试与vet工具辅助检测错误,依赖模块化管理,提升开发效... 目录Go语言优势下载 Go  配置编译环境配置 GOPROXYIDE 设置(VS Code)一些基本

Spring Boot配置和使用两个数据源的实现步骤

《SpringBoot配置和使用两个数据源的实现步骤》本文详解SpringBoot配置双数据源方法,包含配置文件设置、Bean创建、事务管理器配置及@Qualifier注解使用,强调主数据源标记、代... 目录Spring Boot配置和使用两个数据源技术背景实现步骤1. 配置数据源信息2. 创建数据源Be

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

RabbitMQ消息总线方式刷新配置服务全过程

《RabbitMQ消息总线方式刷新配置服务全过程》SpringCloudBus通过消息总线与MQ实现微服务配置统一刷新,结合GitWebhooks自动触发更新,避免手动重启,提升效率与可靠性,适用于配... 目录前言介绍环境准备代码示例测试验证总结前言介绍在微服务架构中,为了更方便的向微服务实例广播消息,

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs