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

相关文章

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

一篇文章彻底搞懂macOS如何决定java环境

《一篇文章彻底搞懂macOS如何决定java环境》MacOS作为一个功能强大的操作系统,为开发者提供了丰富的开发工具和框架,下面:本文主要介绍macOS如何决定java环境的相关资料,文中通过代码... 目录方法一:使用 which命令方法二:使用 Java_home工具(Apple 官方推荐)那问题来了,

Nginx搭建前端本地预览环境的完整步骤教学

《Nginx搭建前端本地预览环境的完整步骤教学》这篇文章主要为大家详细介绍了Nginx搭建前端本地预览环境的完整步骤教学,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录项目目录结构核心配置文件:nginx.conf脚本化操作:nginx.shnpm 脚本集成总结:对前端的意义很多

Linux云服务器手动配置DNS的方法步骤

《Linux云服务器手动配置DNS的方法步骤》在Linux云服务器上手动配置DNS(域名系统)是确保服务器能够正常解析域名的重要步骤,以下是详细的配置方法,包括系统文件的修改和常见问题的解决方案,需要... 目录1. 为什么需要手动配置 DNS?2. 手动配置 DNS 的方法方法 1:修改 /etc/res

mysql8.0.43使用InnoDB Cluster配置主从复制

《mysql8.0.43使用InnoDBCluster配置主从复制》本文主要介绍了mysql8.0.43使用InnoDBCluster配置主从复制,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录1、配置Hosts解析(所有服务器都要执行)2、安装mysql shell(所有服务器都要执行)3、

java程序远程debug原理与配置全过程

《java程序远程debug原理与配置全过程》文章介绍了Java远程调试的JPDA体系,包含JVMTI监控JVM、JDWP传输调试命令、JDI提供调试接口,通过-Xdebug、-Xrunjdwp参数配... 目录背景组成模块间联系IBM对三个模块的详细介绍编程使用总结背景日常工作中,每个程序员都会遇到bu

Python之变量命名规则详解

《Python之变量命名规则详解》Python变量命名需遵守语法规范(字母开头、不使用关键字),遵循三要(自解释、明确功能)和三不要(避免缩写、语法错误、滥用下划线)原则,确保代码易读易维护... 目录1. 硬性规则2. “三要” 原则2.1. 要体现变量的 “实际作用”,拒绝 “无意义命名”2.2. 要让

Python中的sort方法、sorted函数与lambda表达式及用法详解

《Python中的sort方法、sorted函数与lambda表达式及用法详解》文章对比了Python中list.sort()与sorted()函数的区别,指出sort()原地排序返回None,sor... 目录1. sort()方法1.1 sort()方法1.2 基本语法和参数A. reverse参数B.

JDK8(Java Development kit)的安装与配置全过程

《JDK8(JavaDevelopmentkit)的安装与配置全过程》文章简要介绍了Java的核心特点(如跨平台、JVM机制)及JDK/JRE的区别,重点讲解了如何通过配置环境变量(PATH和JA... 目录Java特点JDKJREJDK的下载,安装配置环境变量总结Java特点说起 Java,大家肯定都

linux配置podman阿里云容器镜像加速器详解

《linux配置podman阿里云容器镜像加速器详解》本文指导如何配置Podman使用阿里云容器镜像加速器:登录阿里云获取专属加速地址,修改Podman配置文件并移除https://前缀,最后拉取镜像... 目录1.下载podman2.获取阿里云个人容器镜像加速器地址3.更改podman配置文件4.使用po