shell 是操作系統中“提供使用者使用界面”的軟件,它包在 linux 內核的外面,為用戶和內核之間的交互提供了一個接口,系統中的命令用 shell 去解釋,shell 接收系統回應的輸出并顯示其到屏幕中。
1.shell簡介
#!/bin/bash??????????幻數,指定解釋器#!/usr/bin/env?bash??自動匹配解釋器
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
方法二:
[root@desktop5?mnt]#?vim?westos.sh#!/bin/bash?-xecho?hello?westos
實驗一:快捷鍵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’執行填充
實驗二:執行新建以.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>
測試:
[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所在行,以空格間隔,第二個字符
測試:
[root@desktop5?mnt]#?sh?ip_show.sh
練習二:顯示當前主機中能登陸系統的用戶
[root@desktop5?mnt]#?vim?user_show.sh#!/bin/bashawk?-F?:?'/bash$/{print?$1}'?/etc/passwd??????##以bash結尾,打印出第一個字符
測試:
練習三:執行命令后可清空日至
方法一:
[root@desktop5?mnt]#?vim?clear_log.sh#!/bin/bash>?/var/log/messages
方法二:
[root@desktop5?mnt]#?vim?clear_log.sh#!/bin/bashecho?""?>?/var/log/messages
測試:
[root@desktop5?mnt]#?chmod?+x?clear_log.sh?[root@desktop5?mnt]#?/mnt/clear_log.sh
【推薦課程:linux視頻教程】
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END