Shell编程-入门

本文介绍了Shell编程在操作系统自动化、服务安装、脚本自动化、监控和服务管理等方面的重要作用。详细讲解了Shell的基础知识,如环境变量、条件表达式、流程控制,并提供了学习路径建议。此外,还强调了脚本规范,包括统一目录、文件名后缀、解释器声明、作者信息、注释和执行权限。文章最后讨论了编译型和解释型语言的区别,并通过示例展示了Shell脚本的执行方式。
摘要由CSDN通过智能技术生成

Shell编程-入门

为什么要使用shell

1.安装操作系统(CentOS)自动化安装操作系统(kickstart cobbler)底层shell

2.初始化/优化操作系统

1)ntp时间同步

2)更改默认yum源

3)ssh优化

4)关闭Selinux

5)关闭/开启 防火墙(C6:iptables C7:firewalld)

6)安装基础服务(wget vim lrzsz net-tools unzip gzip…)

7)优化文件描述符

8)优化字符集

9)…

3.安装服务

1)Nginx

2)PHP

3)MySQL

4)Redis

5)MHA

6)Rsync

7)NFS

8)MongoDB

9)Zabbix …

4.启动服务(系统默认的shell脚本)

5.脚本实现自动化代码上线

6.监控服务(使用shell)

7.结合定时任务使用shell

8.重复性工作写入脚本

1)日志切割

2)日志分析

3)数据统计

4)机器巡检

5)数据备份

shell编程和基础知识

1)熟练使用vim编辑器

2)熟悉ssh终端(Xshell、CRT)

3)熟练掌握linux常用命令

4)熟练掌握linux正则表达式及三剑客命令

如何学好shell编程

1.环境变量

2.条件表达式

3.流程控制语句

4.循环

5.数组

6.函数

学习shell三部曲:

先读懂shell

再修改shell

自己写shell

找一本合适的教材、或者自己认真做的较为全面的笔记

大忌:不可拿来主义,可以模仿,但是要自己嚼烂了在吃下

学完shell 可解决企业中大部分脚本问题

shell脚本的规范

  • 统一目录
  • shell脚本的结尾要以.sh结尾
  • 脚本开头要有解释器
#!/bin/bash
  • 脚本中要有作者的信息
#!/bin/bash

# Author:_DrierZeng_
# Date: _2021-09-06_
# Mail: -123@qq.com_
# Blog: _www.wjf.com_
# Name: _这是干什么的_
  • 脚本必须要加注释(开发规范,运维规范)
  • shell脚本中的尽量用英文,不要用中文
  • 成对的符号和语句一次性写完
if [ ];then 
fi
''
()
{}

vim模板

#1.编辑模板
[root@m01 ~]# vim /usr/share/vim/vimfiles/template.zls
  
#!/bin/bash
# File Name: __zls_test.sh__
# Version: __v1.1__
# Author: __DriverZeng__
# Mail: __133411023@qq.com__
# Blog: __https://blog.driverzeng.com__
# DateTime: __2021-09-06 09:55__

#2.修改环境变量
[root@m01 ~]# vim /etc/vimrc
autocmd BufNewFile *.sh 0r /usr/share/vim/vimfiles/template.zls

自定义vim函数

[root@m01 ~]# vim ~/.vimrc 
  
autocmd bufNewFile *.py,*.sh,*.java exec ":call SetTitle()"
func SetTitle()
  if expand("%:e") == 'sh'
      call setline(1, "#!/bin/bash")
      call setline(2, "")
      call setline(3, "# File Name: __".expand("%") . "__")
      call setline(4, "# Version: __v1.1__ ")
      call setline(5, "# Author: __DriverWang__ ")
      call setline(6, "# Mail: __372359276@qq.com__ ")
      call setline(7, "# Blog: __https://blog.wjf.com__ ")
      call setline(8, "# DateTime: __".expand(strftime("%Y-%m-%d %H:%M")) . "__")
  endif
endfunc

脚本的执行方式

#!/bin/bash

# File Name: __a.sh__
# Version: __v1.1__ 
# Author: __DriverWang__ 
# Mail: __372359276@qq.com__ 
# Blog: __https://blog.wjf.com__ 
# DateTime: __2021-09-06 16:18__
echo 'hello world

[root@m01 ~]# sh zls_test.sh
hello world
[root@m01 ~]# bash zls_test.sh
hello world

# 必须要有执行权限
[root@m01 ~]# ./zls_test.sh
-bash: ./zls_test.sh: Permission denied

[root@m01 ~]# /root/zls_test.sh
-bash: /root/zls_test.sh: Permission denied

[root@m01 ~]# chmod +x zls_test.sh
[root@m01 ~]# ./zls_test.sh
hello world
[root@m01 ~]# /root/zls_test.sh
hello world

## 脚本的引用
[root@m01 ~]# source zls_test.sh
[root@m01 ~]# . zls_test.sh

开发语言中程序代码的分类

编译型

写完后,需要编译才能运行。开发书写的代码,无法直接运行,需要编译(Java、C语言、等)

# 编辑代码
[root@m01 ~]# vim hello.c
#include <stdio.h>
 
void main(){
  printf("hello world");
}
 
#编译成二进制文件
[root@m01 ~]# gcc hello.c -o hello.bin
 
 
[root@m01 ~]# ll
总用量 16
-rwxr-xr-x 1 root root 8440 826 09:40 hello.bin
-rw-r--r-- 1 root root   60 826 09:39 hello.c
 
[root@m01 ~]# file hello.bin
hello.bin: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=c248ed9f695c05ece7d46731556e86eb0ac6bc11, not stripped
 
[root@m01 ~]# ./hello.bin
hello world

解释型

书写完成,通过命令解释器运行。书写完成后,使用对应的命令解释器(shell、python、等)

[root@m01 ~]# vim hello.sh
[root@m01 ~]# cat hello.sh
#!/bin/bash
echo 'hello world'
 
[root@m01 ~]# ll
总用量 20
-rwxr-xr-x 1 root root 8440 826 09:40 hello.bin
-rw-r--r-- 1 root root   60 826 09:39 hello.c
-rw-r--r-- 1 root root   31 826 09:44 hello.sh
[root@m01 ~]# file hello.sh
hello.sh: Bourne-Again shell script, ASCII text executable
[root@m01 ~]# sh hello.sh
hello world

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值