,变量,环境配置,特殊符号,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

相关文章

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

SQLite3 在嵌入式C环境中存储音频/视频文件的最优方案

《SQLite3在嵌入式C环境中存储音频/视频文件的最优方案》本文探讨了SQLite3在嵌入式C环境中存储音视频文件的优化方案,推荐采用文件路径存储结合元数据管理,兼顾效率与资源限制,小文件可使用B... 目录SQLite3 在嵌入式C环境中存储音频/视频文件的专业方案一、存储策略选择1. 直接存储 vs

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

Python变量与数据类型全解析(最新整理)

《Python变量与数据类型全解析(最新整理)》文章介绍Python变量作为数据载体,命名需遵循字母数字下划线规则,不可数字开头,大小写敏感,避免关键字,本文给大家介绍Python变量与数据类型全解析... 目录1、变量变量命名规范python数据类型1、基本数据类型数值类型(Number):布尔类型(bo

Nginx 重写与重定向配置方法

《Nginx重写与重定向配置方法》Nginx重写与重定向区别:重写修改路径(客户端无感知),重定向跳转新URL(客户端感知),try_files检查文件/目录存在性,return301直接返回永久重... 目录一.try_files指令二.return指令三.rewrite指令区分重写与重定向重写: 请求

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.