天天看点

1 linux shell基础一,DAY1二,DAY2三,DAY3

[toc]

shell是一个命令解释器,提供用户和机器之间的交互(shell脚本是shell的一种表现)

支持特定语法,比如逻辑判断、循环 (if,for,while)

每个用户都可以有自己特定的shell

root:x:0:0:root:/root:/bin/bash

centos7默认shell为bash(bourne agin shell)

还有zsh、ksh等

history命令

<code>.bash_history</code> 存放命令记录的文件,如果终端非exit,logout正常退出,命令会记录不全。

最大1000条,默认1000条,可修改

变量<code>histsize</code>, 查看 <code>echo $histsize</code>如果显示超过1000,是因为命令暂时存在内存中,还没写进文件,<code>history-c</code>可清空内存里记录,但是不会清除<code>.bash_history</code>里的记录

<code>/etc/profile</code>中修改,要修改后立即生效重新进终端或执行 <code>source /etc/profile</code>

<code>histtimeformat="%y/%m/%d %h:%m:%s "</code> 增加此变量,可记录对应命令的执行时间, 例: <code>1005 2019/11/12 10:29:47 w</code>

永久保存 <code>chattr +a ~/.bash_history</code> 即使超过1000条也没有权限删除

!! 上一条命令

!n n是数字,执行history记录里对应编号的命令

!word

tab键,敲一下,敲两下

centos7支持参数补全,安装<code>yum install -y bash-completion</code> 需要重启系统生效 <code>systemctl restart network</code>

alias别名给命令重新起个名字 <code>alias restartnet="systemctl restart network"</code> 取消alias:<code>unalias restartnet</code>

各用户都有自己配置别名的文件 <code>~/.bashrc</code>

ls /etc/profile.d/

自定义的alias放到<code>~/.bashrc</code>

ls *.txt

ls ?.txt

ls [0-9].txt

ls {1,2}.txt

cat 1.txt &gt;2.txt

cat 1.txt &gt;&gt; 2.txt

ls aaa.txt 2&gt;err //2&gt;表示错误重定向

ls aaa.txt 2&gt;&gt;err //2&gt;&gt;表示错误追加重定向

&amp;&gt; 结合正确和错误的重定向

wc -l &lt; 1.txt

command &gt;1.txt 2&gt;&amp;1

cat 1.txt |wc -l ; cat 1.txt |grep 'aaa'

把前的结果交给后面的命令

ctrl z 暂停一个任务

jobs查看后台的任务

bg[id]把任务调到后台

fg[id]把任务调到前台

命令后面加&amp;直接丢到后台 <code>sleep 100 &amp;</code>

path,home,pwd,logname 常风的一些系统变量

env命令,可以查看系统常用变量,系统变量名通常是大写,变量值可以是数值,也可以是字符串

set命令多了很多变量,并且包括用户自定义的变量

自定义变量a=1

变量名规则:字母、数字下划线,首位不能为数字,可以a1=4 a_1=4 但是不可以1a=4

变量值有特殊符号时需要用单引号括起来

<code>a='a b c'</code> 里面带空格要用单引号括起来

变量的累加,变量累加时要用双引号

全局变量export b=2

unset变量,取消赋值, <code>unset b</code> unset 变量名

系统层面的环境变量,etc下的配置文件,全局生效

<code>/etc/profile</code> 用户环境变量,交互,登录才执行,输入用户名,ip,port,密码就会自动加载,然后profile又会自动的调用bashrc。

<code>/etc/bashrc</code> 用户不用登录,执行shell就生效,只要执行shell脚本,就会调用bashrc里面的配置

用户层面的环境变量,在各个用户的家目录下

<code>~/.bashrc</code>

<code>~/.bash_profile</code> . .bash_profile或者source .bash_profile

<code>~/.bash_history</code>

<code>~/.bash_logout</code> 定义用户退出时需要做出的一些动作,比如:退出时删除命令历史,就可把删除历史命令的命令,放在.bash_logout里面

<code>ps1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '</code>

<code>vim /etc/bashrc</code> 里定义

<code>* 任意个任意字符</code>

<code>? 任意一个字符</code>

<code># 注释字符</code>

<code>\ 脱义字符</code> 要使c='$a$b'的结果为$a$b除了加单引号,还可以使用脱意符c=\$a\$b。

<code>| 管道符</code>

<code>cut 分割</code>,-d 分隔符 -f 指定段号 -c 指定第几个字符

演示:

<code>sort 排序</code>, -n 以数字排序 -r 反序 -t 分隔符 -kn1/-kn1,n2

<code>wc -l 统计行数</code> -m 统计字符数 -w 统计词

演示

[root@localhost ~]# cat 1.txt

123

abc ,f23

[root@localhost ~]# wc -w 1.txt 统计多少个词,以空格区分

3 1.txt

<code>uniq 去重</code>, -c统计行数

[root@localhost ~]# sort 1.txt |uniq 去重首先要排序,uniq通常要与sort一起使用

1

2

abc

[root@localhost ~]# sort 1.txt |uniq -c 先排序再去重再统计行数

2 1

2 123

1 2

1 abc

1 abc ,f23

<code>tee 和 &amp;gt; 类似</code>,重定向的同时还在屏幕显示

[root@localhost ~]# sort 1.txt |uniq -c |tee a.txt |tee的作用相当于&gt;,但是它会把重定向的内容显示在屏屏幕上

[root@localhost ~]# sort 1.txt |uniq -c |tee -a a.txt |tee -a 相当于&gt;&gt;

[root@localhost ~]# cat a.txt

<code>tr 替换字符</code>,tr 'a' 'b',大小写替换tr '[a-z]' '[a-z]'

<code>split 切割</code>,-b大小(默认单位字节),-l行数, -d添加数字后缀

$ 变量前缀,!$组合,正则里面表示行尾

;多条命令写到一行,用分号分割

~ 用户家目录,后面正则表达式表示匹配符

&amp; 放到命令后面,会把命令丢到后台

<code>&amp;gt; &amp;gt;&amp;gt; 2&amp;gt; 2&amp;gt;&amp;gt; &amp;&amp;gt;</code>

<code>[ ]</code> 指定字符中的一个,[0-9],[a-za-z],[abc]

|| 和 &amp;&amp; ,用于命令之间

[root@localhost test]# ls 2a.txt &amp;&amp; wc -l 2.txt

ls: 无法访问2a.txt: 没有那个文件或目录

[root@localhost test]# ls 2.txt &amp;&amp; wc -l 2.txt

2.txt

77517 2.txt

前面执行不成功,后面就不会去执行,前面执行成功,后面才会去执行

[root@localhost test]# [ -d aminglinux ] || mkdir aminglinux

[root@localhost test]# ls

201911.aa 201911.ac 201911.ae 201911.ag 2.txt

201911.ab 201911.ad 201911.af 201911.ah aminglinux

[root@localhost test]# [ -d aminglinux ] &amp;&amp; mkdir aminglinux

mkdir: 无法创建目录"aminglinux": 文件已存在