技术流ken

运维拯救世界

DAY18: 课堂记录–shell编程一 –技术流ken

shell脚本命名

 

1.见名知意

2. 结尾.sh

 

shell脚本编写

 

#!/bin/bash                           #声明shell解释器
echo “Hello World”               #执行的command

 

shell脚本的运行

 

第一种方式:bash

[root@ken ~]# bash hello_w.sh
Hello World

 

第二种方式:路径方式  #要给脚本添加执行权限

[root@ken ~]# ./hello_w.sh
-bash: ./hello_w.sh: Permission denied

 

[root@ken ~]# ./hello_w.sh
-bash: ./hello_w.sh: Permission denied
[root@ken ~]# ll
total 102412
-rw——-. 1 root root 1206 Feb 26 22:25 anaconda-ks.cfg
-rw-r–r– 1 root root 0 Mar 11 19:08 a.out
-rw-r–r– 1 root root 31 May 29 09:55 hello_w.sh
drwxr-xr-x 3 root root 80 Mar 11 18:18 ken1
lrwxrwxrwx 1 root root 4 Mar 12 11:06 test1 -> test
-rw-r–r– 1 root root 4 Mar 12 11:09 test2
-rw-r–r– 1 root root 104857600 Mar 12 18:03 test.txt
[root@ken ~]# chmod +x hello_w.sh
[root@ken ~]# ll
total 102412
-rw——-. 1 root root 1206 Feb 26 22:25 anaconda-ks.cfg
-rw-r–r– 1 root root 0 Mar 11 19:08 a.out
-rwxr-xr-x 1 root root 31 May 29 09:55 hello_w.sh
drwxr-xr-x 3 root root 80 Mar 11 18:18 ken1
lrwxrwxrwx 1 root root 4 Mar 12 11:06 test1 -> test
-rw-r–r– 1 root root 4 Mar 12 11:09 test2
-rw-r–r– 1 root root 104857600 Mar 12 18:03 test.txt
[root@ken ~]# ./hello_w.sh
Hello World

 

第三步:方式

[root@ken ~]# source hello_w.sh
Hello World
[root@ken ~]# chmod -x hello_w.sh
[root@ken ~]# source hello_w.sh
Hello World

 

练习1:使用root用户帐号创建并执行test2.sh,实现创建一个shelltest用户,并在其家目录中新建文件try.html。

[root@ken ~]# cat test2.sh
#!/bin/bash
useradd shelltest
touch /home/shelltest/try.html

 

执行结果:

[root@ken ~]# id shelltest
uid=1001(shelltest) gid=1001(shelltest) groups=1001(shelltest)
[root@ken ~]# ls /home/shelltest/
try.html

 

练习2:统计当前系统总共有多少用户

[root@ken ~]# cat test3.sh
#!/bin/bash
wc -l /etc/passwd

 

执行结果:

[root@ken ~]# bash test3.sh
22 /etc/passwd

 

练习3:统计当前已经安装的软件数量

[root@ken ~]# cat test4.sh
#!/bin/bash
yum list installed | wc -l

 

执行结果:

[root@ken ~]# bash test4.sh
353

 

 

shell脚本常用命令

 

1.grep: 过滤文本文件内容

 

常用选项:

                 选项                   描述
-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 ~]# cat test
dlakdlad
ad
ad
a
dFSAF
A
F
F
AS
F
f
sf
as
f

 

 

实例1:打印出所有的a无论大小写 : -i选项

[root@ken ~]# grep -i “a” test
dlakdlad
ad
ad
a
dFSAF
A
AS
as

 

实例2:打印出所有的a无论大小写,并且显示该字符串所在的行 : -n选项

[root@ken ~]# grep -i -n “a” test
1:dlakdlad
2:ad
3:ad
4:a
5:dFSAF
6:A
9:AS
13:as

 

实例3:仅仅打印出所有匹配的字符串: -o选项

[root@ken ~]# grep -i -o “a” test
a
a
a
a
a
A
A
A
a

 

实例4:打印出匹配的字符串有多少行 -c选项

[root@ken ~]# grep -i -c “a” test
8

 

实例5:打印出字符S前面的2行 -B

[root@ken ~]# grep -B 2 “S” test
ad
a
dFSAF

F
F
AS

 

实例6:打印出字符S后面的2行 -A

[root@ken ~]# grep -A 2 “S” test
dFSAF
A
F

AS
F
f

 

实例7:打印出字符S前后2行 -C

[root@ken ~]# grep -C 2 “S” test
ad
a
dFSAF
A
F
F
AS
F
f

 

实例8:打印出不包含大小s的所有行  取反 -v

[root@ken ~]# grep -i -v “s” test
dlakdlad
ad
ad
a
A
F
F
F
f
f

 

grep可以从文件当中直接搜索某个关键词

也可以从标准输入里面搜错

[root@ken ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@ken ~]# cat /etc/passwd | grep “root”
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

 

 

2.cut

  • -c :以字符为单位进行分割。
  • -d :自定义分隔符。
  • -f :与-d一起使用,指定显示哪个区域。

 

实例1:获取6位随机加密数

[root@ken ~]# echo $RANDOM | md5sum | cut -c 1-6
bb2788
[root@ken ~]# echo $RANDOM | md5sum | cut -c 1-6
45a0e0

 

实例2:截取指定的字符串

[root@ken ~]# echo “abcd” | cut -c 2
b

 

实例3:截取出来/etc/passwd用户名

[root@ken ~]# cat /etc/passwd | cut -d “:” -f 1
root
bin
daemon
adm
lp
sync
shutdown
halt

 

实例4:截取出来/etc/passwd用户的shell类型

[root@ken ~]# cat /etc/passwd | cut -d “:” -f 7
/bin/bash
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/bin/sync
/sbin/shutdown

 

实例5:截取出来/etc/passwd用户以及shell类型

[root@ken ~]# cat /etc/passwd | cut -d “:” -f 1,7
root:/bin/bash
bin:/sbin/nologin
daemon:/sbin/nologin
adm:/sbin/nologin
lp:/sbin/nologin
sync:/bin/sync
shutdown:/sbin/shutdown
halt:/sbin/halt

 

 

3.sort

  • -k:根据切割后的那一段进行排序
  • -n 依照数值的大小排序(默认是根据字符进行排序)。
  • -r 以相反的顺序来排序。
  • -t<分隔字符> 指定排序时所用的栏位分隔字符。
  • -u:去除重复的行(只要那个指定的字段重复,就认定是重复的行)

演示文件

[root@ken ~]# cat test
12
23
2
3
5

 

实例1:以数字排序 -n

[root@ken ~]# sort test
12
2
23
3
5
[root@ken ~]# sort -n test
2
3
5
12
23

 

实例2:降序排序

[root@ken ~]# sort -n -r test
23
12
5
3
2

 

实例3:降序排序并取出前3行

[root@ken ~]# sort -n -r test | head -3
23
12
5

 

演示文件

[root@ken ~]# cat test
12:3
23:4
2:12
3:5
5:23

 

实例4:以冒号为分隔符,第二列进行数字降序排序

[root@ken ~]# cat test | sort -t “:” -k2 -n -r
5:23
2:12
3:5
23:4
12:3

 

 

演示文本:

[root@ken ~]# cat test
12:3
12:3
12:3
12:3
23:4
2:12
3:5
3:5
3:5
3:5
5:23

 

实例5:显示行并去除重复行

[root@ken ~]# sort -u test
12:3
2:12
23:4
3:5
5:23

 

4. uniq

-c:在行首用数字表示该行出现了多少次

-u:仅仅显示那些没有出现重复过的行

 

演示文件

[root@ken ~]# cat test
12:3
12:3
12:3
12:3
23:4
2:12
3:5
3:5
3:5
3:5
5:23

 

实例1:统计每行出现的次数

[root@ken ~]# uniq -c test
4 12:3
1 23:4
1 2:12
4 3:5
1 5:23

 

实例2:演示没有出现出重复行的数量

[root@ken ~]# uniq -u test
23:4
2:12
5:23

 

5,seq

 

实例1:

[root@ken ~]# seq 5
1
2
3
4
5

 

实例2:

[root@ken ~]# seq 2 5
2
3
4
5

 

实例3:

[root@ken ~]# seq 2 2 10  #第一个2是起始位,第二个2是步长,第三个10是终止位
2
4
6
8
10

 

实例4:取出100以内所有的奇数

[root@ken ~]# seq 1 2 100
1
3
5
7
9
11
13
15
17

 

实例5:取出100以内所有的偶数

[root@ken ~]# seq 2 2 100
2
4
6
8
10
12
14
16

 

6.tr命令:命令用于转换或删除文件中的字符

-d:删除指定的字符串

实例1:把所有的小写字母都转化为大写字母

[root@ken ~]# echo “daadadfaedqdac” | tr a-z A-Z
DAADADFAEDQDAC

 

实例2:把所有的大写字母都转化为小写字母

[root@ken ~]# echo “SDSDA2WZFAF” | tr A-Z a-z
sdsda2wzfaf

 

实例3:删除一行中的空格

[root@ken ~]# echo “ada dada dada” | tr -d ” ”
adadadadada

10 thoughts on “DAY18: 课堂记录–shell编程一 –技术流ken

  1. Can I just say what a aid to search out somebody who truly knows what theyre talking about on the internet. You positively know how to convey an issue to gentle and make it important. Extra folks need to read this and perceive this side of the story. I cant consider youre no more standard because you undoubtedly have the gift.

  2. I am only commenting to let you understand of the incredible experience my friend’s girl had reading your blog. She discovered plenty of things, including what it’s like to possess a great teaching spirit to let men and women with ease have an understanding of various hard to do subject areas. You really surpassed people’s expectations. Thanks for displaying the essential, healthy, edifying and as well as fun tips about the topic to Sandra.

  3. I definitely wanted to construct a small message so as to appreciate you for these amazing steps you are showing on this website. My considerable internet lookup has at the end of the day been recognized with excellent know-how to go over with my visitors. I would point out that we visitors actually are very much blessed to exist in a wonderful network with so many perfect individuals with useful tricks. I feel rather privileged to have discovered your entire webpages and look forward to tons of more exciting minutes reading here. Thank you once again for a lot of things.

  4. I simply wished to appreciate you all over again. I’m not certain the things I might have sorted out without the entire thoughts contributed by you concerning such a concern. It was an absolute horrifying scenario for me, but encountering a new specialised avenue you resolved the issue forced me to leap over delight. I will be thankful for your service and in addition expect you find out what a great job that you are carrying out instructing the mediocre ones by way of your website. Probably you haven’t got to know any of us.

  5. I simply wanted to post a quick word to express gratitude to you for some of the wonderful secrets you are showing at this website. My time-consuming internet lookup has at the end of the day been paid with extremely good concept to exchange with my two friends. I would tell you that we visitors actually are undeniably endowed to exist in a remarkable website with many awesome people with beneficial tricks. I feel very fortunate to have encountered your web pages and look forward to tons of more excellent times reading here. Thank you once again for all the details.

  6. I would like to show some thanks to you for rescuing me from this particular scenario. Because of surfing throughout the search engines and obtaining techniques that were not pleasant, I figured my life was over. Existing without the solutions to the problems you have fixed by means of your post is a crucial case, as well as the kind which might have in a negative way damaged my career if I had not noticed your blog. Your actual know-how and kindness in touching a lot of stuff was helpful. I am not sure what I would have done if I hadn’t come across such a solution like this. I am able to now look forward to my future. Thank you very much for the specialized and effective help. I won’t think twice to recommend the blog to anyone who needs counselling about this situation.

  7. Thank you a lot for giving everyone a very brilliant chance to read from here. It is often so pleasant and as well , full of amusement for me personally and my office peers to visit your blog more than thrice every week to find out the latest tips you have got. And of course, we are certainly pleased with all the very good points you serve. Some 1 ideas in this post are unequivocally the most effective I have had.

  8. I wanted to send you one bit of observation to thank you very much over again for the amazing tactics you have featured above. It was certainly shockingly open-handed of people like you in giving openly what exactly some people could have supplied as an ebook to help with making some cash on their own, certainly considering that you could possibly have tried it if you ever desired. Those thoughts as well acted as a fantastic way to know that other people online have the identical fervor the same as my own to learn more pertaining to this issue. I am sure there are numerous more pleasurable moments ahead for individuals who read carefully your blog post.

  9. My spouse and i ended up being very thrilled when Louis managed to do his homework while using the precious recommendations he made through your blog. It’s not at all simplistic to simply choose to be giving for free tips and hints which usually many others might have been trying to sell. And we also realize we need the website owner to give thanks to because of that. The entire illustrations you’ve made, the straightforward website menu, the friendships you can give support to engender – it is mostly spectacular, and it’s really making our son and our family imagine that this idea is cool, which is certainly rather essential. Thank you for everything!

  10. I simply wanted to construct a small comment in order to say thanks to you for some of the pleasant tips you are giving out on this site. My time consuming internet search has at the end of the day been compensated with high-quality ideas to go over with my pals. I ‘d suppose that most of us visitors actually are very much lucky to dwell in a superb community with many marvellous professionals with insightful things. I feel somewhat lucky to have used your entire web pages and look forward to some more enjoyable moments reading here. Thanks a lot once again for a lot of things.

发表评论

邮箱地址不会被公开。