600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > linux-运维进阶-09 编写Shell脚本

linux-运维进阶-09 编写Shell脚本

时间:2021-05-20 15:50:30

相关推荐

linux-运维进阶-09 编写Shell脚本

linux-运维进阶-09 编写Shell脚本

可以将Shell终端解释器当作人与计算机硬件之间的“翻译官”,它作为用户与Linux系统内部的通信媒介,除了能够支持各种变量与参数外,还提供了诸如循环、分支等高级编程语言才有的控制结构特性。

交互式(Interactive):用户每输入一条命令就立即执行。批处理(Batch):由用户事先编写好一个完整的Shell脚本,Shell会一次性执行脚本中诸多的命令。

指定解释器

脚本的第一行

用于指定脚本使用哪个shell程序做为脚本中命令的解释器

使用bash做为脚本命令的解释器

#!/bin/bash

使用zsh做为脚本的命令解释器

#!/bin/zsh

创建一个脚本

[root@localhost ~]# vim hello.sh里面输入:#!/bin/bashecho "helloworld"保存退出

执行一个脚本

[root@localhost ~]# ./hello.sh-bash: ./hello.sh: Permission denied#发现你的权限不够[root@localhost ~]#

添加执行权限

脚本写完后运行,利用./执行发现权限不够。Linux文件创建之后默认是没有可执行权限的,可以用chmod给文件或目录添加上可执行权限

[root@localhost ~]# ll[root@localhost ~]# chmod +x hello.sh [root@localhost ~]# ll[root@localhost ~]# ./hello.sh helloworld[root@localhost ~]#

上面两次ll出来的文件列表,是为了对比出添加权限前后文件权限的变化,x表示可执行。

没有执行权限情况下想要执行脚本的两种方式

首先咱们在新建一个脚本[root@localhost ~]# vim aaa.sh里面输入:#!/bin/bashecho "hello aaa"保存退出验证它无权限被执行[root@localhost ~]# ./aaa.sh-bash: ./aaa.sh: Permission denied

解释器后面跟上脚本

[root@localhost ~]# bash aaa.sh hello aaa

点加空格后面跟上脚本或者脚本的全路径

[root@localhost ~]# . aaa.sh hello aaa

Shell脚本执行的几种方法

在脚本拥有可执行权限之后,我们就可以去执行脚本,执行脚本有以下几种方式:

在脚本所在目录下时

[root@localhost ~]# ./hello.sh helloworld[root@localhost ~]# . hello.sh helloworld[root@localhost ~]# bash hello.sh helloworld[root@localhost ~]# sh hello.sh helloworld[root@localhost ~]#

不在脚本所在目录下时

[root@localhost ~]# mkdir -p a/b/c/d[root@localhost ~]# cd a[root@localhost a]# bash /root/hello.sh helloworld[root@localhost a]# cd b/c/d[root@localhost d]# pwd/root/a/b/c/d[root@localhost d]# ./../../../../hello.sh helloworld[root@localhost d]#

接受用户的参数

脚本的参数就是脚本后面跟的用户输入的选项或者是变量

在脚本中

脚本的名字使用$0来表示脚本的参数个数使用$#表示脚本的第N个参数使用$N表示(例如脚本第三个参数$3)

[root@localhost ~]# vim hello.sh 输入:#!/bin/bashecho "脚本名称:$0"echo "参数个数:$#"echo "第一个参数:$1"echo "第二个参数:$2"echo "第三个参数:$3"echo "第四个参数:$4"echo "第五个参数:$5"

执行该脚本:

[root@localhost ~]# bash hello.sh 1 2 3 4 5

判断用户参数(下面两种写法)

[root@localhost ~]# vim isroot.sh输入:#!/bin/bashif [ $USER == root ];thenecho "当前用户是管理员"elseecho "当前用户不是管理员" fi[ $USER = root ] && echo "当前用户是管理员" || echo "当前用户不是管理员"exit 0

执行该脚本:

[root@localhost ~]# bash isroot.sh 当前用户是管理员当前用户是管理员[root@localhost ~]#

可用的整数比较运算符

常见的字符串比较运算符

[root@localhost ~]# [ $LANG="EN.US.UTF-8" ]&&echo "is English"is English[root@localhost ~]#

Shell中的四则运算

注意:方括号外取值符号$不能少,方括号内一定要用空格把方括号和里面的命令隔开

[root@localhost ~]# i=$[10 * 10][root@localhost ~]# echo $i100[root@localhost ~]# echo $[999 + 999]1998[root@localhost ~]#

写一个判断内存是否够用的脚本

[root@localhost ~]# vim isfree.sh

输入:

#!/bin/bash# 此脚本用于判断内存是否够用,标准为空闲内存大于总内存的20%(说明脚本功能)# bash isfree.sh (说明脚本的用法)# 作者:feng(说明脚本作者)# qq:略(说明作者的联系方式)# 可能有些脚本里有复杂参数或者函数,请在最下面进行说明Totalmem=`free -m | grep Mem | awk '{print $2}'`Freemem=`free -m | grep Mem | awk '{print $4}'`i=$[ $Totalmem/5 ][ $Freemem -gt $i ] && echo "内存够用" || echo "内存紧缺,请抓紧清理内存"

执行:

[root@localhost ~]# bash isfree.sh 内存够用[root@localhost ~]#

if流程控制语句

上面判断用户参数时,我们已经使用过了if语句

现在再来一次

linux shell 中判断文件、目录是否存在

-e filename 如果 filename存在,则为真

-d filename 如果 filename为目录,则为真

-f filename 如果 filename为常规文件,则为真

-L filename 如果 filename为符号链接,则为真

-r filename 如果 filename可读,则为真

-w filename 如果 filename可写,则为真

-x filename 如果 filename可执行,则为真

-s filename 如果文件长度不为0,则为真

[root@localhost ~]# vim 1.sh

输入:

#!/bin/bashDIR="/root/test"if [ ! -d $DIR ]thenecho "目录不存在" mkdir /root/testelseecho "目录已经存在"fiexit 0

[root@localhost ~]# bash 1.sh 目录不存在[root@localhost ~]# bash 1.sh 目录已经存在

结果如下图:

判断主机是否能正常访问百度的脚本

[root@localhost ~]# vim baidu.sh

输入:

#!/bin/bash# example: ./testhost.sh if [ $# -eq 1 ] thenping -c 4 -i 0.2 -W 3 $1 &> /dev/nullelse echo "example: $0 "fiif [ $? -eq 0 ] thenecho "主机$1能正常访问"exit 0elseecho "主机$1无法访问"exit 1fi

[root@localhost ~]# bash baidu.sh example: baidu.sh 主机能正常访问[root@localhost ~]#

判断成绩的脚本

[root@localhost ~]# vim result.sh

#!/bin/bashread -p "Enter your score(0-100): " scif [ $sc -gt 100 ] || [ $sc -lt 0 ] ; thenecho "Error,Please input score 0~100!"exit 2elif [ $sc -ge 85 ] && [ $sc -le 100 ]; thenecho "$sc is Excellent"elif [ $sc -ge 60 ] && [ $sc -le 84 ]; thenecho "$sc is Pass"elseecho "$sc is Fail"fi

[root@localhost ~]# bash result.sh Enter your score(0-100): 9090 is Excellent[root@localhost ~]#

for循环

题目:在linux上创建一个脚本,名为/root/makeusers,此脚本能实现为系统创建本地用户,并且这些用户的用户名来自一个包含用户名列表的文件。

同时满足下列要求:

此脚本要求提供一个参数,此参数就是包含用户列表的文件

如果没有提供参数,此脚本应该给出下面的提示信息 Usage:/root/makeusers ,然后退出并返回相应的值

如果提供一个不存在的文件名,此脚本应该给出下面的提示信息 Input file not found 然后退

出并返回相应的值

创建的用户登录 shell 为/bin/false,此脚本不需要为用户设置密码

首先,了解一下新建用户的命令:

[root@localhost ~]# useradd -s /bin/fasle "testuser"[root@localhost ~]# cat /etc/passwd

通过cat命令查看所有用户信息,可以看到如下图,用户testuser已经成功创建出来了

现在开始写脚本:

[root@localhost ~]# vim foruser.sh

#!/bin/bashif [ $# -ne 1 ];thenecho 'Usage: $0 <userfile><>'exit 1elif [ ! -f "$1" ];thenecho "input file not found"exit 2elsefor USER in $(cat "$1");douseradd -s /bin/false $USERdonefi

新建一个包含用户名的文件,里面随便设置几个用户

[root@localhost ~]# vim user.txt

aaabbbcccddd

执行脚本

[root@localhost ~]# bash foruser.sh user.txt [root@localhost ~]# cat /etc/passwd

如下图,几个用户已经被成功添加了!

把for循环改成wheel循环的写法

另外再写一个脚本:

[root@localhost ~]# vim wheeluser.sh

#!/bin/bashif [ $# -eq 1 ];thenif [ -f "$1" ];thenwhile read username;douseradd -s /bin/false $username &>/dev/nulldone<$1 #跳出循环elseecho "Input file not found"fi elseecho "Usage: $0 <userfile>"fi

重新编辑一下user.txt

[root@localhost ~]# vim user.txt

aaawwwbbbwwwcccwwwdddwww

执行脚本

[root@localhost ~]# bash wheeluser.sh user.txt [root@localhost ~]# cat /etc/passwd

如下图,几个用户已经被成功添加了!

case语句

在 linux上创建一个名为/root/script.sh 的脚本,让其提供下列特性:

当运行/root/script.sh all,输出为 none

当运行/root/script.sh none,输出为 all

当没有任何参数或参数不是 all 或者 none 时,其错误输出产生以下的信息:

/root/script.sh all|none

开始写脚本

[root@localhost ~]# vim script.sh

#!/bin/bashcase $1 inall)echo "none";; none)echo "all";; *) echo "Usage: $0 all|none";; esac

执行脚本:

[root@localhost ~]# bash script.sh allnone[root@localhost ~]# bash script.sh noneall[root@localhost ~]# bash script.sh abcUsage: script.sh all|none[root@localhost ~]#

如下图

本篇到此结束

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。