在计算机科学中,Shell俗称壳(用来区别于核),Shell 是一个 C 语言编写的脚本语言,它是用户与 Linux 的桥梁,用户输入命令交给 Shell 处理, Shell 将相应的操做传递给内核(Kernel),内核把处理的结果输出给用户。简单来讲就是一我的机交互的中间处理器。前端
下面是流程示意图:python
GUI 为 Unix 或者类 Unix 操做系统构造一个功能完善、操做简单以及界面友好的桌面环境。例如:应用最为普遍的是微软的windows系列操做系统,还有也包括广为人知的 Linux shell,其中linux shell ,以及功能更强大的CDE、GNOME、KDE、 XFCE。linux
CLI 是在用户提示符下键入可执行指令的界面,用户经过键盘输入指令,完成一系列操做。 在 Linux 系统上主流的 CLI 实现是 Bash,是许多 Linux 发行版默认的 Shell。还有许多 Unix 上 Shell,例如 tcsh、csh、ash、bsh、ksh 等。正则表达式
Shell 是一个程序,通常都是放在/bin或者/user/bin目录下,当前 Linux 系统可用的 Shell 都记录在/etc/shells文件中。/etc/shells是一个纯文本文件,你能够在图形界面下打开它,也可使用 cat 命令查看它。shell
经过 cat 命令来查看当前 Linux 系统的可用 Shell:数据库
cat /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/nologin /bin/tcsh /bin/csh
在现代的 Linux 上,sh 已经被 bash 代替,/bin/sh每每是指向/bin/bash的符号连接。 编程
若是你但愿查看当前 Linux 的默认 Shell,那么能够输出 SHELL 环境变量:vim
echo $SHELL
/bin/bash
出结果代表默认的 Shell 是 bash。 windows
SHELL是 Linux 系统中的环境变量,它指明了当前使用的 Shell 程序的位置,也就是使用的哪一个 Shell。后端
么是脚本?
脚本简单地说就是一条条的文字命令(一些指令的堆积),这些文字命令是能够看到的(如能够用记事本打开查看、编辑)。
常见的脚本: JavaScript(JS,前端),VBScript, ASP,JSP,PHP(后端),SQL(数据库操做语言),Perl,Shell,python,Ruby,JavaFX, Lua等。
为何要学习和使用shell?
Shell属于内置的脚本
程序开发的效率很是高,依赖于功能强大的命令能够迅速地完成开发任务(批处理)
语法简单,代码写起来比较轻松,简单易学
新建一个一 .sh 结尾的文件打开
编写
# vim test.sh #!/bin/bash echo "Hello world!"
第一行指定解释器,第二行打印 Hello world!
写好后,开始执行,执行 Shell 脚本有三种方法:
也是最经常使用的一种
# bash test.sh Hello world!
# ll test.sh -rw-r--r--. 1 root root 32 Aug 18 01:07 test.sh # chmod +x test.sh # ./test.sh -bash: ./test.sh: Permission denied # chmod +x test.sh # ./test.sh Hello world!
这种方式默认根据脚本第一行指定的解释器处理,若是没写以当前默认 Shell 解释器执行。
注意,这里在运行时必定要写成 ./test.sh(绝对路径亦可),而不是 test.sh,运行其它二进制的程序也同样,直接写 test.sh,Linux 系统会去 PATH(环境变量) 里寻找有没有叫 test.sh 的,而只有 /bin, /sbin, /usr/bin,/usr/sbin 等在 PATH 里,你的当前目录一般不在 PATH 里,因此写成 test.sh 是会找不到命令的,要用 ./test.sh 告诉系统说,就在当前目录找。
# source test.sh
Hello world!
咱们可使用命令pstree查看咱们当前所处的位置
须要下载
[root@ken ~]# yum search pstree Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile =================== Matched: pstree ======================= psmisc.x86_64 : Utilities for managing processes on your system texlive-pst-tree.noarch : Trees, using pstricks [root@ken ~]# yum install psmisc -y
使用pstree
咱们再次执行bash,就会进入到另一个子shell中
这个时候咱们在这个子shell中定义一个变量,发现能够正确打印出来
[root@ken ~]# age=25 [root@ken ~]# echo $age 25
如今咱们退出当前的shell,即进入了当前子shell中的父shell中,再次打印咱们刚才定义的变量
能够发现如今已经没法获取到咱们刚才定义的变量值了。
咱们把环境变量定义到profile的一个子文件中,并使用source执行该文件并生效
打开一个子shell,定义在父shell中的环境变量依然有效
[root@ken ~]# vim /etc/profile.d/ken.sh export name=ken #写入全局变量 [root@ken ~]# source /etc/profile.d/ken.sh [root@ken ~]# echo $name ken [root@ken ~]# bash [root@ken ~]# echo $name ken
环境变量可用于定义shell的运行环境,环境变量能够在配置文件中定义与修改,也能够在命令行中设置,可是命令行中的修改操做在终端重启时就会丢失,所以最好在配置文件中修改——全局配置 “/etc/profile“ 或在它的子配置文件 “/etc/profile.d” 中定义。(推荐在子配置文件中进行配置不会对系统产生较大影响)将环境变量放在profile文件中,每次用户登陆时这些变量值将被初始化。好比HOME、USER、SHELL、UID等再用户登陆以前就已经被/bin/login程序设置好了。
本地变量在用户当前的shell生存期的脚本中使用。在一个函数中将某个变量声明为local,则该变量就是一个局部变量,只在本函数中有效。
环境变量能够在定义它们的shell及其派生出来的任意子进程的shell中使用。局部变量只能在定义它们的函数/脚本中使用。子shell和父shell互不继承局部变量,但子shell继承父shell的全局变量
其实shell就是常见命令写在一块儿就变成了了脚本文件。这里介绍几个在shell里经常使用的命令
过滤来自一个文件或标准输入匹配模式内容。
除了 grep 外,还有 egrep。egrep 是 grep 的扩展,至关于 grep -E
grep <options> <参数> ...... <file>
选项 | 描述 |
-E,--extended-regexp |
模式是扩展正则表达式(ERE) |
-i,--ignore-case | 忽略大小写 |
-n,--line-number | 打印行号 |
-o,--only-matching | 只打印匹配的内容 |
-c,--count | 只打印每一个文件匹配的行数 |
-B,--before-context=NUM | 打印匹配的前几行 |
-A,--after-context=NUM | 打印匹配的后几行 |
-C,--context=NUM |
打印匹配的先后几行 |
--color[=WHEN], |
匹配的字体颜色 |
-v,--invert-match |
打印不匹配的行 |
例如
[root@ken ~]# echo "this is ken THIS IS KEN" | grep -i 'ken' this is ken THIS IS KEN [root@ken ~]# echo "this is ken THIS IS KEN" | grep 'ken' this is ken THIS IS KEN
[root@ken ~]# grep -n 'root' /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin
[root@ken ~]# echo "this is ken THIS IS KEN" | grep -o 'ken' ken [root@ken ~]# echo "this is ken THIS IS KEN" | grep 'ken' this is ken THIS IS KEN
[root@localhost /]# cat 1.txt 1111111 1111111 2222222 2222222 1111111 2222222 3333333 1111111 [root@localhost /]# grep "11" -c 1.txt 4 [root@localhost /]# grep "2" -c 1.txt 3 [root@localhost /]#
[root@localhost /]# cat 1.txt 000 111 222 333 444 555 666 [root@localhost /]# grep -B 2 "3" 1.txt 111 222 333
[root@localhost /]# cat 1.txt 000 111 222 333 444 555 666 [root@localhost /]# grep -A 2 "3" 1.txt 333 444 555
[root@localhost /]# cat 1.txt 000 111 222 333 444 555 666 [root@localhost /]# grep -C 2 "3" 1.txt 111 222 333 444 555
[root@localhost /]# cat 1.txt 000 111 222 333 444 555 666 [root@localhost /]# grep -v 111 1.txt 000 222 333 444 555 666 [root@localhost /]#
cut [options] [file]
cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段写至标准输出。
若是不指定 File 参数,cut 命令将读取标准输入。必须指定 -b、-c 或 -f 标志之一。
经常使用参数:
-c :以字符为单位进行分割。
-d :自定义分隔符,但只能为一个字符长
-f :与-d一块儿使用,指定显示哪一个区域。
一、-c
[root@ken ~]# echo "this is ken" | cut -c 4 s [root@ken ~]# echo "this is ken" | cut -c 5 [root@ken ~]# echo "this is ken" | cut -c 2 h [root@ken ~]# echo "this is ken" | cut -c 1-5 this
二、-d ,-f 一般连用
从标准中读取
[root@songqi ~]# echo 'this is ken' | cut -d ' ' -f 3 ken [root@songqi ~]# echo "name:ken age:25 gender:male" | cut -d ' ' -f2 age:25
从文件中读取
[root@ken ~]# echo "this is ken">test [root@ken ~]# cut -d' ' -f3 test ken
Linux sort命令用于将文本文件内容加以排序。
sort可针对文本文件的内容,以行为单位来排序。
sort [-bcdfimMnr][-o<输出文件>][-t<分隔字符>][+<起始栏位>-<结束栏位>][--help][--verison][文件]
在使用sort命令以默认的式对文件的行进行排序,使用的命令以下:
[root@ken ~]# cat test 6 this is ken 2 this is ken 5 this is ken 6 this is ken 1 this is ken 8 this is ken [root@ken ~]# sort test 1 this is ken 2 this is ken 5 this is ken 6 this is ken 6 this is ken 8 this is ken
sort 命令将以默认的方式将文本文件的第一列以ASCII 码的次序排列,并将结果输出到标准输出。
[root@ken ~]# sort -r test | head -3 8 this is ken 6 this is ken 6 this is ken
[root@ken ~]# sort -t 'e' -k 2 test 6 this:is:ke1 5 this:is:ke2 6 this:is:ke3 2 this:is:ke4 1 this:is:ke6 8 this:is:ke7 [root@ken ~]# sort -t 'e' -k 2 -r test 8 this:is:ke7 1 this:is:ke6 2 this:is:ke4 6 this:is:ke3 5 this:is:ke2 6 this:is:ke1
[root@ken ~]# sort test 1 this:is:ke6 2 this:is:ke4 2 this:is:ke4 2 this:is:ke4 2 this:is:ke4 5 this:is:ke2 6 this:is:ke1 6 this:is:ke3 8 this:is:ke7 [root@ken ~]# sort -u test 1 this:is:ke6 2 this:is:ke4 5 this:is:ke2 6 this:is:ke1 6 this:is:ke3 8 this:is:ke7
去除重复的行(相邻且相同,认定为重复)
-c:在行首用数字表示该行出现了多少次
-u:仅仅显示那些没有出现重复过的行
[root@ken ~]# uniq -c test 1 6 this:is:ke3 4 2 this:is:ke4 1 5 this:is:ke2 1 6 this:is:ke1 1 1 this:is:ke6 1 8 this:is:ke7
[root@ken ~]# sort test | uniq 1 this:is:ke6 2 this:is:ke4 5 this:is:ke2 6 this:is:ke1 6 this:is:ke3 8 this:is:ke7
Linux tr 命令用于转换或删除文件中的字符。
tr 指令从标准输入设备读取数据,通过字符串转译后,将结果输出到标准输出设备。
a-z 任意小写
A-Z 任意大写
0-9 任意数字
[root@ken ~]# echo "this is ken" | tr a-z A-Z THIS IS KEN [root@ken ~]# echo "THIS IS KEN" | tr A-Z a-z this is ken
s这个字符串都会被删掉
[root@ken ~]# cat test 6 this:is:ke3 2 this:is:ke4 2 this:is:ke4 2 this:is:ke4 2 this:is:ke4 5 this:is:ke2 6 this:is:ke1 1 this:is:ke6 8 this:is:ke7 [root@ken ~]# cat test | tr -d 's' 6 thi:i:ke3 2 thi:i:ke4 2 thi:i:ke4 2 thi:i:ke4 2 thi:i:ke4 5 thi:i:ke2 6 thi:i:ke1 1 thi:i:ke6 8 thi:i:ke7