美文网首页
Linux | shell for 循环 1

Linux | shell for 循环 1

作者: iBioinformatics | 来源:发表于2023-04-30 08:24 被阅读0次

抛砖引玉:
要设定特定的循环列表,可以由文件导入,也可以由{} 导入。
比如要统计每个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循环;
  • 第二种是不带列表for循环;
  • 第三种是类C风格的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

相关文章

  • Linux Shell:Shell循环语句

    摘要:Linux,Shell Shell中常用循环有for,while Shell循环语法结构 (1)for循环语...

  • shell

    1.双重循环 2.循环脚本 3.循环脚本 代码内容 文件内容 4.Linux shell中2>&1的含义解释 (全...

  • shell脚本

    1)什么是shell脚本 文档: http://www.runoob.com/linux/linux-shell....

  • linux系统shell循环

    拷贝grub文件到其他节点机,执行跟新grub 1.创建shell脚本bat.sh内容如下: #!/bin/bas...

  • Linux Shell——迭代循环

    学习是自己的事情。 scripts are for lazy people. 循环语句是任何一门语言都不能缺失的部...

  • Linux系统与shell环境准备

    Linux的目录结构: Linux基本命令: 常见shell: 如何执行该shell?1.如果该shell有执行权...

  • 2018-02-07 linux shell 中"2>&1"含义

    linux shell 中"2>&1"含义

  • 17. Interview-Linux

    1 用过哪些Linux命令? 2 写过shell脚本吗?shell脚本基本格式? 3 Linux I/O读写方式 ...

  • shell(一)

    程序的三大结构: 顺序、循环、分支shell是一个用C语言编写的程序,他是用户使用LInux的桥梁,shell既是...

  • 嵌入式学习笔记19.11.20

    Linux 的shell基本命令: shell的版本:1.Bourne Shell(sh) 2.C Shell(c...

网友评论

      本文标题:Linux | shell for 循环 1

      本文链接:https://www.haomeiwen.com/subject/ohqijdtx.html