Shell基礎知識和編程規范


一,Shell環境查看

1.1 查看系統Shell支持情況
[root@linux-node1 ~]# cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
1.2 查看當前系統默認的Shell

方法一

[root@linux-node1 ~]# echo $SHELL
/bin/bash

方法二

[root@linux-node1 ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash

提示:后面/bin/bash就是用戶登陸后的Shell解釋器

二,Shell腳本的建立和執行

2.1 腳本開通(第一行)

一個規范的Shell腳本在第一行會指出由哪個程序(解釋器)來執行腳本中的內容,這一行內容在Linux bash的編程一般為

#!/bin/bash
或
#!/bin/sh #<---255個字符以內

其中,開頭的“#!"字符又稱為幻數,在執行bash腳本的時候,內核會根據“#!”后面的解釋器來確認哪個程序解釋這個腳本中的內容(注意 這一行必須在每個腳本頂端第一行)

2.2 bash與sh的區別

sh為bash的軟連接

[root@linux-node1 ~]# ll /bin/sh
lrwxrwxrwx. 1 root root 4 Aug  6  2016 /bin/sh -> bash
2.3 查看系統版本
[root@linux-node1 ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 

查看bash版本

[root@linux-node1 ~]# bash --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

測試bash是否有破殼漏洞,如果返回be careful,則表示需要盡快升級bash了

[root@xiewenming]# bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
[root@xiewenming]# 
[root@xiewenming]# env x='() { :;}; echo be careful' bash -c "echo this is a test" be careful
this is a test
2.4 升級bash方法
[root@xiewenming]# yum -y update bash
[root@xiewenming]# rpm -qa bash
bash-4.1.2-48.el6.x86_64
[root@xiewenming]# env x='() { :;}; echo be careful' bash -c "echo this is a test"
this is a test  #再次執行已沒有be careful提示

提示:如果沒有輸出be careful,則不需要升級

2.5 腳本注釋

在Shell腳本中,跟再#后面的內容表示注釋,開發腳本時,如果沒有注釋,那么團隊里的其他人就會很難理解加班對應內容的用途,而且時間長了自己也會忘記。為了方便別人和方便自己,避免影響團隊的協助效率,

要養成一個寫關機注釋的習慣。腳本注釋盡量不要用中文,避免亂碼問題

2.6 Shell腳本的執行

當Shell腳本運行時,它會先查找系統環境變量ENV,改變量指定了環境文件,加載順利通車是/etc/profile ---> ~/.bash_profile ---> ~/.bashrc --->/etc/bashrc等,在加載了上述的環境變量文件后,Shell就開始執行腳本中的內容

通常情況下,在執行Shell腳本時,會向系統請求啟動一個新的進程,以便在該進程中執行腳本的命令及子Shell腳本。

提示:設置crond任務時,最好能再定時任務腳本中重新定義系統環境變量,否則,一些系統環境變量將不會被加載,這個問題需要注意!

Shell腳本的執行可以采用以下幾種方式:

1).bash 腳本名 或sh 腳本名 (推薦使用)

[root@linux-node1 ~]# bash test01.sh 
Welcome warren
[root@linux-node1 ~]# sh test01.sh 
Welcome warren

2). 使用腳本的絕對路徑執行 或相對路徑 ./腳本名 這個需要有文件的執行權限

[root@linux-node1 ~]# chmod +x test01.sh 
[root@linux-node1 ~]# /root/test01.sh
Welcome warren
[root@linux-node1 ~]# ./test01.sh
Welcome warren

3).source 腳本名 或  . 腳本名

[root@linux-node1 ~]# source test01.sh 
Welcome warren
[root@linux-node1 ~]# . test01.sh 
Welcome warren

4) sh < 腳本名 或 cat 腳本名|sh

[root@linux-node1 ~]# cat test01.sh 
echo "Welcome warren"
[root@linux-node1 ~]# sh < test01.sh 
Welcome warren
[root@linux-node1 ~]# cat test01.sh |bash
Welcome warren

使用cat 創建腳本文件

[root@linux-node1 ~]# cat > test02.sh
echo "this is test"

Ctrl+d結束編輯,這里作為cat用法的擴展知識

父Shell不會繼承子Shell的環境變量,測試如下

[root@linux-node1 ~]# cat test.sh 
user001=`whoami`
[root@linux-node1 ~]# echo $user001
#空
[root@linux-node1 ~]# 

使用source導入子腳本的環境變量到當前環境就可以獲取子腳本的變量

[root@linux-node1 ~]# source  test.sh 
[root@linux-node1 ~]# echo $user001
root

三, Shell腳本開發的基本規范

Shell 腳本的開發規范及習慣非常重要,有了好的規范可以大大提升開發效率,並能再后期降低對腳本的維護成本。

1). Shell腳本的第一行是指定腳本介紹器

#!/bin/bash

 2). Shell腳本的開通會加班表,版權等信息

#!/bin/bash
#Date: 2017-12-5
#Author: Created by warren
#Blog: http://www.cnblogs.com/xiewenming/
#Description: This scripts function is ...
#Version:1.1

可以修改“~/.vimrc”配置文件配置vim編輯文件時自動加上以上信息

3).  在Shell腳本中盡量不用中文,防止切換系統環境后中文亂碼的困擾。如果非要加中文,請根據系統進行字符集調整.

      如: export LANG="zh_CN.UTF-8",並在腳本中,重新定義字符集設置,和系統保證一致。

4). Shell腳本的命名應以 .sh 為擴展名

     例如: test.sh

5). Shell 腳本應存放在固定的目錄下

     例如:/opt/scripts

四, Shell腳本書寫的良好習慣

1).成對的符合應盡量一次性寫出來,然后退格在符合里增加內容,以防止遺漏。

這些成對的符合包括:

{}  []  ''  ``  ""

2). 中括號兩端至少各一個空格,先退2格 然后進一格,雙括號也是如此

3). 對應流程控制語句,應一次將格式寫完,再添加內容。

 比如,一次性完成if語句的格式,應為

if 條件內容
  then
    內容
fi

一次性完成for循環語句格式,應為

for
do
  內容
done

一次完成white語句格式,應為

white 條件
do
  內容
done

提示:until和case語句也一樣

4). 通過縮進讓代碼更易讀

5). 對應常規變量的字符串定義變量值應加雙引號,並且等號前后不能有空格

6). 腳本中的單引號和雙引號必須為英文狀態下的符合

7). 變量名稱應該具有相關意思,不能太隨便。

說明:好的習慣可以讓我們避免很多不必要的麻煩並提示工作效率

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM