一、通过子shell运行命令,并返回当前目录
代码如下:
(cd /etc && ls -a)
该命令会输出/etc目录的内容,然而,你的工作目录并没有更换为/etc目录
二、shell 多命令逻辑执行顺序
shell下可以使用“;”、“&&”、“||”、“&”来连接多个命令
1.分号
cmd1;cmd2
命令之间没有逻辑关系。分号连接的命令会按照顺序从前向后依次执行,但分号两端的命令之间没有任何逻辑关系,所有写出来的命令最终都会被执行,即使分号前面的命令出错也不影响后面的命令
例如:
[root@xuexi ~]# ls das;echo "hdakl"
ls: cannot access das: No such file or directory
hdakl
2.&&
cmd1 && cmd2
逻辑与。&&连接的命令会按照顺序从前向后执行,但只有当command1正确执行才执行command2,如果command1不正确执行,则不执行command2。在bash中,通过预定义变量$?来判断命令是否正确执行,如果$?的值为0则表示前一条命令正确执行,其他任意值都表示不正确执行。
例如:
[root@xuexi ~]# echo "hdakl" && ls ds
hdakl
ls: cannot access ds: No such file or directory
[root@xuexi ~]# ls das && echo "hdakl"
ls: cannot access das: No such file or directory
3.||
cmd1 || cmd2
逻辑或。||连接的命令会按照顺序从前向后执行,但只有当command1不正确执行才执行command2,command1正确执行则不会执行command2。||和&&都是短路符号,符号左右的命令之间具有逻辑关系。
例如:
[root@xuexi ~]# ls das || echo "hdakl"
ls: cannot access das: No such file or directory
hdakl
[root@xuexi ~]# echo "hdakl" || ls ds
hdakl
4.&
command1 &
command1 & command2
&表示将其前面的命令放入后台执行,放入后台后会立即返回到bash环境让用户可以继续和bash交互。如果&符号连接了两个命令,则其前面的命令被放入后台,立即执行后面的命令,所以可以简单地认为这两个命令是并行执行的,两端的命令之间也没有任何逻辑关系。
需要注意的一点是,在终端的bash环境下,子shell中的后台的进程不受终端控制,在终端被关闭时它会挂靠在init/systemd进程下,因此退出终端或脚本shell环境,无法中断这些后台进程。例如:
[root@xuexi ~]# (sleep 10 &) # 终端1上执行,立即关闭该终端
[root@xuexi ~]# ps aux | grep slee[p] # 终端2上捕捉sleep进程
root 5732 0.0 0.0 107892 624 ? S 00:28 0:00 sleep 10
三、tac、tr、look、sort、cut
1. tac
tac命令用于将文件已行为单位的反序输出,即第一行最后显示,最后一行先显示
2. tr
tr命令可以对来自标准输入的字符进行替换、压缩和删除。它可以将一组字符变成另一组字符,经常用来编写优美的单行命令
-c或——complerment:取代所有不属于第一字符集的字符;
-d或——delete:删除所有属于第一字符集的字符;
-s或--squeeze-repeats:把连续重复的字符以单独一个字符表示;
-t或--truncate-set1:先删除第一字符集较第二字符集多出的字符。
例如:
echo "hello 123 world 456" | tr -d '0-9' hello world
hello world
3. look
look命令用于显示文件中以指定字符串开头的任意行
look 字符串 指定文件
4. sort
短参数 长参数 说明
-n –number-sort 按字符串数值排序,与-g区别为不转为浮点数
-g –general-number-sort 按通用数值排序,支持科学计数法
-f –ignore-case 忽略大小写,默认大小写字母不同
-k –key=POS1[,POS2] 排序从POS1开始,若指定POS2,则POS2结束,否则以pos1排序
-t –field-separator=SEP 指定列的分割符
-r –reverse 降序排序,默认为升序
-h –human-numeric-sort 使用易读性数字(例如: 2K 1G)
-u –unique 去除重复的行
-o –output=FILE 将输出写入文件
a、例如,指定列排序:
sort排序的时候,可以按字段分割的数据进行排序。-t参数表示行的分割字符,-k表示第几列。当然,可以进行降序排序,-r参数可以实现。
下面是对passwd文件,以冒号(:)进行分割,然后对第三列以数字方式进行降序排序。
shuanghu@shuanghu:etc$ cat passwd
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
shuanghu@shuanghu:etc$ sort -t ':' -k 3 -nr passwd
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
games:x:5:60:games:/usr/games:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
sys:x:3:3:sys:/dev:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
b、将sort输出内容写入文件:
在shell中,一般将控制台内容写入文件,可以使用重定向,但如果想把sort的排序内容写回文件,则不能使用重定向。则需要-o参数。具体如下
shuanghu@shuanghu:tmp$cat word.txt
one
two
three
four
shuanghu@shuanghu:tmp$sort word.txt > word.txt
shuanghu@shuanghu:tmp$cat word.txt #输出为空
shuanghu@shuanghu:tmp$sort word.txt -o word.txt
shuanghu@shuanghu:tmp$sort -u word.txt
four
one
three
two
c、系统进程内存占用排序
查看系统进程中,内存占用最多的前5个进程信息
shuanghu@shuanghu:tmp$ ps aux|sort -gr -k 4|head -n 5
shuanghu 1740 15.7 4.6 1506764 189872 ? Sl 5月07 142:08 compiz
root 1304 2.1 1.9 338928 80208 tty7 Ssl+ 5月07 19:29 /usr/bin/X -core :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
shuanghu 1933 0.0 1.1 1074520 46708 ? Sl 5月07 0:00 /usr/lib/evolution/evolution-calendar-factory
shuanghu 1833 0.0 0.8 974900 34468 ? Sl 5月07 0:01 nautilus -n
shuanghu 2111 0.0 0.6 655712 24920 ? Sl 5月07 0:16 gnome-terminal
5. cut
cut命令用来显示行中的指定部分,删除文件中指定字段
-b:仅显示行中指定直接范围的内容;
-c:仅显示行中指定范围的字符;
-d:指定字段的分隔符,默认的字段分隔符为“TAB”;
-f:显示指定字段的内容;
-n:与“-b”选项连用,不分割多字节字符;
例如:
[root@localhost text]# cat test.txt
No Name Mark Percent
01 tom 69 91
02 jack 71 87
03 alex 68 98
使用 -f 选项提取指定字段:
[root@localhost text]# cut -f 1 test.txt
No
01
02
03
[root@localhost text]# cut -f2,3 test.txt
Name Mark
tom 69
jack 71
alex 68
--complement 选项提取指定字段之外的列(打印除了第二列之外的列):
[root@localhost text]# cut -f2 --complement test.txt
No Mark Percent
01 69 91
02 71 87
03 68 98
使用 -d 选项指定字段分隔符:
[root@localhost text]# cat test2.txt
No;Name;Mark;Percent
01;tom;69;91
02;jack;71;87
03;alex;68;98
[root@localhost text]# cut -f2 -d";" test2.txt
Name
tom
jack
alex
cut 指定字段的字符或者字节范围
cut命令可以将一串字符作为列来显示,字符字段的记法:
N-:从第N个字节、字符、字段到结尾;
N-M:从第N个字节、字符、字段到第M个(包括M在内)字节、字符、字段;
-M:从第1个字节、字符、字段到第M个(包括M在内)字节、字符、字段。
上面是记法,结合下面选项将摸个范围的字节、字符指定为字段:
-b 表示字节; -c 表示字符; -f 表示定义字段。
例如:
[root@localhost text]# cat test.txt
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
打印第1个到第3个字符:
[root@localhost text]# cut -c1-3 test.txt
abc
abc
abc
abc
abc
打印前2个字符:
[root@localhost text]# cut -c-2 test.txt
ab
ab
ab
ab
ab
四、column命令
The column utility formats its input into multiple columns:
The options are as follows:
-c Output is formatted for a display columns wide.
-s Specify a set of characters to be used to delimit columns for the -t option.
-t Determine the number of columns the input contains and create a table. Columns are delimited with whitespace, by default, or with the characters supplied using the -s option. Useful for pretty-printing displays.
-x Fill columns before filling rows
例如:
[root@localhost tmptest]# cat /etc/passwd | column -t -s :
root x 0 0 root /root /bin/bash
bin x 1 1 bin /bin /sbin/nologin
daemon x 2 2 daemon /sbin /sbin/nologin
adm x 3 4 adm /var/adm /sbin/nologin