Shell函数与if、case语句
Shell函数与if、case语句if条件语句的知识与实践if条件语句if条件语句的语法if条件语句其语义类似于汉语里的“如果…那么”。单分支结构if条件表达式;then指令fi双分支结构if条件语句的双分支结构主体则为“如果…那么…否则…”。if条件表达式;then指令集1else指令集2fi多分支结构if条件语句多分支结构的主体为“如果…那么…否则如果…那么否则如果…那么…否则…”。elif可多个if条件表达式1;then指令elif条件表达式2;then指令elif条件表达式3;then指令else指令fiif条件语句多种条件表达式语法[]条件表达式if[字符串 或 算术表达式];then指令fi[[]]条件表达式if[[字符串 或 算术表达式]];then指令fi(())条件表达式if((算术表达式));then指令fiif条件语句实践检测sshd服务是否运行如果未运行则启动sshd服务#!/bin/bashsystemctl is-active sshd/dev/nullif[$?-ne0];thenechosshd is not running, Ill start sshd.systemctl start sshdfi检测sshd服务是否运行如果未运行则启动sshd服务如果运行输出 “Running”。#!/bin/bashsystemctl is-active sshd/dev/nullif[$?-ne0];thenechosshd is not running.echo-nStarting sshd ... ...systemctl start sshdechoDONEelseechosshd is runningfi通过传参控制sshd服务#!/bin/bashif[$1start];thensystemctl start sshdelif[$1stop];thensystemctl stop sshdelif[$1status];thensystemctl status sshdelif[$1restart];thensystemctl restart sshdelseechoUsage:$0start|stop|status|restart fiShell函数的知识与实践Shell函数介绍函数也有类似于别名的作用例如可简化程序的代码量让程序更易读、易改、易用。Shell函数的语法function函数名(){指令...returnn}简化写法1不写()function函数名{指令...returnn}Shell函数的执行函数名 参数1 参数2函数后接参数的说明Shell 的位置参数$1、2…、2…、2…、#、∗、*、∗、?及$)都可以作为函数的参数来使用。此时父脚本的参数临时地被函数参数所掩盖或隐藏。$0 比较特殊它仍然是父脚本的名称。Shell函数的基础实践hello函数[hanhan-shell ~]$catfun1.sh#!/bin/bashfunctionhello(){echoHello World !}hello[hanhan-shell ~]$bashfun1.sh Hello World!# 函数必须先定义后调用[hanhan-shell ~]$catfun2.sh#!/bin/bashhellofunctionhello(){echoHello World !}[hanhan-shell ~]$bashfun2.sh fun2.sh: line2: hello:commandnot found调用外部函数[hanhan-shell ~]$catmylibEOF function hello () { echo Hello World ! } EOF[hanhan-shell ~]$catfun3.sh#!/bin/bashif[-rmylib];thensourcemylibelseechomylib is not existexit1fihello[hanhan-shell ~]$bashfun3.sh Hello World!带参数的函数[hanhan-shell ~]$catfun4.sh#!/bin/bashfunctionprint(){if[$1PASS];thenecho-e\033[1;32mPASS\033[0;39melif[$1FAIL];thenecho-e\033[1;31mFAIL\033[0;39melif[$1DONE];thenecho-e\033[1;35mDONE\033[0;39melseechoUsage: print PASS|FAIL|DONEfi}read-p请输入你想要打印的内容str print$str[hanhan-shell ~]$bashfun4.sh 请输入你想要打印的内容PASSPASS[hanhan-shell ~]$bashfun4.sh 请输入你想要打印的内容hello Usage: print PASS|FAIL|DONEhello函数[hanhan-shell ~]$catfun5.sh#!/bin/bashfunctionprint(){if[$1PASS];thenecho-e\033[1;32mPASS\033[0;39melif[$1FAIL];thenecho-e\033[1;31mFAIL\033[0;39melif[$1DONE];thenecho-e\033[1;35mDONE\033[0;39melseechoUsage:$0PASS|FAIL|DONEfi}str$2print$str# 结果表明脚本的第一个参数并没有传递给脚本内部函数。[hanhan-shell ~]$bashfun5.sh PASS FAIL FAIL# 结果表明$0 仍然使用脚本名而非函数名。[hanhan-shell ~]$bashfun5.sh Usage: fun5.sh PASS|FAIL|DONE函数的递归调用求123…10 的和#!/bin/bashfunctionsum_out(){if[$1-eq1];thensum1elsesum$[$1$(sum_out $[$1 -1])]fiecho$sum}read-p输入一个你想计算和的整数num sum_out$numcase条件语句的应用实践case条件语句的语法case变量值in值1)指令1...;;值2)指令2...;;*)指令3...;;esaccase条件语句实践判断用户输入的数字是否是1、2、3。[hanhan-shell ~]$catcase1.sh#!/bin/bashread-p请输入一个1-3之间数字numcase$numin1)echo您输入的数字是$num;;2)echo您输入的数字是$num;;3)echo您输入的数字是$num;;*)echo请输入一个1-3之间数字。;;esac# 执行验证[hanhan-shell ~]$bashcase1.sh 请输入一个1-3之间数字1 您输入的数字是1[hanhan-shell ~]$bashcase1.sh 请输入一个1-3之间数字4 请输入一个1-3之间数字。实践给输出的字符串加颜色[hanhan-shell ~]$catcase2.sh#!/bin/bashcase$1inPASS)echo-e\033[1;32mPASS\033[0;39m;;FAIL)echo-e\033[1;31mFAIL\033[0;39m;;DONE)echo-e\033[1;35mDONE\033[0;39m;;*)echoUsage:$0PASS|FAIL|DONE;;esaccase语句企业级生产案例控制sshd服务#!/bin/bash case $1 in start) systemctl start sshd ;; stop) systemctl stop sshd ;; restart | reload ) systemctl restart sshd ;; status ) systemctl status sshd ;; *) echo Usage: case-ssh start|stop|restart|reload|status ;; esac简化版#!/bin/bashcase$1instart|stop|restart|reload|status)systemctl$1sshd;;*)echoUsage: case-ssh start|stop|restart|reload|status;;esac管理用户通过传参的方式往 /etc/users 里添加用户具体要求如下。命令用法为Usage: user-mgr [ -add | -del | -search ] username传参要求为参数为-add时表示添加后面接的用户名。如果有同名的用户 则不能添加。参数为-del时表示删除后面接的用户名。如果用户不存在提示用户不存在。参数为-search时表示查找后面接的用户名。 如果用户不存在提示用户不存在。没有用户时应给出明确提示。#!/bin/bash# run as root[$UID-ne0]echoPlease run as rootexit1# create users fileusers_info_file/etc/users[-f${users_info_file}]||touch${users_info_file}# provides two argsif[$#-ne2];thenechoUsage: user-mgr [ -add | -del | -search ] usernameexit2fi# get arg valueaction$1username$2# manager usercase$actionin-search)ifgrep-qusername:$username${users_info_file};thenecho$usernameis exist.elseecho$usernameis not exist.fi;;-add)ifgrep-qusername:$username${users_info_file};thenecho$usernameis exist.elsechattr-i${users_info_file}echousername:$username${users_info_file}echo$usernamehas been added.chattr i${users_info_file}fi;;-del)ifgrep-qusername:$username${users_info_file};thenchattr-i${users_info_file}sed-i/username:$username/d${users_info_file}chattr i${users_info_file}echo$usernamehas been deleted.elseechousername:$username${users_info_file}fi;;*)echoUsage: user-mgr [ -add | -del | -search ] username;;esac