shell基础入门

shell基础入门

为什么使用shell

安装操作系统(centos)自动化安装系统(kickstart cobbler) 底层为shell

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

  • 安装系统
  • ntp时间同步
  • 更改yum源
  • ssh优化
  • 关闭 selinux
  • 开关 防火墙
  • 安装基础服务(wget vim lrzsz net-tools unzip gzip...)
  • 优化文件描述符
  • 优化字符集

2.安装服务

  • Nginx
  • PHP
  • MySQL
  • Redis
  • MHA
  • Rsync
  • NFS
  • MongoDB
  • Zabbix

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

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

5.监控服务(使用shell)

6.结合定时任务使用shell

7.重复性工作写入脚本

  • 日志切割
  • 日志分析
  • 数据统计
  • 机器巡检
  • 数据备份

shell编程需要掌握的知识

  • 熟练使用vim编辑器

  • 熟悉ssh终端(Xshell、CRT)

  • 熟练掌握linux常用命令

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

如何学好shell编程

  • 环境变量

  • 条件表达式

  • 流程控制语句

  • 循环

  • 数组

  • 函数

学习shell三部曲:

  • 先读懂shell

  • 再修改shell

  • 自己写shell

什么是shell

img

1.交互式shell

交互式模式就是shell等待你的输入,并且执行你提交的命令。这种模式被称作交互式是因为shell与用户进行交互。这种模式也是大多数用户非常熟悉的:登录、执行一些命令、签退。当你签退后,shell也终止了。

2.非交互式shell

shell也可以运行在另外一种模式:非交互式模式。在这种模式下,shell不与你进行交互,而是读取存放在文件中的命令,并且执行它们。当它读到文件的结尾,shell也就终止了。

什么是shell脚本

把在命令行执行的命令放在一个文件里统一执行,称为Shell脚本 包含若干个linux命令、循环语句,条件语句等。

shell脚本规范

  • 目录统一

  • shell脚本的结尾要以.sh结尾 (为了给人看的)

  • 脚本的开头需要有解释器

#!/bin/bash
  • 脚本中需要有作者信息
#!/bin/bash
#Author: _ljy_
#Date: _2023-5-21_
#Name: _Print Message_
  • 必须要加注释(开发规范,运维规范)
  • shell中的文字尽量使用英文
  • 成对的符号和语句一次性写完

vim一个脚本模板

# 1.首先先编辑一个模板文件,该模板文件可以叫任何名字
[root@zabbix01 ~]# vim /usr/share/vim/vimfiles/template.yys
#!/bin/bash
#Author: _ljy_
#Date: _2023-5-21_
#Name: _Test_

# 2.写完之后,我们需要修改一下vim的配置文件
[root@zabbix01 ~]# vim /etc/vimrc
autocmd BufNewFile *.spec 0r /usr/share/vim/vimfiles/template.spec
## 在第28行,autocmd自动保存模板文件,修改一下,因为我们是要写shell脚本的模板
## 所以我们要把*.spec 修改成*.sh
## 然后将后面的模板文件改成你定义的模板文件名
autocmd BufNewFile *.sh 0r /usr/share/vim/vimfiles/template.zls

# 3.接下来,我们编辑所有只要以sh结尾的文件,都会带有作者信息
[root@zabbix01 ~]# vim test_zls.sh

img

优化脚本模板

vim ~/.vimrc

autocmd bufNewFile *.py,*.sh,*.java exec ":call SetTitle()"
func SetTitle()
  if expand("%:e") == 'sh'
      call setline(1, "#!/bin/bash")
      call setline(2, "# File Name: ".expand("%"))
      call setline(3, "# Version: v1.1 ")
      call setline(4, "# Author: nibaba")
      call setline(5, "# Mail: @qq.com ")
      call setline(5, "# Domain: yys ")
      call setline(6, "# Date Time: ".expand(strftime("%Y-%m-%d %H:%M")))
  endif
endfunc

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: __nibaba__ ")
      call setline(6, "# Mail: __@qq.com__ ")
      call setline(7, "# Blog: __yys__ ")
      call setline(8, "# DateTime: __".expand(strftime("%Y-%m-%d %H:%M")) . "__")
  endif
endfunc

img

img

脚本的执行方式

vim 2.sh

#!/bin/bash
# File Name: 2.sh
# Version: v1.1 
# Author: ljy
# Domain: ljy_main 
# Date Time: 2023-06-25 09:09
echo "Hello world"

# 执行脚本
[root@web01 ~]# sh 2.sh 
Hello world

[root@web01 ~]# bash 2.sh 
Hello world

[root@web01 ~]# source 2.sh 
Hello world

# 带有执行权限的脚本
[root@web01 ~]# chmod +x 2.sh 
[root@web01 ~]# ./2.sh 
Hello world

[root@web01 ~]# /root/2.sh 
Hello world

[root@web01 ~]# . 2.sh 
Hello world

. source 都是在父shell(影响黑终端)下执行的

sh bash相对路径绝对路径都是在子shell(不会影响到黑终端)下执行的

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

  • 编译型

    # 编辑代码
    [root@m01 ~]# vim hello.c
    #include 
    
    void main(){
    printf("hello world");
    }
    
    #编译成二进制文件
    [root@m01 ~]# gcc hello.c -o hello.bin
    
    [root@m01 ~]# ll
    总用量 16
    -rwxr-xr-x 1 root root 8440 8月  26 09:40 hello.bin
    -rw-r--r-- 1 root root   60 8月  26 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

    img

  • 解释型

    针对解释器的命令,如果解释器与命令不符合,将无法执行
    
    [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 8月  26 09:40 hello.bin
    -rw-r--r-- 1 root root   60 8月  26 09:39 hello.c
    -rw-r--r-- 1 root root   31 8月  26 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
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇