栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux系统shell脚本基础之while循环

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux系统shell脚本基础之while循环

Linux系统shell脚本基础之while循环
  • 一、脚本要求
  • 二、脚本内容
    • 1、脚本1
    • 2.脚本2
  • 三、执行脚本1结果
  • 四、执行脚本2

一、脚本要求

脚本1:计算从1加到100的值,使用while循环
脚本2:输入一个数,输出1加到这个数的值

二、脚本内容 1、脚本1
[root@192 scripts]# cat cal1_100.sh 
#!/bin/bash
########################################
#Author:jeven
#time:Sat 14 May 2022 08:06:28 PM CST
#filename:cal1_100.sh
#Script description:
########################################
i=0
s=0
while [ "$i" != 100 ]
	do
	i=$(($i+1))
	s=$(($s+$i))
	done
	echo "THE result of '1+2+3..+100' is ==> $s"

2.脚本2
[root@192 scripts]# cat ./cal_sum.sh 
#!/bin/bash
########################################
#Author:jeven
#time:Sat 14 May 2022 08:06:28 PM CST
#filename:cal1_100.sh
#Script description:
########################################
i=0
s=0
read -p "please enter any num:" NUM
while [ "$i" != $NUM ]
	do
	i=$(($i+1))
	s=$(($s+$i))
	done
	echo "THE result of '1+2+3..+100' is ==> $s"

三、执行脚本1结果
[root@192 scripts]# ./cal1_100.sh 
THE result of '1+2+3..+100' is ==> 5050

四、执行脚本2
[root@192 scripts]# ./cal_sum.sh 
please enter any num:200
THE result of '1+2+3..+100' is ==> 20100
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/883917.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号