shell概念與編程規(guī)范

shell編程規(guī)范

一、前言

? 本文將對shell的編程規(guī)范與變量進行講解并且進行實例驗證,適合初學者學習shell基礎。在學習過程中我們需要自己動手,實際操作幾次,邊做邊體會其含義,然后學會自我總結(jié)歸納,逐步提升自己的能力。相信自己你一定可以!

創(chuàng)新互聯(lián)公司專注于企業(yè)網(wǎng)絡營銷推廣、網(wǎng)站重做改版、廬山網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5網(wǎng)站設計、商城建設、集團公司官網(wǎng)建設、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為廬山等各大城市提供網(wǎng)站開發(fā)制作服務。

首先我們介紹一下強語言與弱語言類型的簡單解釋(目前不太理解沒關系):

  • 強語言類型——需要聲明變量類型
  • 弱語言類型——無需聲明變量類型
  • HTML 標記語言 標簽<head></head>

而shell腳本編程,屬于典型的弱語言類型,其中的變量直接定義,無需聲明,并且shell語言沒有面向?qū)ο笏枷搿V恍枰獙⒁獔?zhí)行的命令按順序保存到一個文本文件,給該文件可執(zhí)行權(quán)限,即可運行,當慢慢深入學習shell編程,可以結(jié)合各種Shell控制語句以完成更復雜的操作,結(jié)合shell腳本編程,可以將各種服務等大量操作通過一個腳本直接執(zhí)行,提高工作效率。

當然,如果想要學好shell,一定需要對shell命令比較熟悉,尤其是常用的一些命令以及一些重要的命令如:grep、egrep、sed、awk等。

二、shell的概念

2.1什么是shell?

? shell——在計算機科學中,Shell俗稱殼(用來區(qū)別于核),是指“為使用者提供操作界面”的軟件(命令解析器)。它類似于DOS下的command.com和后來的cmd.exe。它接收用戶命令,然后調(diào)用相應的應用程序。

2.2shell的應用場景

  • 重復性操作
  • 批量事務處理
  • 自動化運維
  • 服務運行狀態(tài)監(jiān)控
  • 定時任務執(zhí)行

2.3shell的作用——命令解釋器

介于系統(tǒng)內(nèi)核與用戶之間,負責解釋命令行,一行執(zhí)行多個不相關的命令使用";"分隔開來執(zhí)行

三、shell編程規(guī)范

? Linux系統(tǒng)中的shell腳本是一個特殊的應用程序,其遵循標準的腳本結(jié)構(gòu),而且能夠輸出友好的提示信息、更加容易讀懂。對應代碼較多、結(jié)構(gòu)復雜的腳本,應當添加必要的注釋文字。實例如下:

[root@localhost shell]# cat demo.sh 
#!/bin/bash               //特殊的腳本聲明,表示此行以后的語句通過/bin/bash程序解釋
#this is a demo shell script   //除了上方的#號鍵其余的#號鍵都表示改行是注釋信息
cd /boot
echo "當前位于:"
pwd
echo "其中以vml開頭的文件包括:"
ls -lh vml*

? 那么該程序如何執(zhí)行呢?首先我們可以直接使用source或者sh命令直接執(zhí)行(注意差別?。。。駝t需要添加其執(zhí)行權(quán)限,然后執(zhí)行。

[root@localhost shell]# ls -l
總用量 16
-rw-r--r--. 1 root root 134 11月 25 19:59 demo.sh   //沒有可執(zhí)行權(quán)限
-rwxr-xr-x. 1 root root 274 11月 25 18:58 state.sh
-rwxr-xr-x. 1 root root 208 11月 25 19:17 sujiaju.sh
-rwxr-xr-x. 1 root root 345 11月 25 19:11 welcome.sh
[root@localhost shell]# source demo.sh 
當前位于:
/boot
其中以vml開頭的文件包括:
-rwxr-xr-x. 1 root root 5.7M 11月 14 11:41 vmlinuz-0-rescue-a33ccb7775134b0e83e04555d56fedb7
-rwxr-xr-x. 1 root root 5.7M 8月  23 2017 vmlinuz-3.10.0-693.el7.x86_64
[root@localhost boot]# cd -
/root/shell
[root@localhost shell]# sh demo.sh 
當前位于:
/boot
其中以vml開頭的文件包括:
-rwxr-xr-x. 1 root root 5.7M 11月 14 11:41 vmlinuz-0-rescue-a33ccb7775134b0e83e04555d56fedb7
-rwxr-xr-x. 1 root root 5.7M 8月  23 2017 vmlinuz-3.10.0-693.el7.x86_64
[root@localhost shell]# 

? 經(jīng)過實踐操作,可以作以下小結(jié):沒有執(zhí)行權(quán)限的shell腳本文件,可以通過source或者sh命令執(zhí)行,但是source命令執(zhí)行之后有路徑切換的操作會切換到該路徑下,而sh命令不會,而對文件直接執(zhí)行是沒有足夠權(quán)限的,需要更改權(quán)限才可執(zhí)行。

四、管道與重定向

? shell的大部分操作過程都位于后臺,不需要用戶干預,因此學會提取、過濾執(zhí)行信息非常重要,下面介紹shell環(huán)境中的兩個I/O操作:管道、重定向。

4.1管道操作

? 前面的結(jié)果作為后面命令的輸入或者是處理對象,同一行命令中可以使用多個管道符號。

? 看下面一實例:

[root@localhost ~]# grep -v "/sbin/nologin$" /etc/passwd  //將系統(tǒng)中允許登錄的用戶檢索出來
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
lokott:x:1000:1000:lokott:/home/lokott:/bin/bash
[root@localhost ~]# tail /etc/passwd | grep "/sbin/nologin$"  | wc -l //統(tǒng)計文件passwd中最后10行的不 9                                                                     允許登錄的用戶總數(shù)

4.2重定向

? 用戶通過操作系統(tǒng)處理信息的過程中,有以下幾類交互設備文件

  • 標準輸入:鍵盤,文件編號為0
  • 標準輸出:顯示器,文件編號為1
  • 標準錯誤:顯示器,文件編號為2

在實際Linux系統(tǒng)維護中,可以改變輸入、輸出內(nèi)容的方向,而不是使用默認的標準輸入、輸出設備(鍵盤和顯示器),這種操作稱為“重定向”。

1)重定向輸入

重定向輸入指的是將命令中接收輸入的途徑由默認的鍵盤改為指定的文件,而不是等待鍵盤輸入。重定向輸入使用“<”操作符。

如我們使用免交互給某個用戶設置登錄密碼的操作實例:

[root@localhost shell]#  echo "111111" |passwd --stdin zhangsan  //一般方法
更改用戶 zhangsan 的密碼 。
passwd:所有的身份驗證令牌已經(jīng)成功更新。
[root@localhost shell]# vim pass.txt 
[root@localhost shell]# cat pass.txt 
123456
[root@localhost shell]# passwd --stdin zhangsan < pass.txt   //使用重定向輸入會失敗
更改用戶 zhangsan 的密碼 。
passwd: 鑒定令牌操作錯誤

根據(jù)以上操作,我們發(fā)現(xiàn)會失敗,這并不是我們命令寫錯了,而是我們Linux系統(tǒng)中的SELinux在作怪:

[root@localhost shell]# tail /var/log/messages           //查看系統(tǒng)日志文件 
Nov 26 14:10:09 localhost python: SELinux is preventing /usr/bin/passwd from ioctl access on the file /root/shell/pass.txt.#012#012*****  Plugin catchall (100. confidence) suggests   **************************#012#012If you believe that passwd should be allowed ioctl access on the pass.txt file by default.#012Then you should report this as a bug.#012You can generate a local policy module to allow this access.#012Do#012allow this access for now by executing:#012# ausearch -c 'passwd' --raw | audit2allow -M my-passwd#012# semodule -i my-passwd.pp#012

然后查看 tail /var/log/audit/audit.log 內(nèi)容就會發(fā)現(xiàn)一個修改密碼failed報告信息。因此我們需要關閉SELinux ,再執(zhí)行。

錯誤信息如下:

type=USER_CHAUTHTOK msg=audit(1574750006.593:666): pid=53587 uid=0 auid=0 ses=11 subj=unconfined_u:unconfined_r:passwd_t:s0-s0:c0.c1023 msg='op=PAM:chauthtok grantors=? acct="zhangsan" exe="/usr/bin/passwd" hostname=localhost.localdomain addr=? terminal=pts/2 res=failed'
type=USER_CHAUTHTOK msg=audit(1574750008.850:667): pid=53587 uid=0 auid=0 ses=11 subj=unconfined_u:unconfined_r:passwd_t:s0-s0:c0.c1023 msg='op=change password id=1001 exe="/usr/bin/passwd" hostname=localhost.localdomain addr=? terminal=pts/2 res=failed'

關閉之后執(zhí)行結(jié)果:

[root@localhost shell]# setenforce 0
[root@localhost shell]# passwd --stdin zhangsan < pass.txt
更改用戶 zhangsan 的密碼 。
passwd:所有的身份驗證令牌已經(jīng)成功更新。

(2)重定向輸出

">"——表示覆蓋

“>>”——表示追加

實例:

[root@localhost shell]# uname -p > kernel.txt
[root@localhost shell]# cat kernel.txt 
x86_64
[root@localhost shell]# uname -p >> kernel.txt
[root@localhost shell]# cat kernel.txt 
x86_64
x86_64
[root@localhost shell]# uname -p > kernel.txt
[root@localhost shell]# cat kernel.txt 
x86_64

(3)錯誤重定向

將出錯的信息存放到指定文件,而不是直接顯示到屏幕上。錯誤重定向使用“2>”操作符。

實例:

[root@localhost shell]# tar jcf /nonedir/ex.bz2 /etc/ 2> error.log
[root@localhost shell]# cat error.log 
tar: 從成員名中刪除開頭的“/”
tar (child): /nonedir/ex.bz2:無法 open: 沒有那個文件或目錄
tar (child): Error is not recoverable: exiting now

重定向與管道操作是shell環(huán)境中十分常用的功能,需要熟練掌握。

本文題目:shell概念與編程規(guī)范
分享網(wǎng)址:http://bm7419.com/article28/goeicp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設、網(wǎng)站建設、外貿(mào)網(wǎng)站建設、響應式網(wǎng)站、手機網(wǎng)站建設、用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設