【linux/shell】shell中使用for循环读取数据

目录

一.for循环从列表中读取数据的几种形式

二.for循环从配置文件读取数据

三.for循环用通配符读取目录

四.for循环带有数字变量


一.for循环从列表中读取数据的几种形式

bash 复制代码
#!/bin/bash
listl="aa bb cc"
list2='aa bb cc'

for i in $list3
do
    echo $i
done

使用这种形式的列表时需要使用${list[@]}这样的形式访问变量list3=("aa" "bb" "cc")

bash 复制代码
#!/bin/bash
for i in ${list[@]}
do
    echo $i 
done

二.for循环从配置文件读取数据

从配置文件中读取数据:

bash 复制代码
#!/bin/bash
for moudle in $(cat PRE Config)
do
    echo $moudle
done

三.for循环用通配符读取目录

bash 复制代码
#!/bin/bash
for file in /home/rich/*
do
    if [ -d "$file" ]
    then
        echo "$file is a directory"
    elif [ -f "$file" ]
        echo "$file is a file"
    fi
done

四.for循环带有数字变量

第一种:使用C语言风格for循环

bash 复制代码
for(i=0;i<10;i++)

{
  printf("number is %d/n",i)
}

第二种:

bash 复制代码
for k in {1..14}
do
    n1='cat result1 |sed-n "$k"p | awk '{print $0}'
    n2='cat result2| sed -n "$k"p | awk '{print $0'}
    echo "scale=6; $n1/$n2"bc | awk '{printf "%.6f" $0}'>> result
done
相关推荐
yuanyxh6 分钟前
前端仔的一次运维实践
linux·运维·前端
wkj0011 小时前
conda创建在指定目录创建项目
linux·windows·conda
望获linux3 小时前
【Linux基础知识系列:第一百五十九篇】磁盘健康监测:smartctl
linux·前端·数据库·chrome·python·操作系统·软件
Sylvia@8885 小时前
18.SELInux安全性
linux·运维·服务器
Xの哲學5 小时前
Linux NAPI 架构详解
linux·网络·算法·架构·边缘计算
碼不停提6 小时前
linux 内核架构
linux
gplitems1237 小时前
Technox – IT Solutions & Services WordPress Theme: A Practical
linux·服务器·数据库
Deamon Tree7 小时前
后端开发常用Linux命令
linux·运维·python
key_Go8 小时前
0.基于Centos-Stream8 搭建Openstack环境
linux·运维·centos·openstack
wacpguo8 小时前
centos 配置网络
linux·网络·centos