Linux | shell for 循环 1

2023-04-30  本文已影响0人  iBioinformatics

抛砖引玉:
要设定特定的循环列表,可以由文件导入,也可以由{} 导入。
比如要统计每个BAM文件里的reads数目,用for 循环可以如下:

for i in H3K4me1_{0,1,4,12}hour.bam;do echo $i; samtools view -c $i ;done

多行注释

<<COMMENR
for i in mysql_{0,1,4,12}sql #多个文件
do
     echo $i
     samtools view -c $i
done
COMMENT

Shell for循环语法

for 变量 in 列表
do
     command1
     command2
     ...
     commandN
done

for 循环有三种结构:

一、分类

第一类:数字性循环

对等差数列:

#有很多种用法
for i in {1..5};do echo $i;done
for i in {1..5..2};do echo $i;done   #等差
for i in $(seq 1 5);do echo $i;done
for i in $(seq 1 2 5);do echo $i;done
awk 'BEGIN{for(i=1; i<=5; i++) print i}'  

示例 1

for((i=1;i<=4;i++));  
    do   
    echo $(expr $i \* 3 + 1);  
done  


4
7
10
13

示例 2 按规定的步数进行跳跃的方式实现列表for循环,例如计算1~100内所有的奇数之和。

#!/bin/bash  

sum=0  
  
for i in {1..100..2}  
do  
    let "sum+=i"  
done  
    
echo "sum=$sum"  

通过 i 的按步数2不断递增,计算sum值为2500。同样可以使用seq命令实现按2递增来计算1~100内的所有奇数之和

for i in $(seq 1 2 100)

seq表示起始数为1,跳跃的步数为2,结束条件值为100。

第二类:字符性循环

最原始的

#!/bin/bash
#使用列表for循环显示周一到周日对应的英文-->学习日期的英文
for day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday
do
     echo "$day"
done
j=4

for ((i=1; i<=j; i++))
    do
    touch file$i && echo file $i is ok
done


file 1 is ok
file 2 is ok
file 3 is ok
file 4 is ok

$ ll
total 0
-rw-rw-r-- 1 linlin linlin 0 Apr 28 18:10 file1
-rw-rw-r-- 1 linlin linlin 0 Apr 28 18:10 file2
-rw-rw-r-- 1 linlin linlin 0 Apr 28 18:10 file3
-rw-rw-r-- 1 linlin linlin 0 Apr 28 18:10 file4

示例1 : 显示当前目录下所有的文件

for i in `ls`;  
do   
    echo $i is file name\! ;  
done   

file1 is file name!
file2 is file name!
file3 is file name!
file4 is file name!

示例 2

for file in $( ls )  
#for file in *  
do  
   echo "file: $file"  
done  

示例 3

for i in file1 file2 file3 ;  
do  
    echo $i is appoint ;  
done  

file1 is appoint
file2 is appoint
file3 is appoint

第三类:路径查找

对从目录提取:

for i in `ls /`;do echo $i;done

用通配符读取目录(无命令)

for file in ~/*; #一级目录下的内容-->并不递归显示!
do
     echo $file is file path \! ; #${file}代表的是文件的全路径
done
for file in ./test/*;  
do  
    echo $file is file path \! ;  
done  

./test/file1 is file path !
./test/file2 is file path !
./test/file3 is file path !
./test/file4 is file path !
for file in $(ls file.*)  
do  
    echo $file is file path \! ;  
done  

总结:现在一般都使用for in结构,for in结构后面可以使用函数来构造范围,比如$()、``这些,里面写一些查找的语法,比如ls test*,那么遍历之后就是输出文件名了。

类C风格的for循环

也被称为计次循环

#!/bin/bash  
  
for((integer = 1; integer <= 5; integer++))  
do  
    echo "$integer"  
done  

第一个表达式(integer = 1)是循环变量赋初值的语句;
第二个表达式(integer <= 5)决定是否进行循环的表达式,退出状态为非0时,将退出for循环执行done后的命令(与C中的for循环条件是刚好相反的);
第三个表达式(integer++)用于改变循环变量的语句。

Shell 中不运行使用非整数类型的数作为循环变量,循环条件被忽略则默认的退出状态是0,for(( ; ; ))为死循环。

类C的for循环计算1~100内所有的奇数之和。

#!/bin/bash  
  
sum=0  
  
for(( i = 1; i <= 100; i = i + 2 ))  
do  
     let "sum += i"  
done  
  
echo "sum=$sum"  

二、练习

练习1:编写脚本清空所有arp缓存记录:

#!/bin/bash
for i in $(arp | tail -n +2|tr -s ' ' |cut -d' ' -f1)
do
  arp -d $i
done

练习2:产生十个随机数:
方法1:

for i in {0..9};do echo $RANDOM;done
30760
17450
19471
11301
9586
5555
22956
27167
11383
5586

方法2:

for i in $(seq 10);do echo $RANDOM;done

练习3:倒数五秒:

#!/bin/bash
echo "准备倒数5秒:"
for i in $(seq 5 -1 1)
do
  echo -en "$i"
done
echo -e "开始"


54321

练习4:批量添加用户:

for i in $(cat /root/users.txt)    --》从列表文件读取文件名
do
  useradd $i
  echo "123456" | passwd --stdin $i --》通过管道指定密码字串
done

参考:
https://www.cnblogs.com/EasonJim/p/8315939.html
https://blog.csdn.net/theomarker/article/details/81191738
https://blog.csdn.net/weixin_44545549/article/details/88936857
https://www.cnblogs.com/klb561/p/10841402.html
https://blog.csdn.net/weixin_43927730/article/details/88264458

上一篇 下一篇

猜你喜欢

热点阅读