shell基础(下)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell基础(下)相关的知识,希望对你有一定的参考价值。

shell特殊符号

技术分享图片
技术分享图片

1.cut命令:

[[email protected] ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1
root
bin
[[email protected] ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1,2
root:x
bin:x
[[email protected] ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1-3
root:x:0
bin:x:1
[[email protected] ~]# cat /etc/passwd |head -2 |cut -c 4
t
:

2.sort,排序

[[email protected] ~]# sort /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
[[email protected] ~]# sort 1.txt

<
>
{
13
2222
2 2.txt
333
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
[[email protected] ~]# sort -n 1.txt                       #-n 字母和特殊字符当做0

<
>
{
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
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
*qweq
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
2 2.txt
13
333
2222

3.wc:

[[email protected] ~]# wc -l 1.txt         #统计行数
19 1.txt
[[email protected] ~]# wc -m 1.txt        #统计字符数
418 1.txt
[[email protected] ~]# wc -w 1.txt           #统计词
19 1.txt

4.uniq:去重,

[[email protected] ~]# uniq 2.txt
-bash: lsaaa: 未找到命令
123
abc
123
abc
1
2
[[email protected] ~]# cat 2.txt
-bash: lsaaa: 未找到命令
-bash: lsaaa: 未找到命令
123
abc
123
abc
1
1
2

5.排序再去重:

[[email protected] ~]# sort 2.txt
1
1
123
123
2
abc
abc
-bash: lsaaa: 未找到命令
-bash: lsaaa: 未找到命令
[[email protected] ~]# sort 2.txt |uniq
1
123
2
abc
-bash: lsaaa: 未找到命令
[[email protected] ~]# sort 2.txt |uniq -c         #去重并统计出现次数
      2 1
      2 123
      1 2
      2 abc
      2 -bash: lsaaa: 未找到命令

6.tee命令:类似重定向

[[email protected] ~]# sort 2.txt |uniq -c |tee a.txt
      2 1
      2 123
      1 2
      2 abc
      2 -bash: lsaaa: 未找到命令
[[email protected] ~]# cat a.txt
      2 1
      2 123
      1 2
      2 abc
      2 -bash: lsaaa: 未找到命令
[[email protected] ~]# sort 2.txt |uniq -c |tee -a a.txt         #-a  追加
      2 1
      2 123
      1 2
      2 abc
      2 -bash: lsaaa: 未找到命令
[[email protected] ~]# cat a.txt
      2 1
      2 123
      1 2
      2 abc
      2 -bash: lsaaa: 未找到命令
      2 1
      2 123
      1 2
      2 abc
      2 -bash: lsaaa: 未找到命令

7.tr命令:替换字符:

[[email protected] ~]# echo "weixlinux"   |tr ‘[wl]‘ ‘[WL]‘
WeixLinux
[[email protected] ~]# echo "weixlinux"   |tr ‘w‘ ‘W‘
Weixlinux
[[email protected] ~]# echo "weixlinux"   |tr ‘[a-z]‘ ‘[A-Z]‘
WEIXLINUX
[[email protected] ~]# echo "weixlinux"   |tr ‘[a-z]‘ ‘1‘
111111111

8.split:切割

-b   按照大小
-l    按照行数

技术分享图片
9.||:前面命令执行成功,后面命令不再执行,如果前面命令执行不成功,后面会执行

[[email protected] ~]# ls 1.txt || wc -l 2.txt
1.txt
[[email protected] ~]# ls 3.txt || wc -l 2.txt
ls: 无法访问3.txt: 没有那个文件或目录
9 2.txt

10.&&:前面命令执行成功,后面才执行

[[email protected] ~]# ls 3.txt && wc -l 2.txt
ls: 无法访问3.txt: 没有那个文件或目录
[[email protected] ~]# ls 1.txt && wc -l 2.txt
1.txt
9 2.txt

以上是关于shell基础(下)的主要内容,如果未能解决你的问题,请参考以下文章

逆向及Bof基础实践

20155307刘浩《网络对抗》逆向及Bof基础

20155311高梓云《网络对抗》逆向及Bof基础

20145301赵嘉鑫《网络对抗》逆向及Bof基础

Linux bash基础特性二

代码片段:Shell脚本实现重复执行和多进程