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

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

一、脚本语言

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

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

二、Shell脚本格式范例

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

#!/bin/bash
#
#

echo "Hello the world"
exit 0

三、运行脚本的方式

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

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

2 没有权限,通过bash或sh运行脚本

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

3 没有权限,通过.或source运行脚本

source print.sh #
. print.sh #

四、Shell语法之判断语句

1 if语句

if条件

then

命令序列

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

   

五、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循环格式相同

六、Shell语法之控制语句

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

七、Shell语法之函数

语法格式1 语法格式2

name(){

命令序列

}

function name{

命令序列

}

八、Shell语法之图形脚本

dialog

1 日历对话框

2 选择对话框

3 图形进度条

4 图形密码框

5 消息框

6 确认框

原文地址:https://www.cnblogs.com/yaochc/p/4967926.html