1. 準備工具
添加到/etc/vimrc
autocmd BufNewFile *.py,*.cc,*.sh,*.java,*.bash,Dockerfile,docker-compose.yml exec ":call SetTitle()"func SetTitle() if expand("%:e") =~ 'sh\|bash' call setline(1,"#!/bin/bash")call setline(2, "##############################################################") call setline(3, "# File Name: ".expand("%"))call setline(4, "# Version: V1.0")call setline(5, "# Author: zbl")call setline(6, "# Organization: www.zbl.com")call setline(7, "# Description:")call setline(8, "##############################################################")call setline(9, "")endif if expand("%") == 'Dockerfile' call setline(1, "#####################Dockerfile###############################")call setline(2, "##############################################################") call setline(3, "# File Name: ".expand("%"))call setline(4, "# Version: V1.0")call setline(5, "# Author: zbl")call setline(6, "# Organization: www.zbl.com")call setline(7, "# Description:")call setline(8, "##############################################################")call setline(9, "")call setline(10, "FROM")call setline(11, "LABEL maintaniner='zbl zbl@qq.com' author=zbl")call setline(12, "CMD []")endif if expand("%") == 'docker-compose.yml' call setline(1, "#####################docker-compose###########################")call setline(2, "##############################################################") call setline(3, "# File Name: ".expand("%"))call setline(4, "# Version: V1.0")call setline(5, "# Author: zbl")call setline(6, "# Organization: www.zbl.com")call setline(7, "# Description:")call setline(8, "##############################################################")call setline(9, "")call setline(10, "version: '3.3'")call setline(11, "services:")call setline(12, "volumes:")endif
endfunc
- vim
- sublime/notepad(大一些腳本使用)
- 故障案例:windows書寫的腳本上傳到Linux運行,出現錯誤
原因:windows下回車符號是\n\r,linux下回車符號就是\n,執行windows下寫的內容報錯
解決:dos2unix xxx.sh #windows格式轉換linux格式
2. 腳本基本必會內容
2.1. #!/bin/bash
- 指定命令解釋器
- 用于給系統運行或指定服務運行的腳本
- #! shabang符號
2.2. 運行腳本方法
運行腳本方法 | 應用場景 |
bash/sh | 書寫的腳本通過sh/bash運行 |
路徑方法運行 | 服務軟件運行腳本需要這樣使用 |
source 或 . | 運行腳本,加載帶有變量,自定義函數庫 /etc/profile使用 實現include功能,加載子腳本 |
3. 變量
- 普通變量
- 環境變量
- 特殊變量
3.1. 普通變量
- 命名規則
-
- 不能以數字開頭
- 不要使用單獨字母,最好看見變量名字就知道變量作用(memory_total)
- ${} 引用變量
3.2. 環境變量
- 全局變量(系統定義的)
環境變量 | 說明 |
LANG | 語言字符集,export LANG=en_US.UTF-8,localectl永久修改 |
PS1 | 命令行格式 |
PATH | 存放命令的路徑 |
UID | 當前的用戶UID,判斷中使用,檢查當前用戶是否為root |
- export創建或修改全局變量
3.3. 特殊變量
3.3.1. 常用必會的特殊變量
- 腳本中使用
特殊變量 | 說明 |
$n | 數字,腳本傳參功能,$1表示第一個參數,$2表示第二個參數 |
$0 | 腳本名字,有路徑或無路徑 |
$# | 腳本參數的個數,一般與判斷結合做檢查 |
$* | 取出腳本所有參數相當于替你寫了$1,$2,$3......,一般搭配循環 |
$@ | 取出腳本所有參數相當于替你寫了$1,$2,$3......,一般搭配循環 |
$? | 上一個命令返回值是否正確,0表示正確,非0表示不正確,腳本判斷使用 |
[root@ky201 /study]# cat show.sh
#!/bin/bash
##############################################################
# File Name: show.sh
# Version: V1.0
# Author: zbl
# Organization: www.zbl.com
# Description:
##############################################################cat<<EOF
腳本前三個參數:$1 $2 $3
腳本名字:$0
腳本參數個數:$#
腳本所有參數:$*
腳本所有參數:$@
EOF
[root@ky201 /study]# bash show.sh 1 2 3 4
腳本前三個參數:1 2 3
腳本名字:show.sh
腳本參數個數:4
腳本所有參數:1 2 3 4
腳本所有參數:1 2 3 4
- 面試題:$n,n大于9會有什么問題?
[root@ky201 /study]# cat show.sh
#!/bin/bash
##############################################################
# File Name: show.sh
# Version: V1.0
# Author: zbl
# Organization: www.zbl.com
# Description:
##############################################################cat<<EOF
腳本前三個參數:$1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11
腳本名字:$0
腳本參數個數:$#
腳本所有參數:$*
腳本所有參數:$@
EOF[root@ky201 /study]# bash show.sh {a..z}
腳本前三個參數:a b c d e f g h i a0 a1
腳本名字:show.sh
腳本參數個數:26
腳本所有參數:a b c d e f g h i j k l m n o p q r s t u v w x y z
腳本所有參數:a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@ky201 /study]# [root@ky201 /study]# bash show.sh {a..z}
腳本前三個參數:a b c d e f g h i j k
腳本名字:show.sh
腳本參數個數:26
腳本所有參數:a b c d e f g h i j k l m n o p q r s t u v w x y z
腳本所有參數:a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@ky201 /study]#
4. 基礎判斷
4.1. if判斷格式
#單分支判斷
if [ 條件 ];then執行的命令
fiif [ 條件 ]
then執行的命令
fi#雙分支判斷
if [ 條件 ];then滿足條件執行的命令
else不滿足條件執行的命令
fiif [ 條件 ]
then滿足條件執行的命令
else不滿足條件執行的命令
fi
4.2. 條件:比較大小
比較大小 | 符號 |
等于 | -eq(equal) |
不等于 | -ne(not equal) |
大于 | -gt(great then) |
大于等于 | -ge(great equal) |
小于 | -lt(less than) |
小于等于 | -le(less equal) |
4.3. 案例
- 案例01:書寫ping檢查域名/ip的腳本,成功輸出ip/域名可以訪問,否則輸出無法訪問
#!/bin/bash
##############################################################
# File Name: check_ip.sh
# Version: V1.0
# Author: zbl
# Organization: www.zbl.com
# Description:
###############################################################1.vars
ip=$1#1.1 check
if [ $# -ne 1 ];thenecho "use $0 ip/url"exit 1#退出腳本#返回值是1
fi#2.ping
ping -c2 $ip >/dev/null 2>&1#3.if
if [ $? -eq 0 ];thenecho "${ip} 可以訪問"
elseecho "${ip} 不可以訪問"
fi
- 案例02:檢查指定用戶是否存在,存在輸出用戶存在,不存在輸出用戶不存在。檢查參數個數
#!/bin/bash
##############################################################
# File Name: check_user.sh
# Version: V1.0
# Author: zbl
# Organization: www.zbl.com
# Description:
###############################################################1.vars
user=$1#2.check
if [ $# -ne 1 ];thenecho "use $0 username"exit 1
fi#3.id user
id ${user}>/dev/null 2>&1
if [ $? -eq 0 ];thenecho "用戶存在"
elseecho "用戶不存在"
fi
案例03: 書寫回收站腳本執行rm命令的時候調用mv命令移動指定文件或目錄到/recycyle/目錄下
- 編寫腳本
#!/bin/bash
##############################################################
# File Name: recycle.sh
# Version: V1.0
# Author: zbl
# Organization: www.zbl.com
# Description:
###############################################################1.vars
files="$*"#2.check
if [ $# -eq 0 ];thenecho "use rm files dir"exit 1
fi#3.check root
if [ $UID -ne 0 ];thenecho "please use mv"exit 2
fi#4.創建臨時目錄
tmpdir=`mktemp -dp /recycle/`#5.mv
mv ${files} ${tmpdir}#6.輸出結果
echo "文件已經移動到回收站:${tmpdir}"
- 使用別名
#臨時使用別名,然后測試腳本
alias rm='bash /server/scripts/recycle.sh'#寫入到/etc/profile文件中使其永久生效
vim /etc/profile
alias rm='bash /server/scripts/recycle.sh'
5. 基礎循環
- for 循環
5.1. 基本格式
for n in {1..10}
do命令
donefor n in 1 2 3 3 4 5 6 7 8 9 10
doecho $n
done
5.2. 案例
- 案例01:批量添加用戶
for name in zbl{01..10}
douseradd $name
done
- 案例02:批量檢查linux系統中可登錄用戶的uid,gid信息
- 步驟
1.取出所有linux系統中可登錄用戶名
2.交給for循環
3.對用戶進行檢查,取出uid,gid
4.輸出
- 編寫腳本
#!/bin/bash
##############################################################
# File Name: 02-userid.sh
# Version: V1.0
# Author: zbl
# Organization: www.zbl.com
# Description:
###############################################################1.vars
users=`grep '/bin/bash' /etc/passwd | awk -F ':' '{print $1}'`#2.for
for name in $users
douid=`id $name | awk -F '[= ]' '{print $2}'`gid=`id $name | awk -F '[= ]' '{print $4}'`group=`id $name | awk -F '[= ]' '{print $6}'`echo "用戶名:$name,用戶的uid:$uid,用戶的gid:$gid,用戶組信息:$group"
done
- 案例03:批量檢查ip或url是否可以訪問
#!/bin/bash
##############################################################
# File Name: 03-ip.sh
# Version: V1.0
# Author: zbl
# Organization: www.zbl.com
# Description:
###############################################################1.vars
ips="$*"#2.check
if [ $# -eq 0 ];thenecho "use $0 ip/url"exit 1
fi#3.for
for ip in $ips
doping -c1 $ip >/dev/null 2>&1if [ $? -eq 0 ];thenecho "$ip 可以訪問"elseecho "$ip 不可以訪問"fi
done