linux之shell——函数以及语句

该博客主要介绍脚本编程知识,涵盖脚本中的函数定义与调用,for、while、until等循环语句,if、case等条件判断语句,以及exit、break、continue三种结束方式。还介绍了自动应答命令expect,可用于交互式命令自动执行。

1、脚本中的函数

  • 脚本中的函数是把一个复杂的语句块i定义成一个简单的字符串的方法。
  • 该字符串为函数名
  • 通过函数名来调用函数
  • 需要先进行函数的定义,然后才能调用该函数

函数的定义
函数名称()
{
语句块
}

#!/bin/bash
FILE_TYPE()     #定义函数
{
        [ -z "$1" ] && {
                echo "error  please input the filename folowing $0"
                exit
        }
        [ ! -e "$1" ] && {
                echo " error   $1 is not exist"
                exit
        }
        [ -f "$1" ] && {
                echo "the file is a nomal file"
        }
        [ -b "$1" ] && {
                echo "the file is a block device"
        }
        [ -S "$1" ] && {
                echo "the file is a socket"
        }

}
FILE_TYPE     # 通过函数名称调用函数

2、for 语句

循环语句
for 循环条件
do 执行的动作
done 循环结束标志

循环条件:会依次执行满足的每一个条件,规定循环的次数。

1 num in 1 2 3

[root@localhost mnt]# cat test.sh 
#!/bin/bash
for num in 1 2 3       # num 依次为1 2 3 
do
	echo $num   # 指定的动作是输出num的值
done
[root@localhost mnt]# sh test.sh    # 执行脚本输出结果
1
2
3

2 num in {1…3}

[root@localhost mnt]# cat test.sh 
#!/bin/bash
for num in {1..3} 
do
	echo $num
done
[root@localhost mnt]# sh test.sh 
1
2
3

3 num in $(seq 1 3)

[root@localhost mnt]# cat test.sh 
#!/bin/bash
for num in `seq 1 3` 
do
	echo $num
done
[root@localhost mnt]# sh test.sh 
1
2
3

4 seq 存在一个好处 是可以指定步长。

[root@localhost mnt]# cat test.sh 
#!/bin/bash
for num in $(seq 1 2 10)    # 从1开始  步长为2 到10 结束
do
	echo $num
done
[root@localhost mnt]# sh test.sh    # 输出就是相差2
1
3
5
7
9

5 num的值不一定是数字,也可以是字符

[root@localhost mnt]# cat test.sh 
#!/bin/bash
for num in lala toto lele haha    # 设定区取值范围为几个字符串
do
	echo $num
done
[root@localhost mnt]# sh test.sh    # 运行依次输出这几该字符串
lala
toto
lele
haha
应用示例

10秒倒计时

#!/bin/bash
for num in {10..1}
do
        echo -n " $num "
        echo -ne  "\r"
        sleep 1
done

3、exit、 break、continue

结束的三种方式:
exit :直接结束脚本运行
break:跳出所在的循环语句 继续进行脚本
continue:结束本次循环,进行下次循环

test.sh 脚本正常运行是以下情况,分别在num为3 的使用结束 查看对比效果。
正常运行

[root@localhost mnt]# cat test.sh 
#!/bin/bash
for num in {1..5}
do
	echo $num
done
echo "hello welcome "
[root@localhost mnt]# sh test.sh 
1
2
3
4
5
hello welcome 

exit

[root@localhost mnt]# cat test.sh 
#!/bin/bash
for num in {1..5}
do
	[ "$num" = '3' ] && exit    # exit
	echo $num
done
echo "hello welcome "
[root@localhost mnt]# sh test.sh     # 直接结束脚本的运行
1
2

break

[root@localhost mnt]# cat test.sh 
#!/bin/bash
for num in {1..5}
do
	[ "$num" = '3' ] && break    # break
	echo $num
done
echo "hello welcome "
[root@localhost mnt]# sh test.sh 
1
2
hello welcome     # 结束循环  但是循环之后的语句还是要正常执行

continue

[root@localhost mnt]# cat test.sh 
#!/bin/bash
for num in {1..5}
do
	[ "$num" = '3' ] && continue   #continue
	echo $num
done
echo "hello welcome "
[root@localhost mnt]# sh test.sh 
1
2
4                             # 只是跳过num为3 那一次循环 继续进行下一次循环以及后续的语句
5
hello welcome 

4 、if语句

主要用于进行条件判断

if 语句语法格式:

if          跟条件
then        跟上述条件满足所要执行的动作
fi          if语句结束标志

示例:

[root@localhost mnt]# cat test.sh 
#!/bin/bash
A=$1
B=$2
if
	[ "$A" = "$B" ] 
then
	echo "yes"
fi
[root@localhost mnt]# sh test.sh 2 2    # 满足条件执行 
yes
[root@localhost mnt]# sh test.sh 2 3  # 不满足条件不执行

if else 语法格式:

 if        
 		      条件1
 then       
    	      条件1满足所要执行的动作
 else
              其他情况下执行的动作
 fi           if语句结束标志

示例:

[root@localhost mnt]# cat test.sh 
#!/bin/bash
A=$1
B=$2
if
	[ "$A" = "$B" ] 
then
	echo "yes"
else
	echo "no"
fi
[root@localhost mnt]# sh test.sh 2  2
yes
[root@localhost mnt]# sh test.sh 2  1
no
[root@localhost mnt]# sh test.sh 2  3
no

if else-if else 语法格式:适用于判断的条件较多的时候

     if        
     		       条件1
     then       
        	       条件1满足所要执行的动作
     elif
     			   条件2
    then
    			   条件2 所要执行的动作
    elif
     			   条件3
    then
    			   条件3 所要执行的动作
    ......
     else
                   其他情况下执行的动作
     fi            if语句结束标志

示例:

[root@localhost mnt]# cat test.sh 
#!/bin/bash
A=$1
B=$2
if
	[ "$A" = "$B" ] 
then
	echo "等于"
elif
	[ "$A" -gt "$B" ]
then
	echo "大于"
else
	echo "小于"
fi
[root@localhost mnt]# sh test.sh 1 2
小于
[root@localhost mnt]# sh test.sh 1 1
等于
[root@localhost mnt]# sh test.sh 2 1 
大于

5、while 语句

while循环用于不断执行一系列命令
其格式为:

while          条件
do
			   动作
done           #结束标志

示例:

[root@localhost mnt]# cat test.sh 
#!/bin/bash
i=0
while [ "$i" -ne "10" ]   # 进行循环的条件
do
	echo $i      # 执行的动作
	((i+=1))     # i 自加1
done
[root@localhost mnt]# sh test.sh  运行循环 直到条件不满足结束
0
1
2
3
4
5
6
7
8
9

6、until 语句

  • until 循环执行一系列命令直至条件为 true 时停止。
  • until 循环与 while 循环在处理方式上刚好相反。
  • 一般 while 循环优于 until 循环,极少数情况下,until 循环更加有用。

其格式为:

until        条件
do
             动作
done         #结束标志

示例:

[root@localhost mnt]# cat test.sh 
#!/bin/bash
i=15
until [ "$i" -lt "10" ]
do
	echo $i
	((i--))
done
[root@localhost mnt]# sh test.sh 
15
14
13
12
11
10

7 、case 语句

  • case语句为多选择语句,可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。
  • case 语句在进行判断的时候是并发进行的,处理效率较高,if 语句在进行条件判断的时候是逐条从上往下,处理效率较低。

case语句格式如下:

case  变量   in
			情况1)
			命令
			;;
			情况2)
			命令
			;;
			......
			*)   #剩余的所有情况
			命令
esac

示例

#!/bin/bash
discrip()           # 定义函数
{
echo "action C|B|S|O "
read -p "please input the action following $0 :  " ACTION
case $ACTION in        # case 语句
        c|C)
        echo "$ACTION  is  create !!"
        ;;
        b|B)
        echo "$ACTION  is  backup !!"
        ;;
        S|s)
        echo "$ACTION  is  skip !!"
        ;;
        o|O)
        echo "$ACTION  is overwrite !!"
        ;;
        e|E)
        exit
        ;;
        *)
        echo "error : $ACTION is  unkown action !!"
esac
}

while true
do
        discrip    # while 语句调用函数
done

[root@localhost mnt]# sh test.sh 
action C|B|S|O 
please input the action following test.sh :  g
error : g is  unkown action !!
action C|B|S|O 
please input the action following test.sh :  t
error : t is  unkown action !!
action C|B|S|O 
please input the action following test.sh :  c
c  is  create !!
action C|B|S|O 
please input the action following test.sh :  r
error : r is  unkown action !!
action C|B|S|O 
please input the action following test.sh :  o
o  is overwrite !!
action C|B|S|O 
please input the action following test.sh :  s
s  is  skip !!
action C|B|S|O 
please input the action following test.sh :  e

8、expect

  • expect 相对于语句,更像是一条命令。
  • expect 是自动应答命令用于交互式命令的自动执行
  • spawn 是expect中的监控程序,其运行后会监控命令提出的交互问题
  • send 发送问题答案给交互命令
  • exp_continue 表示当问题不存在时继续回答下面的问题
  • expecte of 表示问题回答完毕退出expect环境
  • interact 表示问题回答完毕留在交互界面
  • set NAME [ lindex $argvn ] 定义变量

yum install expect.x86_64 -y # 安装软件 ,才能使用 expect

expect脚本一般以#!/usr/bin/expect 开头 规定其运行环境
spawn 新建一个进程,这个进程的交互由expect控制

示例

1 存在一个交互式的脚本,

#!/bin/bash
read -p " who are you ? " NAME
read -p " where are you from ? " ADDR
read -p " how old are you ? " AGE
read -p " waht are you doing ? " ACTION

echo "$NAME is from $ADDR,now $AGE yeas old ,$ACTION  "

2 在运行该脚本的时候需要一步一步的输入所要的参数

[root@localhost mnt]# sh question.sh 
 who are you ? shang
 where are you from ? xi'an
 how old are you ? 18
 waht are you doing ? study
shang is from xi'an,now 18 yeas old ,study  

3 使用expect 可以一次回答所有的问题。

vim anwser.exp      # 注意文件后缀 便于区分


#!/usr/bin/expect     # 注意规定运行环境
set NAME [ lindex $argv 0 ]
set ADDR [ lindex $argv 1 ]
set AGE [ lindex $argv 2 ]
set ACTION [ lindex $argv 3 ]     # 定义四个变量
spawn /mnt/question.sh    # 监控需要进行交互式的脚本
expect {
        "who"   { send "$NAME\r";exp_continue }
        "where" { send "$ADDR\r";exp_continue }    exp_continue 表示当问题不存在时继续回答下面的问题
        "how"   { send "$AGE\r";exp_continue }
        "what"  { send "$ACTION\r" }   # 根据问题发送答案
}
expect eof    expect eof   表示问题回答完毕退出expect环境

4 执行
chmod 777 /mnt/question.sh # 被检控的文件必须有可执行权限

[root@localhost mnt]# expect anwser.exp shang xian  18  study # 运行expect  后面跟这问题的答案
spawn /mnt/question.sh
 who are you ? shang
 where are you from ? xian
 how old are you ? 18
 what are you doing ? study
shang is from xian , now 18 yeas old,  study !!   # 一次回答所有的问题并输出结果!

5 也可以将 anwser.exp 修改成可以运行在shell 脚本中

修改后的内容

#!/bin/bash
/usr/bin/expect <<EOF
spawn /mnt/question.sh
expect {
"who"   { send "$1\r";exp_continue }
"where" { send "$2\r";exp_continue }
"how"   { send "$3\r";exp_continue }
"what"  { send "$4\r" }
}
expect eof
EOF

执行结果

[root@localhost mnt]# sh anwser.sh shang 18 xian study
spawn /mnt/question.sh
 who are you ? shang
 where are you from ? 18
 how old are you ? xian
 what are you doing ? study
shang is from 18 , now xian yeas old,  study !!
标题SpringBoot智能在线预约挂号系统研究AI更换标题第1章引言介绍智能在线预约挂号系统的研究背景、意义、国内外研究现状及论文创新点。1.1研究背景与意义阐述智能在线预约挂号系统对提升医疗服务效率的重要性。1.2国内外研究现状分析国内外智能在线预约挂号系统的研究与应用情况。1.3研究方法及创新点概述本文采用的技术路线、研究方法及主要创新点。第2章相关理论总结智能在线预约挂号系统相关理论,包括系统架构、开发技术等。2.1系统架构设计理论介绍系统架构设计的基本原则和常用方法。2.2SpringBoot开发框架理论阐述SpringBoot框架的特点、优势及其在系统开发中的应用。2.3数据库设计与管理理论介绍数据库设计原则、数据模型及数据库管理系统。2.4网络安全与数据保护理论讨论网络安全威胁、数据保护技术及其在系统中的应用。第3章SpringBoot智能在线预约挂号系统设计详细介绍系统的设计方案,包括功能模块划分、数据库设计等。3.1系统功能模块设计划分系统功能模块,如用户管理、挂号管理、医生排班等。3.2数据库设计与实现设计数据库表结构,确定字段类型、主键及外键关系。3.3用户界面设计设计用户友好的界面,提升用户体验。3.4系统安全设计阐述系统安全策略,包括用户认证、数据加密等。第4章系统实现与测试介绍系统的实现过程,包括编码、测试及优化等。4.1系统编码实现采用SpringBoot框架进行系统编码实现。4.2系统测试方法介绍系统测试的方法、步骤及测试用例设计。4.3系统性能测试与分析对系统进行性能测试,分析测试结果并提出优化建议。4.4系统优化与改进根据测试结果对系统进行优化和改进,提升系统性能。第5章研究结果呈现系统实现后的效果,包括功能实现、性能提升等。5.1系统功能实现效果展示系统各功能模块的实现效果,如挂号成功界面等。5.2系统性能提升效果对比优化前后的系统性能
在金融行业中,对信用风险的判断是核心环节之一,其结果对机构的信贷政策和风险控制策略有直接影响。本文将围绕如何借助机器学习方法,尤其是Sklearn工具包,建立用于判断信用状况的预测系统。文中将涵盖逻辑回归、支持向量机等常见方法,并通过实际操作流程进行说明。 一、机器学习基本概念 机器学习属于人工智能的子领域,其基本理念是通过数据自动学习规律,而非依赖人工设定规则。在信贷分析中,该技术可用于挖掘历史数据中的潜在规律,进而对未来的信用表现进行预测。 二、Sklearn工具包概述 Sklearn(Scikit-learn)是Python语言中广泛使用的机器学习模块,提供多种数据处理和建模功能。它简化了数据清洗、特征提取、模型构建、验证与优化等流程,是数据科学项目中的常用工具。 三、逻辑回归模型 逻辑回归是一种常用于分类任务的线性模型,特别适用于二类问题。在信用评估中,该模型可用于判断借款人是否可能违约。其通过逻辑函数将输出映射为0到1之间的概率值,从而表示违约的可能性。 四、支持向量机模型 支持向量机是一种用于监督学习的算法,适用于数据维度高、样本量小的情况。在信用分析中,该方法能够通过寻找最佳分割面,区分违约与非违约客户。通过选用不同核函数,可应对复杂的非线性关系,提升预测精度。 五、数据预处理步骤 在建模前,需对原始数据进行清理与转换,包括处理缺失值、识别异常点、标准化数值、筛选有效特征等。对于信用评分,常见的输入变量包括收入水平、负债比例、信用历史记录、职业稳定性等。预处理有助于减少噪声干扰,增强模型的适应性。 六、模型构建与验证 借助Sklearn,可以将数据集划分为训练集和测试集,并通过交叉验证调整参数以提升模型性能。常用评估指标包括准确率、召回率、F1值以及AUC-ROC曲线。在处理不平衡数据时,更应关注模型的召回率与特异性。 七、集成学习方法 为提升模型预测能力,可采用集成策略,如结合多个模型的预测结果。这有助于降低单一模型的偏差与方差,增强整体预测的稳定性与准确性。 综上,基于机器学习的信用评估系统可通过Sklearn中的多种算法,结合合理的数据处理与模型优化,实现对借款人信用状况的精准判断。在实际应用中,需持续调整模型以适应市场变化,保障预测结果的长期有效性。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值