Linux运维初级教程(一)Shell脚本

序,掌握shell脚本是linux运维工程师的最基础技能。php

 

1、脚本语言

与高级语言不通,脚本语言运行时须要调用相应的解释器来翻译脚本中的内容。linux

常见的脚本语言有Shell脚本、Python脚本、php脚本、Perl脚本等。shell

2、Shell脚本格式范例

第一行#!的做用是指定该脚本程序的命令解释器bash

#!/bin/bash
#
#

echo "Hello the world"
exit 0

3、运行脚本的方式

1 赋予权限,直接运行脚本运维

chmod a+x print.sh
/root/print.sh #经过绝对路径执行脚本
./print.sh #经过相对路径执行脚本

2 没有权限,经过bash或sh运行脚本函数

bash print.sh #调用bash程序解释器脚本内容执行
sh print.sh #调用sh程序解释脚本内容并执行

3 没有权限,经过.或source运行脚本linux运维

source print.sh #
. print.sh #

4、Shell语法之判断语句

1 if语句

if条件spa

then翻译

命令序列code

fi

if条件

then

命令序列

else

命令序列

fi

if条件

then

命令序列

elif条件

then

命令序列

elif条件

then

命令序列

else

命令序列

fi

     

 

2 case语句

case $变量名称 in

模式1)

命令序列

;;

模式2)

命令序列

;; 

模式N)

命令序列

;;

 *)

esac

case $变量名称 in

模式1|模式2)

命令序列

;;

模式3|模式4)

命令序列

;; 

模式5|模式6)

命令序列

;;

 *)

esac

   

 

 

5、Shell语法之循环语句

1 for语法

语法格式1 语法格式2

for 变量 in 值1 值2 ... 值N

do

命令序列

done

for((初始化变量值;结束循环条件;运算))

do

命令序列

done

 

2 while语法

语法格式1 语法格式2

while [条件]

do

命令格式

done

while read -r line

do

命令序列

done < file

 

3 until语法

until [条件]

do

命令序列

done

4 select语法

select与for循环格式相同

6、Shell语法之控制语句

Shell支持的控制语句有shift、continue、break、exit

7、Shell语法之函数

语法格式1 语法格式2

name(){

命令序列

}

function name{

命令序列

}

 

8、Shell语法之图形脚本

dialog

1 日历对话框

2 选择对话框

3 图形进度条

4 图形密码框

5 消息框

6 确认框

相关文章
相关标签/搜索