您现在的位置是:网站首页> 编程资料编程资料
Shell中的for和while循环详细总结_linux shell_
2023-05-26
514人已围观
简介 Shell中的for和while循环详细总结_linux shell_
一、for循环
1.数字段形式
复制代码 代码如下:
for i in {1..10}
do
echo $i
done
2.详细列出(字符且项数不多)
复制代码 代码如下:
for File in 1 2 3 4 5
do
echo $File
done
3.对存在的文件进行循环
复制代码 代码如下:
for shname in `ls *.sh`
do
name=`echo "$shname" | awk -F. '{print $1}'`
echo $name
done
4.查找循环(ls数据量太大的时候也可以用这种方法)
复制代码 代码如下:
for shname in `find . -type f -name "*.sh"`
do
name=`echo "$shname" | awk -F/ '{print $2}'`
echo $name
done
5.((语法循环--有点像C语法,但记得双括号
复制代码 代码如下:
for((i=1;i<100;i++))
do
if((i%3==0))
then
echo $i
continue
fi
done
6.seq形式 起始从1开始
复制代码 代码如下:
for i in `seq 100`
do
if((i%3==0))
then
echo $i
continue
fi
done
二、while循环
1.while循环注意为方括号[],且注意空格
复制代码 代码如下:
min=1
max=100
while [ $min -le $max ]
do
echo $min
min=`expr $min + 1`
done
2.双括号形式,内部结构有点像C的语法,注意赋值:i=$(($i+1))
复制代码 代码如下:
i=1
while(($i<100))
do
if(($i%4==0))
then
echo $i
fi
i=$(($i+1))
done
3.从配置文件读取,并可以控制进程数量
复制代码 代码如下:
MAX_RUN_NUM=8
cat cfg/res_card_partition.cfg |grep -v '^$'|grep -v "#" | grep -v grep |while read partition
do
nohup sh inv_res_card_process.sh $partition >log/resCard$partition.log 2>&1 &
while [ 1 -eq 1 ]
do
psNum=`ps -ef | grep "inv_res_card_process" | grep -v "grep" | wc -l`
if [ $psNum -ge $MAX_RUN_NUM ]
then
sleep 5
else
break
fi
done
done
三.循环控制语句
复制代码 代码如下:
# break 命令不执行当前循环体内break下面的语句从当前循环退出.
# continue 命令是程序在本循体内忽略下面的语句,从循环头开始执行
您可能感兴趣的文章:
相关内容
- Shell中的for循环总结_linux shell_
- Shell脚本bash: ./t.sh:/bin/bash^M:损坏的解释器: 没有那个文件或目录_linux shell_
- Shell脚本计算字符串长度和判断字符串为空小技巧_linux shell_
- Shell、Perl、Python、PHP访问 MySQL 数据库代码实例_linux shell_
- Bash脚本内置的调试方法技巧_linux shell_
- Shell脚本配合iptables屏蔽来自某个国家的IP访问_linux shell_
- Shell脚本逐行读取文本文件(不改变文本格式)_linux shell_
- Shell脚本一次读取文件中一行的2种写法_linux shell_
- Shell中的${}、##和%%使用范例_linux shell_
- ssh远程执行命令方法和Shell脚本实例_linux shell_
