【Linux學習】shell腳本語言

shell 是操作系統中“提供使用者使用界面”的軟件,它包在 linux 內核的外面,為用戶和內核之間的交互提供了一個接口,系統中的命令用 shell 去解釋,shell 接收系統回應的輸出并顯示其到屏幕中。

1.shell簡介

  • 解釋性語言——shell腳本python,運行效率低,基本只適用企業內部
    shell——腳本,記錄系統命令及命令執行的系統關系,充當解釋器
    gcc ——編譯器
    vim ——編輯器
#!/bin/bash??????????幻數,指定解釋器#!/usr/bin/env?bash??自動匹配解釋器
  • 描述性語言——c語言Java,執行效率高

1.腳本的調用
腳本(一般以.sh結尾):

[root@desktop5?mnt]#?vim?westos.sh#!/bin/bash?echo?hello?westos

方法一:無執行權限,用sh調用

[root@desktop5?mnt]#?sh?westos.sh

方法二:有執行權限,用絕對路徑調用

[root@desktop5?mnt]#?chmod?+x?westos.sh?[root@desktop5?mnt]#?/mnt/westos.sh

2.腳本的檢查


 + 表示:執行動作

無+表示:動作輸出


方法一:

[root@desktop5?mnt]#?sh?-x?/mnt/westos.sh

【Linux學習】shell腳本語言
方法二:

[root@desktop5?mnt]#?vim?westos.sh#!/bin/bash?-xecho?hello?westos

【Linux學習】shell腳本語言


實驗一:快捷鍵F4執行填充
方法一:

[root@desktop5?mnt]#?vim?/etc/vimrc?map?<f4>?ms:call?WESTOS()<cr>'s??????? ##ms:執行命令時,不提示報錯function?WESTOS()????????? call?append(0,"#################################")????????? call?append(1,"#?Author?:???????Hao????????????#")????????? call?append(2,"#?Mail?:?????????Hao@westos.com?#")????????? call?append(3,"#?Version?:??????1.0????????????#")????????? call?append(4,"#?Create_Time:???".strftime("%Y-%m-%d")."?????#")????##時間更新 call?append(5,"#?Description:??????????????????#")????????? call?append(6,"#################################") endfunction</cr></f4>

方法二:利用.來承接后面的#

map?<f4>?ms:call?WESTOS()<cr>'sfunction?WESTOS()????????? call?append(0,"#################################")????????? call?append(1,"#?Author?:???????Hao".("????????????#"))????????? call?append(2,"#?Mail?:?????????Hao@westos.com".("?#"))????????? call?append(3,"#?Version?:??????1.0???????????".("?#"))????????? call?append(4,"#?Create_Time:???".strftime("%Y-%m-%d").("?????#"))????????? call?append(5,"#?Description:?????????????????".("?#"))????????? call?append(6,"#################################") endfunction</cr></f4>

測試:

[root@desktop5?mnt]#?vim?westos.sh????????##按‘F4’執行填充

【Linux學習】shell腳本語言

實驗二:執行新建以.sh結尾的vim文件時,自動填充
注意:舊文件不自動填充
方法一:

[root@desktop5?mnt]#?vim?/etc/vimrc?autocmd?BufNewFile?*.sh?exec?":call?WESTOS()"????##新文件,以.sh結尾,執行,調用文件"map?<f4>?ms:call?WESTOS()<cr>'s???????????##此行注釋,在此"表注釋function?WESTOS() ?????????call?append(0,"#################################") ?????????call?append(1,"#?Author?:???????Hao????????????#") ?????????call?append(2,"#?Mail?:?????????Hao@westos.com?#") ?????????call?append(3,"#?Version?:??????1.0????????????#") ?????????call?append(4,"#?Create_Time:???".strftime("%Y-%m-%d")."?????#") ?????????call?append(5,"#?Description:??????????????????#") ?????????call?append(6,"#################################") ?????????call?append(7,"") ?????????call?append(8,"#!/bin/bash")endfunction</cr></f4>

方法二:

[root@desktop5?mnt]#?vim?/etc/vimrc?autocmd?BufNewFile?*.sh?exec?":call?WESTOS()""map?<f4>?ms:call?WESTOS()<cr>'s function?WESTOS() ?????????call?append(0,"#################################") ?????????call?append(1,"#?Author?:???????Hao".("????????????#")) ?????????call?append(2,"#?Mail?:?????????Hao@westos.com".("?#")) ?????????call?append(3,"#?Version?:??????1.0???????????".("?#")) ?????????call?append(4,"#?Create_Time:???".strftime("%Y-%m-%d").("?????#")) ?????????call?append(5,"#?Description:?????????????????".("?#")) ?????????call?append(6,"#################################") ?????????call?append(7,"") ?????????call?append(8,"#!/bin/bash") endfunction</cr></f4>

【Linux學習】shell腳本語言
測試:

[root@desktop5?mnt]#?vim?file1.sh??##新建以.sh結尾的文件,自動填充

2.shell腳本練習

練習一:顯示當前主機ip地址

[root@desktop5?mnt]#?vim?ip_show.sh#!/bin/bashifconfig?eth0?|?awk?-F?"?"?'/inet?/{print?$2}'??##inet所在行,以空格間隔,第二個字符

【Linux學習】shell腳本語言
測試:

[root@desktop5?mnt]#?sh?ip_show.sh

【Linux學習】shell腳本語言
練習二:顯示當前主機中能登陸系統的用戶

[root@desktop5?mnt]#?vim?user_show.sh#!/bin/bashawk?-F?:?'/bash$/{print?$1}'?/etc/passwd??????##以bash結尾,打印出第一個字符

【Linux學習】shell腳本語言
測試:
【Linux學習】shell腳本語言
練習三:執行命令后可清空日至
方法一:

[root@desktop5?mnt]#?vim?clear_log.sh#!/bin/bash&gt;?/var/log/messages

方法二:

[root@desktop5?mnt]#?vim?clear_log.sh#!/bin/bashecho?""?&gt;?/var/log/messages

測試:

[root@desktop5?mnt]#?chmod?+x?clear_log.sh?[root@desktop5?mnt]#?/mnt/clear_log.sh

【Linux學習】shell腳本語言

【推薦課程:linux視頻教程

? 版權聲明
THE END
喜歡就支持一下吧
點贊14 分享