您好,登录后才能下订单哦!
[TOC]
shell是一个命令解释器,提供用户和机器之间的交互(shell脚本是shell的一种表现)
.bash_history 存放命令记录的文件,如果终端非exit,logout正常退出,命令会记录不全。HISTSIZE,  查看 echo $HISTSIZE如果显示超过1000,是因为命令暂时存在内存中,还没写进文件,history-c可清空内存里记录,但是不会清除.bash_history里的记录/etc/profile中修改,要修改后立即生效重新进终端或执行 source /etc/profileHISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 增加此变量,可记录对应命令的执行时间, 例: 1005  2019/11/12 10:29:47 wchattr +a ~/.bash_history 即使超过1000条也没有权限删除yum install -y bash-completion 需要重启系统生效 systemctl restart networkalias restartnet="systemctl restart network"  取消alias:unalias restartnet~/.bashrc自定义的alias放到~/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias restartnet="systemctl restart network"
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
sleep 100 &a='a b c'  里面带空格要用单引号括起来
[root@mydb1 ~]# a="a$bc"
[root@mydb1 ~]# echo $a
a
[root@mydb1 ~]# a='a$bc'
[root@mydb1 ~]# echo $a
a$bc
在取值时,单引号和双引号结果不一样,所以在取值时要用单引号[root@mydb1 ~]# a=1
[root@mydb1 ~]# b=2
[root@mydb1 ~]# echo $a$b
12
[root@mydb1 ~]# a='a$bc'
[root@mydb1 ~]# echo $a$b
a$bc2
[root@mydb1 ~]# c="a$bc"
[root@mydb1 ~]# echo $c
a
[root@mydb1 ~]# c="a$b"c
[root@mydb1 ~]# echo $c
a2c全局变量export b=2
[root@mydb1 ~]# w
 14:44:15 up 24 days, 23:49,  2 users,  load average: 0.07, 0.04, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.1.182    10:18    7:11   0.37s  0.37s -bash
root     pts/1    192.168.1.182    14:44    0.00s  0.03s  0.01s w
[root@mydb1 ~]# echo $SSH_TTY  查看当前终端的tty
/dev/pts/1
[root@localhost ~]# yum -y install psmisc
[root@localhost ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─agetty
        ├─auditd───{auditd}
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───6*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd───sshd───bash───pstree
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─vmtoolsd
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# bash  //bash命令进入到子shell
[root@localhost ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─agetty
        ├─auditd───{auditd}
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───6*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd───sshd───bash───bash───pstree  //这里可以看出在子shell上执行了pstree命令
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─vmtoolsd
[root@localhost ~]# a=linux  
[root@localhost ~]# echo $a
linux
[root@localhost ~]# bash
[root@localhost ~]# echo $a    非全局变量下,进入子shell,无法获取父shell中a=linux的值,这是因为在非全局变量下,当前定义的变量只在当前shell中生效,而不能在子shell生效
[root@localhost ~]# export b=123  父shell下定义全局变量 b=123,这样定义全局变量只在当前终端有效,重新打开新的终端无效。
[root@localhost ~]# echo $b      
123
[root@localhost ~]# bash         进入子shell,仍然可以获得父shell定义的b=123的值
[root@localhost ~]# echo $b
123
[root@localhost ~]# export c=456  在子shell中定义c=456
[root@localhost ~]# echo $c
456
[root@localhost ~]# exit    退出子shell进入父shell
exit
[root@localhost ~]# echo $c  在父shell中无法获得在子shell中定义c=456的值,父shell与子shell的关系是从上到下的,而不能从下到上
unset b unset 变量名/etc/profile 用户环境变量,交互,登录才执行,输入用户名,ip,port,密码就会自动加载,然后profile又会自动的调用bashrc。/etc/bashrc 用户不用登录,执行shell就生效,只要执行shell脚本,就会调用bashrc里面的配置~/.bashrc   ~/.bash_profile  . .bash_profile或者source .bash_profile~/.bash_history~/.bash_logout  定义用户退出时需要做出的一些动作,比如:退出时删除命令历史,就可把删除历史命令的命令,放在.bash_logout里面PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '
vim /etc/bashrc 里定义
* 任意个任意字符? 任意一个字符# 注释字符\ 脱义字符  要使c='$a$b'的结果为$a$b除了加单引号,还可以使用脱意符c=\$a\$b。| 管道符cut 分割,-d 分隔符  -f 指定段号   -c 指定第几个字符
[root@localhost ~]# cat /etc/passwd |head -2
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1
root
bin
[root@localhost ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1,2
root:x
bin:x
[root@localhost ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1,5
root:root
bin:bin
[root@localhost ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1-5
root:x:0:0:root
bin:x:1:1:bin
[root@localhost ~]# cat /etc/passwd |head -2 |cut -c 4
t
:sort 排序, -n 以数字排序 -r 反序  -t 分隔符 -kn1/-kn1,n2
[root@localhost ~]# cat /etc/passwd |head -4 |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
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# cat /etc/passwd |head -4 |sort -n
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
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# cat /etc/passwd |head -4 |sort -nr
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologinwc -l 统计行数 -m 统计字符数 -w 统计词
[root@localhost ~]# cat 1.txt 内容有6个字符
123
abc
[root@localhost ~]# cat -A 1.txt 
123$
abc$
[root@localhost ~]# wc -m 1.txt  
8 1.txt   统计有8个字符,不要忘记隐藏的换行符[root@localhost ~]# cat 1.txt 
123
abc  ,f23
[root@localhost ~]# wc -w 1.txt  统计多少个词,以空格区分
3 1.txt
uniq 去重, -c统计行数
[root@localhost ~]# uniq 1.txt  直接uniq没有去重
123
abc  ,f23
123
abc
1
2
1[root@localhost ~]# sort 1.txt |uniq  去重首先要排序,uniq通常要与sort一起使用
1
123
2
abc
abc  ,f23
[root@localhost ~]# sort 1.txt |uniq -c  先排序再去重再统计行数
2 1
2 123
1 2
1 abc
1 abc  ,f23
tee 和 > 类似,重定向的同时还在屏幕显示
[root@localhost ~]# cat 1.txt 
123
abc  ,f23
123
abc
1
2
1[root@localhost ~]# sort 1.txt |uniq -c |tee a.txt  |tee的作用相当于>,但是它会把重定向的内容显示在屏屏幕上
2 1
2 123
1 2
1 abc
1 abc  ,f23
[root@localhost ~]# sort 1.txt |uniq -c |tee -a a.txt  |tee -a 相当于>>
2 1
2 123
1 2
1 abc
1 abc  ,f23
[root@localhost ~]# cat a.txt 
2 1
2 123
1 2
1 abc
1 abc  ,f23
2 1
2 123
1 2
1 abc
1 abc  ,f23
tr 替换字符,tr 'a' 'b',大小写替换tr '[a-z]' '[A-Z]'
[root@localhost ~]# echo "aminglinux" |tr 'a' 'A'   替换单个
Aminglinux
[root@localhost ~]# echo "aminglinux" |tr '[al]' '[AL]'  替换多个
AmingLinux
[root@localhost ~]# echo "aminglinux" |tr '[a-z]' '[A-Z]'  替换指定范围
AMINGLINUX
[root@localhost ~]# echo "aminglinux" |tr '[a-z]' '1'  也可以替换成数字
1111111111split 切割,-b大小(默认单位字节),-l行数, -d添加数字后缀
[root@localhost test]# split -b 2M 2.txt 指定切割后每个文件的大小
[root@localhost test]# ls
2.txt  xaa  xab  xac  xad  xae
[root@localhost test]# ll -h
总用量 20M
-rw-r--r--. 1 root root  10M 2月  24 08:57 2.txt
-rw-r--r--. 1 root root 2.0M 2月  24 09:04 xaa
-rw-r--r--. 1 root root 2.0M 2月  24 09:04 xab
-rw-r--r--. 1 root root 2.0M 2月  24 09:04 xac
-rw-r--r--. 1 root root 2.0M 2月  24 09:04 xad
-rw-r--r--. 1 root root 2.0M 2月  24 09:04 xae
[root@localhost test]# split -b 2M 2.txt 2019.  指定切割后每个文件的大小以及文件的前缀
[root@localhost test]# ls
2019.aa  2019.ab  2019.ac  2019.ad  2019.ae  2.txt  xaa  xab  xac  xad  xae
[root@localhost test]# split -l 10000 2.txt 201911.   指定切割文件的行数以及文件的前缀
[root@localhost test]# ls
201911.aa  201911.ab  201911.ac  201911.ad  201911.ae  201911.af  201911.ag  201911.ah  2.txt
[root@localhost test]# for i in `seq 1 10`
> do
> echo $i
> done
1
2
3
4
5
6
7
8
9
10
[root@localhost test]# for i in `seq 1 10`; do echo $i; done
[root@localhost test]# ls 2.txt ; wc -l 2.txt 
2.txt
77517 2.txt> >> 2> 2>> &>[ ] 指定字符中的一个,[0-9],[a-zA-Z],[abc]|| 和 && ,用于命令之间
[root@localhost test]# ls 2.txt || wc -l 2.txt 
2.txt
[root@localhost test]# ls 2a.txt || wc -l 2.txt 
ls: 无法访问2a.txt: 没有那个文件或目录
77517 2.txt
|| 如果前面执行成功,就不执行后面。如果前面执行不成功,才会执行后面[root@localhost test]# ls 2a.txt && wc -l 2.txt 
ls: 无法访问2a.txt: 没有那个文件或目录
[root@localhost test]# ls 2.txt && 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 ] && mkdir aminglinux
mkdir: 无法创建目录"aminglinux": 文件已存在
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。