当前位置:网站首页>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
边栏推荐
- JS parasitic combinatorial inheritance
- Sword finger offer note: T39. Numbers that appear more than half of the time in the array
- [untitled] multimodal model clip
- [网摘][医学影像] 常用的DICOM缩略图解释以及Viewer converter 转换工具
- Firewalld防火墙
- Greek alphabet reading
- The difference between microcomputer and single chip microcomputer
- Matlab draws Bode diagram with time delay system
- 【无标题】多模态模型 CLIP
- STS下载教程(include官网无法下载解决方案)
猜你喜欢

Japan Fukushima waste dump safety monitoring agreement will recognize the "safety" of the sea discharge plan

基于反馈率的控制系统原理

TapNet: Multivariate Time Series Classification with Attentional Prototypical Network

TapNet: Multivariate Time Series Classification with Attentional Prototypical Network

SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series

Keil MDK编译出现..\USER\stm32f10x.h(428): error: #67: expected a “}“错误的解决办法

Temporary use of solo, difficult choice of Blog

go入门篇 (4)
![[machine learning whiteboard derivation series] learning notes - probability graph model and exponential family distribution](/img/cc/a27db6c5a380102029b85255df79c9.png)
[machine learning whiteboard derivation series] learning notes - probability graph model and exponential family distribution

Source code compilation and installation lamp
随机推荐
Source code compilation and installation lamp
剑指 Offer 笔记: T57 - I. 和为 s 的两个数字
How to make a graph? Multiple subgraphs in a graph are histogram (or other graphs)
Shell script text three swordsman awk
检定和校准的区别
go语言之sync.Map
Japan Fukushima waste dump safety monitoring agreement will recognize the "safety" of the sea discharge plan
剑指 Offer 笔记: T53 - II. 0~n-1 中缺失的数字
shell中的while循环实例
我在英国TikTok做直播电商
关于离线缓存Application Cache /使用 manifest文件缓存
LeetCode 03: T58. 最后一个单词的长度(简单); 剑指 Offer 05. 替换空格(简单); 剑指 Offer 58 - II. 左旋转字符串(简单)
NPM step pit
V-show failure
Sword finger offer notes: T53 - ii Missing numbers from 0 to n-1
USB network card drive data stream
硬刚甲方后:中国移动 4908 万大单被废
In the first half of the year, the number of fires decreased by 27.7%. Guangdong will improve the fire safety quality of the whole people in this way
剑指 Offer 笔记: T53 - I. 在排序数组中查找数字
go入门篇 (3)