我學(xué)習(xí)的系統(tǒng)
CentOS 7.x Linux bogon 3.10.0-693.el7.x86_64
查看系統(tǒng)的shell
方法1:
[pandongmin@bogon ~]$ echo $SHELL
/bin/bash
方法2:
[pandongmin@bogon ~]$ grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
shell 腳本的說(shuō)明
一般是在vi/vim 中編寫茂洒,設(shè)置vi 等同vim .做一個(gè)別名alias vi='vim'
孟岛。
[root@bogon ~]# echo "alias vi='vim'" >>/etc/profile
[root@bogon ~]# tail -l /etc/profile
alias vi='vim'
[root@bogon ~]# source /etc/profile
腳本的第一行#!/bin/bash
或者#!/bin/sh #<==255個(gè)字符以內(nèi)。
督勺,用于指定用哪個(gè)程序解釋腳本渠羞,除了第一行,其他行這種格式是注釋智哀。
這第一行叫幻數(shù)
次询,如果沒(méi)有第一行,則需要指定對(duì)應(yīng)解釋器執(zhí)行腳本:
如果是 Shell 腳本瓷叫,就用 bash test.sh 執(zhí)行 test.sh
如果是 Python 腳本屯吊,就用 python test.py 執(zhí)行 test.py
如果是 expect 腳本,就用 expect test.exp 執(zhí)行 test.exp
查看系統(tǒng)bash 版本
[root@bogon ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@bogon ~]# bash --version
GNU bash, version 4.2.46(2)-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 有嚴(yán)重的漏洞摹菠,會(huì)帶來(lái)傷害盒卸,特別是企業(yè),所以需要及時(shí)更新次氨。
檢測(cè)系統(tǒng)是否存在漏洞:
[root@bogon ~]# env x='() {:;}; echo be careful' bash -c "echo this is a test"
this is a test
如上表示安全蔽介,如果返回以下兩行,請(qǐng)盡快升級(jí)煮寡。
be careful
this is a test
bash 升級(jí)方法:
[root@bogon ~]# yum -y update bash
[root@bogon ~]# rpm -qa bash
bash-4.2.46-29.el7_4.x86_64
shell 腳本與其他編程語(yǔ)言一樣虹蓄,都需要注釋,注釋內(nèi)容是#
號(hào)后面的內(nèi)容洲押。
第一個(gè) Shell 及 Shell 的執(zhí)行方法
在適當(dāng)?shù)哪夸浵聞?chuàng)建學(xué)習(xí)文件夾武花,創(chuàng)建第一個(gè) Shell 并執(zhí)行。
[root@bogon studyShell]# vim test.sh #用 vim 創(chuàng)建文件
echo 'I am pandongmin' #在 vim 里輸入這個(gè)內(nèi)容杈帐,保存并退出
[root@bogon studyShell]# cat test.sh #查看文件的內(nèi)容
echo 'I am pandongmin'
[root@bogon studyShell]# sh test.sh #用 sh 命令執(zhí)行文件
I am pandongmin
[root@bogon studyShell]# bash test.sh #用 bash 命令執(zhí)行文件
I am pandongmin
上面的執(zhí)行方法是指定解釋器的体箕,還有兩種執(zhí)行方法专钉,下面分別介紹:
[root@bogon studyShell]# ./test.sh #這種執(zhí)行方法需要執(zhí)行權(quán)限,如果沒(méi)有權(quán)限無(wú)法執(zhí)行累铅,也無(wú)法補(bǔ)全
-bash: ./test.sh: Permission denied
[root@bogon studyShell]# . test.sh #這種方法不需要權(quán)限跃须,注意命令的空格
I am pandongmin
[root@bogon studyShell]# source test.sh #這種執(zhí)行方法也不需要權(quán)限
I am pandongmin
更改 test.sh 文件的權(quán)限,使其能夠用 ./test.sh
執(zhí)行
[root@bogon studyShell]# chmod u+x test.sh
[root@bogon studyShell]# ./test.sh
I am pandongmin
bash/sh 命令執(zhí)行腳本與用 source/. 命令執(zhí)行腳本的區(qū)別娃兽。
[root@bogon studyShell]# echo 'userdir=`pwd`' >testsource.sh #將一行代碼寫入文件
[root@bogon studyShell]# cat testsource.sh #查看寫入的內(nèi)容
userdir=`pwd`
[root@bogon studyShell]# sh testsource.sh #用這種方法執(zhí)行
[root@bogon studyShell]# echo $userdir #執(zhí)行完成后查看變量
#變量為空
[root@bogon studyShell]# source testsource.sh #用這種方法執(zhí)行
[root@bogon studyShell]# echo $userdir #查看變量菇民,發(fā)現(xiàn)有內(nèi)容
/tmp/studyShell
區(qū)別:
- 用 source/. 執(zhí)行腳本是在本腳本中執(zhí)行,要用到變量和函數(shù)會(huì)將其引入來(lái)投储。所以退出后依然保存變量(包括函數(shù))第练。
- bash/sh 命令執(zhí)行腳本是開(kāi)啟新的子 Shell 執(zhí)行,執(zhí)行完再返回父腳本玛荞,所以變量與函數(shù)不會(huì)保存下來(lái)娇掏。
- 記住這兩種執(zhí)行方法的區(qū)別,說(shuō)不定面試時(shí)會(huì)遇到這種題呢勋眯。
Shell 腳本開(kāi)發(fā)的規(guī)范及習(xí)慣
Shell 腳本的第一行是幻數(shù)婴梧,用于指定執(zhí)行腳本的解釋器:
#!/bin/bash
或
#!/bin/sh
Shell 腳本開(kāi)頭應(yīng)該有些說(shuō)明信息:(可修改~/vimrc
配置文件使其自動(dòng)添加信息)
# Date: 21:25 2017-11-29
# Author: Created by Allen151
# Blog: http://allen.men
# Description: This scripts just is for study
# Version: 1.0
不要使用中文,任何地方都不要使用中文客蹋,這樣就不會(huì)有亂碼的煩惱了塞蹭。設(shè)置編碼:
export LANG="zh_CN.UTF-8"
Shell 腳本擴(kuò)展名為 *.sh
Shell 腳本應(yīng)該存放在固定的路徑下。例如/server/scripts
打代碼時(shí)讶坯,出現(xiàn)成對(duì)的符號(hào)時(shí)番电,應(yīng)該一次性寫出來(lái),再進(jìn)入符號(hào)內(nèi)部添加內(nèi)容闽巩。(同樣可以配置自動(dòng)添加雙符號(hào))
中括號(hào)兩端都應(yīng)該留有空格钧舌,如( [ ] )。
流程控制語(yǔ)句應(yīng)該一次性將其格式鍵入完成涎跨,如:
if 條件內(nèi)容
then
內(nèi)容
if
要規(guī)范縮進(jìn)洼冻,增強(qiáng)代碼的可讀性。
等號(hào)兩邊不能有空格隅很,常規(guī)變量的值用雙引號(hào)撞牢,需要強(qiáng)引用的(即所見(jiàn)即所得)的變量的值用單引號(hào),命令的引用用反引號(hào)(``)叔营。例如:
PDM_NAME="test.txt"
env x='() {:;}; echo be careful'
userdir=`pwd`
各種符號(hào)都必須是英文狀態(tài)下的符號(hào)屋彪。