当前位置:网站首页>While loop instance in shell
While loop instance in shell
2022-07-27 11:57:00 【Big leaves are not small】
shell Medium while Examples of loops _wdz306ling The blog of -CSDN Blog _shell while
1. utilize while Cycle calculation 1 To 100 And :
Sample code 1:
#!/bin/bash
i=1
sum=0
while [ $i -le 100 ]
do
let sum=sum+$i
let i++
done
echo $sum
Sample code 2: utilize while Cycle calculation 1 To 100 The sum of all the odd numbers
#!/bin/bash
i=1
sum=0
while [ $i -le 100 ]
do
let sum=sum+$i
let i+=2
done
echo $sum
Sample code 3: utilize while Cycle calculation 1 To 100 The sum of all the even numbers
#!/bin/bash
i=2
sum=0
while [ $i -le 100 ]
do
let sum=sum+$i
let i+=2
done
echo $sum
2. utilize while Loop printing **
Sample code : utilize while Print a loop 5x5 Of *
#!/bin/bash
i=1
j=1
while [ $i -le 5 ]
do
while [ $j -le 5 ]
do
echo -n "* "
let j++
done
echo
let i++
let j=1
done
3. Use read combination while Loop read text file :
Sample code 1:
#!/bin/bash
file=$1 # Set the position parameter 1 Copy the file name of to file
if [ $# -lt 1 ];then # Judge whether the user has entered the position parameter
echo "Usage:$0 filepath"
exit
fi
while read -r line # from file Read the file content in the file and assign it to line( Using parameter r Will mask special symbols in the text , Only output, no translation )
do
echo $line # Output file content
done < $file
Example 2: Read the contents of the file by column
#!/bin/bash
file=$1
if [[ $# -lt 1 ]]
then
echo "Usage: $0 please enter you filepath"
exit
fi
while read -r f1 f2 f3 # Divide the contents of the document into three columns
do
echo "file 1:$f1 ===> file 2:$f2 ===> file 3:$f3" # Output file contents by column
done < "$file"
4.while The dead circle in the circle :
Example : Using the dead loop , Let the user make a choice , Print the corresponding results according to the customer's choice
#!/bin/bash
# Print menu
while :
do
echo "********************"
echo " menu "
echo "1.tima and date"
echo "2.system info"
echo "3.uesrs are doing"
echo "4.exit"
echo "********************"
read -p "enter you choice [1-4]:" choice
# Do the corresponding operation according to the customer's choice
case $choice in
1)
echo "today is `date +%Y-%m-%d`"
echo "time is `date +%H:%M:%S`"
read -p "press [enter] key to continue..." Key # Suspension loop , Prompt the customer to press enter Key continuation
;;
2)
uname -r
read -p "press [enter] key to continue..." Key
;;
3)
w
read -p "press [enter] key to continue..." Key
;;
4)
echo "Bye!"
exit 0
;;
*)
echo "error"
read -p "press [enter] key to continue..." Key
;;
esac
done
————————————————
Copyright notice : This paper is about CSDN Blogger 「wdz306ling」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/wdz306ling/article/details/79602739
边栏推荐
- Can you really write binary search - variant binary search
- MySQL数据库主从复制集群原理概念以及搭建流程
- Packet transmission: application layer - kernel - hardware
- 剑指 Offer 笔记: T57 - I. 和为 s 的两个数字
- Principle of PWM and generation of PWM wave
- Sword finger offer note: T39. Numbers that appear more than half of the time in the array
- Analysis of the use of JUC framework from runnable to callable to futuretask
- Sword finger offer note: t45. arrange the array into the smallest number
- Introduction to box diagram
- 剑指 Offer 笔记: T58 - I. 翻转单词顺序
猜你喜欢

求不同采样周期下的传递函数有限零点

上半年火灾起数下降27.7%,广东将这样提升全民消防安全素质

Principle, concept and construction process of MySQL database master-slave replication cluster

哈希表 详细讲解

Why is ack=seq+1 when TCP shakes hands three times

日本福岛废堆安全监视协议会认可排海计划“安全”

Finding the finite zero point of transfer function under different sampling periods

EfficientNet

JS parasitic combinatorial inheritance

广东:剧本杀等新行业新业态场所,消防安全监管不再“缺位”
随机推荐
求不同采样周期下的传递函数有限零点
TapNet: Multivariate Time Series Classification with Attentional Prototypical Network
JS-寄生组合式继承
Vscode removes style / syntax highlighting / code highlighting / black background when copying code
STS下载教程(include官网无法下载解决方案)
Leetcode 03: t58. Length of the last word (simple); Sword finger offer 05. replace spaces (simple); Sword finger offer 58 - ii Rotate string left (simple)
go入门篇 (4)
剑指 Offer 笔记: T57 - I. 和为 s 的两个数字
剑指 Offer 笔记: T58 - II. 左旋转字符串
Shell编程之正则表达式(Shell脚本文本三剑客之grep)
Unexpected harvest of epic distributed resources, from basic to advanced are full of dry goods, big guys are strong!
kazoo使用教程
EfficientNet
JUC框架 从Runnable到Callable到FutureTask 使用浅析
箱型图介绍
shell编程之免交互
USB network card drive data stream
N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei
Sword finger offer notes: t58 - ii Rotate string left
pytorch和tensorflow一样展示summary